Din't do what i want.

I was a newby in this arduino world and want to learn more from this.
Was making a board with 2 leds and let them blink, this was easy for an newby.
Then i was making a sort of an program to switch between the 2 leds to blink with in switch button, that was some more dificult for me!

I want the red led to blink endlessly till the switch is puched, then the green led is blinking endlessly till the switch button is pushed again. Then start again with the same program loop from the beginning.

So this was the library, now itt just blinks for times and then stops, this needs to be endlessly.

const int ledRood =8;
const int ledGroen =9;
const int knop = 2;

int knopStatus;
int a ;

void setup() {

pinMode ( ledRood , OUTPUT);
pinMode ( ledGroen, OUTPUT);

pinMode ( knop , INPUT);
}

void loop() {

knopStatus = digitalRead ( knop );

if ( knopStatus == HIGH && a ==0){
digitalWrite ( ledGroen, LOW);
digitalWrite ( ledRood, HIGH);
delay (500);
digitalWrite ( ledRood, LOW);
delay (500);
digitalWrite ( ledRood, HIGH);
delay (500);
digitalWrite ( ledRood, LOW);
delay (500);
digitalWrite ( ledRood, HIGH);
delay (500);
digitalWrite ( ledRood, LOW);
delay (500);
digitalWrite ( ledRood, HIGH);
delay (500);
digitalWrite ( ledRood, LOW);
delay (500);

a =1;

delay(500);
}

knopStatus = digitalRead ( knop );

if ( knopStatus == HIGH && a ==1){

digitalWrite ( ledGroen , HIGH);
delay (500);
digitalWrite ( ledGroen , LOW);
delay (500);
digitalWrite ( ledGroen , HIGH);
delay (500);
digitalWrite ( ledGroen , LOW);
delay (500);
digitalWrite ( ledGroen , HIGH);
delay (500);
digitalWrite ( ledGroen , LOW);
delay (500);
digitalWrite ( ledGroen , HIGH);
delay (500);
digitalWrite ( ledGroen , LOW);
delay (500);
digitalWrite ( ledRood, LOW);
a=0;

delay(500);
}
}

Switch_program_blink_green_and_red_led.ino (1.16 KB)

Read "How To Use This Forum"

in particular, 7. If you are posting code or error messages, use "code" tags

This is what happens when you do not

if you have a module that is not working, do not say "A GPS" or "the fingerprint sensor". Show us a link to the particular sensor, and a link to the datasheet if available

If the code didn't do what you expected, it must have done something else. You failed to tell us what it actually does.

Variable names should reflect the purpose of the variable. You want the red LED blinking, or you don't (in which case the green one should be). a does NOT indicate that that is the purpose of that variable. redBlinking, as a boolean, does.

How is your switch wired? Why are you not using the internal pullup resistor with its simpler wiring?

You realize that you will need to hold the switch down for up to 4 seconds in order for the Arduino to know that you pushed it, right?

I want the program too let the red LED blink for a long time(as long as i want too do) the switching should be when you push an external pullup switch to an green LED to blink for a long time (as long as i want too do). When you push the external pullup switch the red LED blinks again and starts the program again.

For now the library is that the red LED blinks four times and then stops. When pushed the external pullup switch the green LED blinks four times and stop again. Push the pullup switch again and the program starts again like explained.

For now the library is that the red LED blinks four times and then stops. When pushed the external pullup switch the green LED blinks four times and stop again. Push the pullup switch again and the program starts again like explained.

So, the code is working exactly like you wrote it.

Suppose that loop() looked something like:

void loop()
{
   byte currVal = digitalRead(somePin);
   if(currVal != prevVal)
   {
      // The switch was just pressed or was just released
      if(currVal == HIGH) // assuming HIGH means pressed; change as necessary
      {
         redBlinking = !redBlinking;
      }
   }
   prevVal = currVal;

   if(redBlinking)
      blink(redPin);
   else
      blink(greenPin);
}

The switch simply determines which LED pin should be being toggled. Actually toggling the correct pin is handled by another function, and happens regardless of the state of the switch.

Write the blink function as shown in the Blink Without Delay example.

If this feels like I'm pushing you into the pool, at the deep end, get over it.

marcello_b:
the red LED blinks four times and then stops. When pushed the external pullup switch the green LED blinks four times and stop again. Push the pullup switch again and the program starts again like explained.

That is exactly what you told it to do.

int CurrentColor = 0; // RED
void loop() 
{
  if (the button is pressed and CurrentColor==0)
  {
    Blink RED four times;
    CurrentColor = 1;  // GREEN
  }
  if (the button is pressed and CurrentColor==1)
  {
    Blink GREEN four times;
    CurrentColor = 0;  // RED
  }
}

While the LED is blinking your sketch is mostly doing nothing. It certainly isn't checking to see if the color should change. Once the red blinking is done you have set the color to green so the next time the button is pressed the green LED will do the blinking.

Now would be a good time to read about millis(), and Blink Without Delay, followed by reading up on simple ‘State Machines’.

Both these skillsets will help you enormously in this project and future programming

As noted above, check your switch wiring, and learn about detecting WHEN the button is pressed, not IF the button is pressed… i.e. state change.

Good luck, we were all beginners once.

I always think of the loop() function as being like a goldfish. It has no 'memory' of anything unless you explicitly write it down, and the only question in life that it has is "what should I be doing right now?"

There's only four things your sketch does - turn the red led on, turn the red led off, turn the green led on, tun the green led off. And there's only two things that tell it what to do: whether or not that button is pressed, and the progress of time.

There's a subtlety there: we don't want to be switching from red to green while the button is pressed, we want to switch when the button changes from being not pressed to being pressed.

One way to do the progress of time is by using delay(), but the way that we all eventually have to learn is by using millis().