Code don't do what it should do

Hi,
I'm a really beginner with Arduino an I tried to make a Blinker.
So if I press Button1 the LED 1 shoud blink and if I press Button2 LED2 should blink.

my code:

const int buttonPinRight = 3;
const int buttonPinLeft =  10;
const int ledPinLeft = 8;
const int ledPinRight = 6;

int buttonStateRight = LOW;
int buttonStateLeft = LOW;
int ledStateLeft = LOW;
int ledStateRight = LOW;

long previousMillisRight = 0;
long intervalRight = 500;
long previousMillisLeft = 0;
long intervalLeft = 500;

unsigned long currentMillis;

void setup() {
  pinMode(ledPinLeft, OUTPUT);      
  pinMode(ledPinRight, OUTPUT); 

  pinMode(buttonPinRight, INPUT);     
  pinMode(buttonPinLeft, INPUT);  
  
  Serial.begin(9600);
}

void loop(){
  buttonStateRight = digitalRead(buttonPinRight);
  if (buttonStateRight == HIGH){
    if (millis() - previousMillisRight > intervalRight) {
      previousMillisRight = millis();   
      if (ledStateRight == LOW)
        ledStateRight = HIGH;
      else
        ledStateRight = LOW;
      digitalWrite(ledPinRight, ledStateRight);
    }
  }else{
    if (ledStateRight == HIGH){
      ledStateRight = LOW;
      digitalWrite(ledPinRight, ledStateRight);  
    }
  }
  
  // ...and the same with Left
  buttonStateLeft = digitalRead(buttonPinLeft);
  if (buttonStateLeft == HIGH){
    if (millis() - previousMillisLeft > intervalLeft) {
      previousMillisLeft = millis();   
      if (ledStateLeft == LOW)
        ledStateLeft = HIGH;
      else
        ledStateLeft = LOW;
      digitalWrite(ledPinLeft, ledStateLeft);
    }
  }else{
    if (ledStateLeft == HIGH){
      ledStateLeft = LOW;
      digitalWrite(ledPinLeft, ledStateLeft);  
    }
  }
}

It's also blink when the buttons are not pressed...

What is wrong?

Thanks for your help!

make sure to use { brackets after every if statement { }, and else { }

Looked at the code and couldnt find the problem right off.(been staring at this screen all day, cant think any more.) Did notice the missing {}'s. But I wrote this one that works. Obviously this is for only the right. Also you defined currentmillis as a variable and i dont see it used.

void loop(){
  buttonStateRight = digitalRead(buttonPinRight);
if (buttonStateRight == LOW){
  ledStateRight = LOW;
}
if (buttonStateRight == HIGH){
    if (millis() - previousMillisRight > intervalRight) {
      previousMillisRight = millis();   
      ledStateRight = !ledStateRight;  
  }  
  }
digitalWrite(ledPinRight, ledStateRight);
}

@TWAIN
Just to clarify, what Big Oil is referring to is this

      if (ledStateRight == LOW)
        ledStateRight = HIGH;
      else
        ledStateRight = LOW;

While {} are technically not needed here because there is only a single statement after the if and else what often happens is you add another line one day, eg

      if (ledStateRight == LOW)
        ledStateRight = HIGH;
        digitalWrite (1, HIGH);
      else
        ledStateRight = LOW;

which looks ok visually (this is called "reading the indentation") but of course doesn't work as intended.

So it's good practice to add the {} all the time. (It does look ugly though so I'll confess I often don't do it :))


Rob

Can you get a schematic... a real schematic and not the mickey mouse paint tool stuff???
I can't understand why you put two wires going to the LEDs... did they ever worked?

Try this code:

void setup() {
  pinMode(ledPinLeft, OUTPUT);      
  pinMode(ledPinRight, OUTPUT); 
  digitalWrite(ledPinRight, HIGH); //I can only assume you connected the Leds in a certain way... if they don't turn on after this, try with LOW instead of HIGH
  digitalWrite(ledPinLeft, HIGH);
}

Now, did you get any of the LEDs to turn on?

Also, did you verify the button outputs with a multimeter? Last time I saw, the Omron type of button you have on your sketch, had crossed contacts, and if this is the case, your software isn't getting any signal in.
So, see which pins make contact on your buttons, see if the LEDs turn on without reading any inputs with the code I posted. post the results.

if (ledStateRight == LOW)
        ledStateRight = HIGH;
        digitalWrite (1, HIGH);
      else
        ledStateRight = LOW;

@Graynomad
The compiler will get that one for you. ('else' without a previous 'if')
It won't, however, get:

if (ledStateRight == LOW)
        ledStateRight = HIGH;
      else
        ledStateRight = LOW;
        digitalWrite (1, HIGH);

So yes, always put in braces.

Yep, good point, written in haste.


Rob

Hi,
thanks for all the answers. I think I understood the point with the brakets.
I get a signal from the Buttons and the LED's are also blinking but the problem is that they are also blinking when the buttons aren't pressed.
I've so many wires to the LED's because of I've 4 LED's (2 and 2). On the picture above you can see the secound very bad.
I've got another picture but it's very confusing sorry. I made it with the programm fritzing. Perhaps I can make an better one on monday.

thanks for your help!

Looking at your schematic...

I think it's about time you clearly explain what you want to do so we can point you in the right direction towards getting a correct schematic... or learn electricity/electronics and understand what's the problem with your hardware.

Don't get me wrong, but there is so much wrong things in this drawing that explaining everything would take a lot of time.

Here is a fixed breadboard. I tried to leave it as you had it, did mkde it easier to see how the leds are wired. Hope it helps. (only needed wires were put on here)

Nothing beats a schematic...

Added comments to point out the error:

void loop(){
  buttonStateRight = digitalRead(buttonPinRight);
  if (buttonStateRight == HIGH)
    {
    //  The button is down
    if (millis() - previousMillisRight > intervalRight)
        {
        //  The button is down and it has been half a second since "previousMillis" was set
        previousMillisRight = millis();     // ?!?!  Set previousMillis only after the time has passed  ?!?
      if (ledStateRight == LOW)
        ledStateRight = HIGH;
      else
        ledStateRight = LOW;
      digitalWrite(ledPinRight, ledStateRight);
    }
  }else{
    //  The button is NOT pressed
    //  previousMillisRight = millis();     // THIS is where you want it.  You want it to be half a second since the button was NOT pressed

    if (ledStateRight == HIGH){
      ledStateRight = LOW;
      digitalWrite(ledPinRight, ledStateRight);  
    }
  }

!!!!!!!!!!!!!!! FINALLY !!!!!!!!!!!!!!!!!!!!
Finally found your issue, although your code could use some cleaning up it will work if you add a pull-down resister to you input. This has been a big headache lol. Because if I changed your code, not even very much, I could get it to work without the pull-down. I do not know why but your code requires it.

UPDATE
I figured out most the time when i changed the code i would use a debounce in it. So it ignored the noise you are having.

Kind of interesting, when testing your code on a bare uno, i could get the led to flash, although not very brightly, but at the interval set... Just by putting my finger over the input pin..