-1
int ledy = 5 ;
int leds = 4 ;
int ledk = 3;
int button = 2 ;
int count = 0 ;
  
void setup()
{
  pinMode(ledy, OUTPUT);
  pinMode(leds, OUTPUT);
  pinMode(ledk, OUTPUT);
  pinMode(button,INPUT);
  randomSeed(analogRead(A0));
}

void loop()
{
  if(digitalRead(button)== HIGH)
  {
  count = count++ ;
  }
    if (count==1)
  {
    digitalWrite(ledk, HIGH);
    digitalWrite(leds, LOW);
    digitalWrite(ledy, LOW);
      
   }
    else if (count==2)
   {
    digitalWrite(ledk, LOW);
    digitalWrite(leds, HIGH);
    digitalWrite(ledy, LOW);
     
    }
    
   else if (count==3)
   {
    digitalWrite(ledk, LOW);
    digitalWrite(leds, LOW);
    digitalWrite(ledy, HIGH);
   }
   
     delay(1000);
  

}

I don't undertstand why it isn't working. I want it three leds to light up one by one as loop. But none of them lights up.

6
  • 2
    Please tell us what you want to achieve and what happens instead. Commented Jan 29, 2023 at 17:47
  • 1
    You also could use the serial port to print some messages to the arduino serial monitor to see if the button press is detected and the right if-condition gets triggered. Also show your circuit - there could also be something wrong. Depending on how your button is wired a pull-down resistor might be needed. If the button pulls the pin to GND you could use pinMode(button,INPUT_PULLUP); and then need to test for digitalRead(button)== LOW... Commented Jan 29, 2023 at 18:07
  • 2
    @gruesome, please add that information to your question. This is probably caused by count = count++ which (I guess) does not do what you think it does. Replace it with either count++ or count = count + 1. It might also be a good idea to reset the counter after it reaches 3. Commented Jan 29, 2023 at 18:09
  • 1
    @gruesome Please "edit" your original question and add additional information there and not to the comments. Commented Jan 29, 2023 at 18:13
  • 2
    forget your code for now ... write a simple sketch that lights one of the LEDs for one second, turns off LED for one second, and repeats ... test all three LEDs this way ... update your question with results of the test Commented Jan 29, 2023 at 19:20

1 Answer 1

0

One thing that jumps out to me as a possible cause of your issue here is that many push buttons require a pull-up resistor, which is provided as an internal component of most microcontrollers, but needs to be specifically requested in the pinMode instantiation of the button. Try changing your setup() function to the following:

void setup()
{
  pinMode(ledy, OUTPUT);
  pinMode(leds, OUTPUT);
  pinMode(ledk, OUTPUT);
  pinMode(button,INPUT_PULLUP);
  randomSeed(analogRead(A0));
}

however if you do this, you must also change your if statement to if(digitalRead(button) == LOW), as a pullup resistor will make it so that when the button is not pressed, the pin is pulled high, and vice-versa.

Another way to troubleshoot this is to see if the LEDs light up when you remove the reliance on the button altogether by removing the

if(digitalRead(button)== HIGH)
  {
  count = count++ ;
  }

and changing it to

count++;
delay(1000);

and see if the LED changes every 1 second (delay requires a millisecond value). I agree with @StarCat that count = count++; possibly isn't doing what you think it is.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.