Im truly lost in this code Arduino Program template to perfo
Im truly lost in this code
Arduino
//Program template to perform Pre-Lab task (2)
int icount;
int StartButtonPin = 6;
int tensValue;
int onesValue;
unsigned long currentTimeStamp; //The millis() function
unsigned long endTimeStamp; //will return an unsigned long datatype so we remain consistent
boolean startButtonLast = LOW;
boolean startButtonCurrent = LOW;
boolean loopExit;
void setup()
{
Serial.begin(9600); // This line enables the serial monitor to be used for us to
pinMode(StartButtonPin, INPUT); // print the number of button presses for reading in the console.
//7-Segment LED Display
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
void LEDDisplay(int count) //Can be re-used once you have this completed in the previous template.
{
switch(count) // Each case value also corresponds to the decimal value. Ex) Case 0 is decimal 0 and
//so on.
{
default:
// Display letter \'d\'..
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
case 0:
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
break;
case 1:
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
break;
case 2:// when count value is 2 show”2” on disp
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
break;
case 3:// when count value is 3 show ”3” on disp
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
break;
case 4:// when count value is 4 show”4” on disp
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
break;
case 5:// when count value is 5 show”5” on disp
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
break;
case 6:// when count value is 6 show”6” on disp
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
break;
case 7:// when count value is 7 show”7” on disp
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
break;
case 8:// when count value is 8 show”8” on disp
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
break;
case 9:// when count value is 9 show”9” on disp
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
break;
} // switch
} // void, end of functions
}//switch-case ends
}//LED Display function end
//This function waits for button push and debounces button
boolean waitForButtonPush(boolean lastStartSwitchState)
{
//This function waits for a button push and also debounces it..
boolean currentStartSwitchState = digitalRead(StartButtonPin);
if(lastStartSwitchState != currentStartSwitchState) delay(20);
currentStartSwitchState = digitalRead(StartButtonPin);
return currentStartSwitchState;
}
void loop()
{
if(loopExit != 1)
{
Serial.println(\"Press button and continue pressing button as many times as you wish.\");
Serial.println(\"within a duration of 10 seconds: The total no of times pushed will be printed.\");
Serial.print(\'\ \'); //Newline
}
loopExit = 1; //Used as a flag for checking condition for displaying digits on the 7-segment LED
icount = 0;
LEDDisplay(10); //10 is chosen arbitrarily to force the default case in the switch-case structure
//which will turn the LED counter off when the button isn\'t pushed
startButtonCurrent = waitForButtonPush(startButtonLast); //This is a debouncing measure for the button when it
is pressed.
if(startButtonLast == LOW && startButtonCurrent == HIGH)
{
currentTimeStamp = millis(); // After button is pressed the current time the program has elapsed for
endTimeStamp = millis() + 10000; // is recorded with millis(), we know 10,000 ms = 10 seconds
while(currentTimeStamp < endTimeStamp)// While loop runs for 10 seconds according to the condition while
{ // also constantly checking for button presses and incrementing upon
detection.
startButtonCurrent = waitForButtonPush(startButtonLast);
if(startButtonLast == LOW && startButtonCurrent == HIGH)
{
;// Fill in missing line: Hint: Increment counter
Serial.println(\"Hit!..\");
}
startButtonLast = startButtonCurrent;//Re-assign current state to previous/last.
currentTimeStamp = millis(); // Updates elapsed time since while loop has started.
}//while
Serial.println(\"Stop!\");
Serial.print(\'\ \');
// Assumed that button cannot be hit more than 99 times..
tensValue = ;//Fill in missing parts: Hint: extract 10\'s digit from icount
onesValue = ;// Fill in missing parts: Hint: extract unit\'s digit from icount
while (loopExit == 1) // This while statement displays both digits for a 0-99
{ //count button push. it will display both digits then exit
Serial.print(\"Button was pushed \");
Serial.print(icount);
Serial.println(\" times.\");
Serial.print(\'\ \');
Serial.println(\"Waiting for 5 seconds...LOOK AT THE LED DISPLAY\ \");
delay(5000); // Wait for five seconds after stop message.
LEDDisplay(icount); //Fill in missing part: Hint: Display the ten’s digit on the LED//icount
delay(2000);
LEDDisplay(icount); //Fill in missing part: Hint: Display the unit’s digit on the LED//icount
delay(2000);
; // Fill in missing line: Hint: forces exiting the while loop, use correct variable
}//while
startButtonLast = startButtonCurrent; //current state becomes last state for new repetition of the function loop()
}//if
}//loop
Solution
There are many error in your program ,
Error :i bold your error
Error 1 missing } after steup()
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
Error 2:Two extra }} ,just remove them
// switch
} // void, end of functions
//}//switch-case ends
//}//LED Display function end
error 3:please comment the \'is pressed\' its not part of code
startButtonCurrent = waitForButtonPush(startButtonLast); //This is a debouncing measure for the button when it
is pressed.
error 4:please comment the \'detection.\' its not part of code
while(currentTimeStamp < endTimeStamp)// While loop runs for 10 seconds according to the condition while
{ // also constantly checking for button presses and incrementing upon
detection.
error: please give my some ,i have given values as 10,1 ,you can given your own according to program requirement
tensValue = ;//Fill in missing parts: Hint: extract 10\'s digit from icount
Program code:
int icount;
int StartButtonPin = 6;
int tensValue;
int onesValue;
unsigned long currentTimeStamp; //The millis() function
unsigned long endTimeStamp; //will return an unsigned long datatype so we remain consistent
boolean startButtonLast = LOW;
boolean startButtonCurrent = LOW;
boolean loopExit;
void setup()
{
Serial.begin(9600); // This line enables the serial monitor to be used for us to
pinMode(StartButtonPin, INPUT); // print the number of button presses for reading in the console.
//7-Segment LED Display
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void LEDDisplay(int count) //Can be re-used once you have this completed in the previous template.
{
switch(count) // Each case value also corresponds to the decimal value. Ex) Case 0 is decimal 0 and
//so on.
{
default:
// Display letter \'d\'..
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
case 0:
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
break;
case 1:
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
break;
case 2:// when count value is 2 show”2” on disp
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
break;
case 3:// when count value is 3 show ”3” on disp
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
break;
case 4:// when count value is 4 show”4” on disp
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
break;
case 5:// when count value is 5 show”5” on disp
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
break;
case 6:// when count value is 6 show”6” on disp
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
break;
case 7:// when count value is 7 show”7” on disp
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
break;
case 8:// when count value is 8 show”8” on disp
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
break;
case 9:// when count value is 9 show”9” on disp
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
break;
} // switch
} // void, end of functions
//}//switch-case ends
//}//LED Display function end
//This function waits for button push and debounces button
boolean waitForButtonPush(boolean lastStartSwitchState)
{
//This function waits for a button push and also debounces it..
boolean currentStartSwitchState = digitalRead(StartButtonPin);
if(lastStartSwitchState != currentStartSwitchState) delay(20);
currentStartSwitchState = digitalRead(StartButtonPin);
return currentStartSwitchState;
}
void loop()
{
if(loopExit != 1)
{
Serial.println(\"Press button and continue pressing button as many times as you wish.\");
Serial.println(\"within a duration of 10 seconds: The total no of times pushed will be printed.\");
Serial.print(\'\ \'); //Newline
}
loopExit = 1; //Used as a flag for checking condition for displaying digits on the 7-segment LED
icount = 0;
LEDDisplay(10); //10 is chosen arbitrarily to force the default case in the switch-case structure
//which will turn the LED counter off when the button isn\'t pushed
startButtonCurrent = waitForButtonPush(startButtonLast); //This is a debouncing measure for the button when it
//is pressed.
if(startButtonLast == LOW && startButtonCurrent == HIGH)
{
currentTimeStamp = millis(); // After button is pressed the current time the program has elapsed for
endTimeStamp = millis() + 10000; // is recorded with millis(), we know 10,000 ms = 10 seconds
while(currentTimeStamp < endTimeStamp)// While loop runs for 10 seconds according to the condition while
{ // also constantly checking for button presses and incrementing upon
//detection.
startButtonCurrent = waitForButtonPush(startButtonLast);
if(startButtonLast == LOW && startButtonCurrent == HIGH)
{
;// Fill in missing line: Hint: Increment counter
Serial.println(\"Hit!..\");
}
startButtonLast = startButtonCurrent;//Re-assign current state to previous/last.
currentTimeStamp = millis(); // Updates elapsed time since while loop has started.
}//while
Serial.println(\"Stop!\");
Serial.print(\'\ \');
// Assumed that button cannot be hit more than 99 times..
tensValue = 10;//Fill in missing parts: Hint: extract 10\'s digit from icount
onesValue =1 ;// Fill in missing parts: Hint: extract unit\'s digit from icount
while (loopExit == 1) // This while statement displays both digits for a 0-99
{ //count button push. it will display both digits then exit
Serial.print(\"Button was pushed \");
Serial.print(icount);
Serial.println(\" times.\");
Serial.print(\'\ \');
Serial.println(\"Waiting for 5 seconds...LOOK AT THE LED DISPLAY\ \");
delay(5000); // Wait for five seconds after stop message.
LEDDisplay(icount); //Fill in missing part: Hint: Display the ten’s digit on the LED//icount
delay(2000);
LEDDisplay(icount); //Fill in missing part: Hint: Display the unit’s digit on the LED//icount
delay(2000);
; // Fill in missing line: Hint: forces exiting the while loop, use correct variable
}//while
startButtonLast = startButtonCurrent; //current state becomes last state for new repetition of the function loop()
}//if
}//loop
onesValue = ;//












