Home > Blockchain >  I don't know how to use state function yet in arduino and this is my first little project
I don't know how to use state function yet in arduino and this is my first little project

Time:03-25

const int buttonPin =  4;     // the port number of the pushbutton pin

const int green    = 13;      // the port number of the LED pin
const int red      = 12;
const int blue     = 11;

void setup() {
  pinMode(green, OUTPUT);   // initialize the LED pin as an output:
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}
void loop()
{

int state = digitalRead(buttonPin); // to check if it's on or not

if (state == 1)
{
  
  digitalWrite(green, HIGH); //LED GREEN ON
  digitalWrite(red, LOW); // LED RED OFF
  digitalWrite(blue, LOW); //LED BLUE OFF
  delay(10000);
  
}

if(state == 2)
{
  
  
  digitalWrite(green, LOW); //LED GREEN OFF
  digitalWrite(red, HIGH); // LED RED ON
  digitalWrite(blue, LOW); //LED BLUE OFF
  delay(10000);

}
if(state == 3)
{
  
  
  digitalWrite(green, LOW); //LED GREEN OFF
  digitalWrite(red, LOW); // LED RED OFF
  digitalWrite(blue, HIGH); //LED BLUE ON
  delay(10000);

}
else
{
  
  
  digitalWrite(green, LOW); //LED GREEN OFF
  digitalWrite(red, LOW); // LED RED OFF
  digitalWrite(blue, LOW); //LED BLUE OFF

}
}

I encoded this problem and decided to put state function so that it will store a number when i press the button to light up the leds. but after pushing the button the only led that lights up are led green and red and i don't even know why they are turning on at the same time

CodePudding user response:

You have multiple if() after each other instead of using else if.

The else only relates to if(state == 3) which will always be false since state can only be 0 or 1 in this case. So the else will always run, together with the if(state == 1) when the button is pressed.

Change all but the first if(...) to else if(...), then it will probably make more sense.

CodePudding user response:

Reading digital input will only give you two states HIGH or LOW, wich will be "translated" as 1 (HIGH) or 0 (LOW). I really don't understand why the red LED lights, I think it should not.

For your purpose, you shoul add a "counter" variable in your code, wich will increment every time you press the button, and use this counter variable in the if statements. I also recommend you to set this variable to 0 when it reaches some value, or your LEDs will always be off at certain point, and you will have to restart the Arduino to turn them on again.

You should also care about debouncing the button, and the way your delays are called. As you used, it will totally stop your code when it reaches the delay function.

Also, using "else" statement, is a good practice.

const int buttonPin =  4;     // the port number of the pushbutton pin

const int green    = 13;      // the port number of the LED pin
const int red      = 12;
const int blue     = 11;

void setup() {
pinMode(green, OUTPUT);   // initialize the LED pin as an output:
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
int counter = 0;
}
void loop()
{
    
    int state = digitalRead(buttonPin); // to check if it's on or not

    if(state == 1) {            // if(state) also works
        counter = counter   1;  // counter  
    }

    if (counter == 1)
    {
    
    digitalWrite(green, HIGH); //LED GREEN ON
    digitalWrite(red, LOW); // LED RED OFF
    digitalWrite(blue, LOW); //LED BLUE OFF
    delay(10000);
    
    }

    else if(counter == 2)
    {
    
    
    digitalWrite(green, LOW); //LED GREEN OFF
    digitalWrite(red, HIGH); // LED RED ON
    digitalWrite(blue, LOW); //LED BLUE OFF
    delay(10000);

    }
    else if(counter == 3)
    {
    
    
    digitalWrite(green, LOW); //LED GREEN OFF
    digitalWrite(red, LOW); // LED RED OFF
    digitalWrite(blue, HIGH); //LED BLUE ON
    delay(10000);

    }
    else if (counter > 3)
    {
    
    
    digitalWrite(green, LOW); //LED GREEN OFF
    digitalWrite(red, LOW); // LED RED OFF
    digitalWrite(blue, LOW); //LED BLUE OFF

    }
    else if (counter == 10){    //so it resets at some point
        counter = 0;
    }
}
  • Related