hello everybody I wish everyone fine , how can I end if statement and move to another one ?
I will press the push button and if i = 7 , I want to end this statement and move to the second statement and start counting . this is my code
int i;
int x;
int buttonState = 0;
void setup()
{
pinMode(2, INPUT);
}
void loop()
{
buttonState = digitalRead(2);
if (buttonState == HIGH) {
i=i+1;
Serial.println(i);
}
if (i==7){
i==0;
goto nextvar;
}
nextvar:
if (buttonState == HIGH) {
x=x+1;
Serial.println(x);
}
}
buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.
a button press can be recognized by detecting a change in state and becoming LOW and may need to be debounced (e.g. delay (20)
You got the Serial communications working! That's good. Now, read about debounce and you will have a your program solved.
Here is your program with debounce (your button seems to be tied LOW reading HIGH when pressed).
int i;
int x;
int buttonState = 0;
int buttonPin = 2;
unsigned long timer, debounceTimeout = 50;
bool lastButtonRead, currentButtonState;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
}
void loop() {
// buttonState = digitalRead(2);
readButton();
// if ((buttonState == HIGH) && (i < 7)) {
// i = i + 1;
// Serial.print("i="); Serial.println(i);
// }
// if (i == 7) {
// if ((buttonState == HIGH) && (x < 7)) {
// x = x + 1;
// Serial.print("x="); Serial.println(x);
// }
// }
}
void readButton() {
bool currentButtonRead = digitalRead(buttonPin); // read button pin
if (currentButtonRead != lastButtonRead) { // if button pin reading changes...
timer = millis(); // ...start a timer
lastButtonRead = currentButtonRead; // ... and store current state
}
if ((millis() - timer) > debounceTimeout) { // if button read change was longer than debounceTimeout
if (currentButtonState == HIGH && lastButtonRead == LOW) { // ... and State NOT pressed while Button PRESSED
// your "i" and "x" counters
if (i < 7)
i_function(); // call function i();
if (i == 7)
x_function(); // call function x()
if (x == 7) { // start all over again
i = 0;
x = 0;
}
}
currentButtonState = currentButtonRead; // change the state
}
}
void i_function() {
if (i < 7)
i++;
Serial.print("i = "); Serial.println(i);
}
void x_function() {
if (x < 7)
x++;
Serial.print("x = "); Serial.println(x);
}
Task-1:
You have a push Button, everytime you press it, let a LED1 just flash. You keep pressing the BUtton for seven times to flash LED1 for seven times. Task-1 is done.
Task-2:
The task-2 is similar to above, but this time LED2 will flash for nine times when the Button is pressed for nine times.
1: Can you draw a Flow Chart to show the above-mentioned events?
.... pending
2. Convert the Flow Chart of Step-1 into executeable cods:
byte LED1 = 2; //LED1 is connected with DPin-2 witha 2.2k series resistor
byte LED2 = 3;
#define Button 4 //Button is connected with DPin-4 with internal pull-up resistor
byte counter;
void performTask(byte, byte); //forward declaration of user function
void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(Button, INPUT_PULLUP);
}
void loop()
{
performTask(7, LED1); //flash LED1 seven times against seven times Button press/release
performTask(9, LED2); //flash LED2 nine times against nine times Button press/release
}
void performTask(byte count, byte LED) //fash LED1/LED2 against Butt press/eease
{
do
{
while (digitalRead(Button) != LOW) //wit until Button is pressed
{
;
}
while (digitalRead(Button) != HIGH) //wait until Button is released
{
;
}
counter++; //count Button press/release
//---------------------------------
digitalWrite(LED, HIGH); //flash LED1/ED2
delay(100);
digitalWrite(LED, LOW);
}
while(counter != count); //until counter is 7 or 9
counter = 0; //reset counter for next cycle
}
3. Connect LED1 with DPin-2, LED2 with DPin-3, and Button with DPin-4 (with internal pull-up).
4. Upload sketch of Step-2 into Arduino UNO Board.
5. Press and release the Button, Check that LED1 flashes for seven times and then LED2 flashes for nine times.