Newbie on Arduino trying to understand it´s code. NEED HELP!

Hello there.
I´m currently working for the first time on Arduino. My professor, tasked us to create a project in which we use the following elements:

  • 2 LED´S
    -1 pushbutton
    -Resistors

The problem is that he wants us to activate the pushbutton and, with it´s activation, the first LED turns on, after some time, the first one turns off and the second one turns on, and so on. Once we activate the pushbutton once again, depending on which LED is on, it´ll inmediatly turn on the other LED and turn off the current one.

This is the code I´m currently working on, and I know its incomplete, thats why I ask for your help:

//Code
int Blue = 9;
int Red = 7;
int Button = 13; 
int ButtonState = 0; 

void setup() {

 pinMode(Button, INPUT);
 pinMode(Blue, OUTPUT);
 pinMode(Red, OUTPUT);
}

void loop() {

 ButtonState = digitalRead(Button);
 if((digitalRead(ButtonState) == HIGH)) {

 
   digitalWrite(Blue, HIGH);
   digitalWrite(Red, LOW);
   delay (3500); 
   digitalWrite(Blue, LOW);
   digitalWrite(Red, HIGH);
   delay(3500);
 }
 else {
 
   digitalWrite(Blue, LOW);
   digitalWrite(Red, HIGH);
   delay(3500);
   digitalWrite(Blue, HIGH);
   digitalWrite(Red, LOW);
   delay(3500);
 }
   
 }

Any tip on how I can solve it?

After reading the "How to use this forum" post, please edit your code to add code tags.

We strong recommend against using delay(), clearly explained in this tutorial.

Solve what? Explain the problem you are having.

Hi Axel,

reacting immediately on a push-button is the classical thing to program without delay().
Delay() does what its name says. Delay execution of code and additional and sadly delay new programmers progress in learning to program.

You need another programming technique called non-blocking timing. This technique is based on the function millis().
I recommend watching and reading this tutorial

arduino-sketch-with-millis-instead-of-delay/

best regards Stefan

the basic thing is have a loop that is running through fast
and because of this can react fast on a button-press.

Another part of the code is "watching time passing by" through comparing actual time with a start-time
caclulating the difference between actual time and start-time. If this difference reached a certain size
execute the "timed action"

In your case switching the LEDs ON/OFF

If I understand right whenever LED1 is ON LED2 is OFF and vice versa.

then adding a button-press shall start the LED-blinking

then adding a button-press shall abort the actual blink-cycle and start the next one.
For this function I recommend a pretty slow blinking on LED 4-5 seconds on

best regards Stefan

jremington:
After reading the "How to use this forum" post, please edit your code to add code tags.

We strong recommend against using delay(), clearly explained in this tutorial.

Solve what? Explain the problem you are having.

Sorry, I´ll make sure to read that article.
The problem is that my program currently only activates the LED´S sequence on it´s own, without me activating the pushbutton. What I need it to do is:

  1. Activate the pushbutton and the sequence starts (Blue LED on/Red LED off and viceversa.
  2. Activate once again the pushbutton, the current LED that is on will turn off and the one that was off needs to turn on and once again start the sequence.
    And I´ll make sure to look into the tutorial, I´ve seen on other comments that its preferable to use millis() than delay().
    Thank You
    -Axel

The problem is that my program currently only activates the LED´S sequence on it´s own, without me activating the pushbutton.

 ButtonState = digitalRead(Button);
  if((digitalRead(ButtonState) == HIGH)) {

You activate the sequence based on a reading of pin 0 or pin 1. I'm sure you don't intend to do that.

Wrong format:

if((digitalRead(ButtonState) == HIGH))

This should look like :
if(digitalRead(ButtonState == HIGH))

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

It looks like you have your button is between 5V and the Arduino input pin.

You need a 10K resistor from that input pin the gnd, so that the input pin is at gnd when the button is not pushed.

At the moment you have pushed the button and when you release it the input pin is open circuit or floating.
So it has possibly stored the charge you put on it when the button was pushed and indicates that it is still pressed.

Thanks... Tom... :slight_smile:

Hi ALex,
here is a code-version which has exactly the same lines as your code posted in post #1

but with additional serial output that visualises what is going on in your code
I changed the delaytime to 5000 to make it easier to see how much time passes by
which can be seen in the timestamps

switch on the serial monitor-window by pressing Shift-Ctrl-M, make sure show time stamp is activated
and adjust baudrate to 9600

Then watch the serial output which makes visible what your program is doing

Feel free to ask whatever you want to ask

//Code
int Blue = 9;
int Red = 7;
int Button = 13;
int ButtonState = 0;

void setup() {
  Serial.begin(9600);            // start serial connection at 9600 baud
  Serial.println("Setup Start");
  pinMode(Button, INPUT);
  pinMode(Blue, OUTPUT);
  pinMode(Red, OUTPUT);
}

void loop() {

      Serial.println();                            // print new line
      Serial.println();                            // print new line
      Serial.println("Top of loop");                            // print new line

  ButtonState = digitalRead(Button);

      
      Serial.print("ButtonState = digitalRead(");  // print fixed part of the text
      Serial.print(Button);                        // print value of variable button
      Serial.print(")   ButtonState has value ");  // print fixed text
      Serial.print(ButtonState);                   // print value of variable ButtonState
      Serial.println();                            // print new line
    
      Serial.print("if ((digitalRead(");           // print fixed text
      Serial.print(ButtonState);                   // print value of variable ButtonState 
      Serial.print(") == HIGH))");                 // print fixed text
      Serial.println();                            // print new line
    
      Serial.print("which means you are reading IO-pin number ");
      Serial.print(ButtonState);
      Serial.print(" But your Button is connected to IO-Pin ");
      Serial.print(Button);
      Serial.println();
  
  if ((digitalRead(ButtonState) == HIGH)) {
        Serial.println("if ((digitalRead(ButtonState) == HIGH)) is TRUE");
    
    digitalWrite(Blue, HIGH);
    digitalWrite(Red, LOW);
        Serial.println("TRUE now start first delay");
    delay (5000);
        Serial.println("TRUE first delay over");
    digitalWrite(Blue, LOW);
    digitalWrite(Red, HIGH);
        Serial.println();                            // print new line
        Serial.println("TRUE now start second delay");
    delay(5000);
        Serial.println("TRUE second delay over");
  }
  else {
        Serial.println("if ((digitalRead(ButtonState) == HIGH)) is false");
    digitalWrite(Blue, LOW);
    digitalWrite(Red, HIGH);
        Serial.println("false now start first delay");
    delay (5000);
        Serial.println("false first delay over");
    digitalWrite(Blue, HIGH);
    digitalWrite(Red, LOW);
        Serial.println();                            // print new line
        Serial.println("false now start second delay");
    delay(5000);
        Serial.println("false second delay over");
  }
}

best regards Stefan

74younus:
Wrong format:

if((digitalRead(ButtonState) == HIGH))

This should look like :
if(digitalRead(ButtonState == HIGH))

Sorry, but NO. The first version is OK, the second version is WRONG

The first version could be simplified like this

if (digitalRead(ButtonState) == HIGH)

...R