One push button & two variable

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);
   
}
}

thank you

Forget the goto command.
Rethink your logic (it’s pretty simple), and rework your code.

Also, you should debounce your switch reading, or you’ll blow through your seven count much faster than you want, especially as the switch gets older.

You are using Serial.print() so you must initialize your Serial communications with:

  Serial.begin(115200);

You will find the button gives you many (hundreds?) of pushes or none. Read about button "debounce."

https://docs.arduino.cc/built-in-examples/digital/Debounce/

By the way... you code works, but for the "Serial.begin" and "debounce."

[edit]
I missed this... you will want to replace this "i evaluates to 0"

    i == 0;

with this "i is assigned the value 0"

    i = 0;

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)

look this over
see pg 41 46 in The C Programming Language for what the ++ operator is

const byte PinBut = 2;
      byte buttonState;

const int Nstate = 3;
int state;

// -----------------------------------------------------------------------------
void
butPress ()
{
    if (Nstate <= ++state)
        state = 0;
    Serial.println (state);
}

// -----------------------------------------------------------------------------
void loop()
{
    byte but = digitalRead (PinBut);
    if (buttonState != but)  {      // state change
        buttonState = but;
        delay (20);                 // debounce

        if (LOW == but) {           // pressed
            butPress ();
        }
    }
}


// -----------------------------------------------------------------------------
void setup()
{
    Serial.begin (9600);

    pinMode(PinBut, INPUT_PULLUP);
    buttonState = digitalRead (PinBut);
}

thank you all for your replies , I came up with this solution

int i;
int x;
int buttonState = 0;

void setup() 
{
 
   Serial.begin(115200);
  pinMode(2, INPUT);

 
}


void loop() 
{ 

   buttonState = digitalRead(2);

 
    if ((buttonState == HIGH) && (i<7)) {
   i=i+1;
          Serial.println("i=");Serial.println(i);
    }
    if (i==7){

if ((buttonState == HIGH) && (x<7)) {
   x=x+1;
      Serial.println("x=");  Serial.println(x);
      
}
}
}

How does it work when you hold your finger on the button?

What is this experiment for? What is it that you are going for with this odd code? How will it fit with whatever is your real project goal?


Or page 46.

a7

I doubt it. ONE, short press gets (something like) this (without formatting):

i=1
i=2
i=3
i=4
i=5
i=6
i=7
x=1
x=2
x=3
x=4
x=5
x=6
x=7

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);
}

File for WOKWI.COM

diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -51.4,
      "left": 124.8,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": 14.4,
      "left": 172.25,
      "rotate": 90,
      "attrs": { "value": "10000" }
    }
  ],
  "connections": [
    [ "btn1:2.r", "r1:1", "green", [ "h0" ] ],
    [ "nano:GND.1", "r1:2", "black", [ "v14.4", "h57.1" ] ],
    [ "nano:5V", "btn1:1.r", "red", [ "v24", "h85.9", "v-124.8" ] ],
    [ "nano:2", "btn1:2.l", "green", [ "v0" ] ]
  ],
  "dependencies": {}
}

You have two tasks to accomplish -- correct?

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.