Beginner ;-)

It was my intention to create a loop within a loop ... but it seems that my approach is not correct.

Can someone show me how you should handle this please ?

My target was to get a different frequency for both red Led's (Project 2 from the Arduino Projects book)

Thanks
Karel

Moving to Programming Questions

charbr0:
It was my intention to create a loop within a loop ... but it seems that my approach is not correct.

Pleas post your program code here.

If you are getting compiler errors please also post the error message.

If the program compiles OK but does not do what you want then tell us exactly what it actually does and what you want it to do that is different.

See How to use the Forum

,,,R

It's not a login. It just wants you to enter "first name and last name" in the box.

The most obvious mistake is reading from an output pin.

Many thanks to MorganS for a start.

Others...OMG if you guys dont want to help anyone who posts legitimate Arduino things that's fine nobody has a knife to your throat.

But if its just to winge I will dump those posts.

/* Praktijk-oefening volgens het "Arduino Projects Book"
   "Spaceship Interface" p33
*/

// Create a variable: declare the type
// the data type int is a whole number (an integer)
// The declaration of the variable, as everey statement, must end with a semicolon(;)

int buttonSignal=0;   // = the state of the switch is open
int redFlash=0        // # flashing times of red Led (pin 5)

void setup() {
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(2,OUTPUT);
}

void loop() {
   buttonSignal=digitalRead(2);
   // This is a comment
   
   if (buttonSignal == LOW) {    // if the button is not pressed
   
   digitalWrite(3, HIGH);  // green Led
   digitalWrite(4, LOW);   // red Led
   digitalWrite(5, LOW);   // red Led
   }
   else {  // the button is pressed
   
   digitalWrite(3, LOW);  // green Led
   digitalWrite(4, LOW);   // red Led
   }
      if redFlash <10 {
      digitalWrite(5, High);
      delay(10);
      RedFlash +1
      digitalWrite(5, Low);
      }
      else {

   // toggle the LED's
   
   digitalWrite(4, HIGH);   // red Led
   digitalWrite(5, LOW);   // red Led
   delay(250);  // wait for a quarter of a second
   }
}  // go back to the beginning of the loop

There are several syntax problems:

Line 10: int redFlash=0
semicolon missing
should be: int redFlash=0;

Line 34: if redFlash <10 {
round brackets missing
should be: if (redFlash < 10) {

Line 35: digitalWrite(5, High);
C++ (the Arduino programming language) is case sensitive
"HIGH" is not the same as "High"
should be: digitalWrite(5, HIGH);

Line 37: RedFlash + 1
variables are case sensitive
wrong syntax, missing semicolon
should be: redFlash++;
or: redFlash = redFlash + 1;

Line 38;     digitalWrite(5, Low);
case sensitive...
should be: digitalWrite(5, LOW);

WOW ... thank you all, in particular uxomm :smiley: ..... I made a lot of stupid mistakes in this little exercise !

The code is verified now but the program is still not working as intended:
After the upload the green led and one red led is HIGH
When I push the button the green led goes LOW (te red LED stays HIGH) ... No Flashing ... ;-(

I can assure you: I will learn from my mistakes ... Thanks again, it felt good to notice that my simple message received so much attention.
Best regards

As you did not describe what you want to achive (at least it is not clear to me), it is not easy to help.
Your code is quite different to the code from the Arduino Projects Book.
This is what I notice:
If you want to get some blinking that is visible to the human eye, an interval of 10 milliseconds may be too short, try 50 milliseconds or longer.
Variable "redFlash" is never reset (set back to zero). So it will only work (blink) once.

I've made a few changes in your code, so it does some blinking, when the button is pressed. As said before, I do not know what you want to achive, but it might be some inspiration:

/* Praktijk-oefening volgens het "Arduino Projects Book"
   "Spaceship Interface" p33
*/

// Create a variable: declare the type
// the data type int is a whole number (an integer)
// The declaration of the variable, as everey statement, must end with a semicolon(;)

int buttonSignal = 0; // = the state of the switch is open
int redFlash = 0;     // # flashing times of red Led (pin 5)

void setup() {
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  // pinMode(2, OUTPUT);   // Should not be and output but an input
  pinMode(2, INPUT);
}

void loop() {
  buttonSignal = digitalRead(2);
  
  if (buttonSignal == LOW) {    // if the button is not pressed
    digitalWrite(3, HIGH);  // green Led
    digitalWrite(4, LOW);   // red Led
    digitalWrite(5, LOW);   // red Led
    redFlash = 0;        // added this line
  }
  else {  // the button is pressed
    digitalWrite(3, LOW);  // green Led
    digitalWrite(4, LOW);   // red Led

    if (redFlash < 10) {
      digitalWrite(5, HIGH);
      delay(50);                // was 10 (milliseconds). may be too short as blink interval. try 50
      redFlash = redFlash + 1;
      digitalWrite(5, LOW);
      delay(50);               // was missing
    }
    else {
      // toggle the LED's
      digitalWrite(4, HIGH);   // red Led
      digitalWrite(5, LOW);   // red Led
      delay(250);  // wait for a quarter of a second
    }
  }  // end: the button is pressed
}  // end: loop

THX @ uxomm

There is progress but it's clear I need to explain my target:

  1. I started with the code from the Arduino Projects Book p36: nice and working
    Arduino Cloud

  2. Both red leds are flashing alternatively and with the same frequency
    I wondered if I could change the code to get the same result: when the button is pressed > green led LOW
    (dimmed) and red leds flashing BUT red Led(pin5) flashing at a different frequency's vs red Led(pin4)

  3. To get this result I tried to insert a second loop for red led's (pin5) // not yet successful :smiley:

What do we get now:

  • When the button signal is LOW > the Green led is HIGH and red Led(pin5) is flashing
  • When the button signal is HIGH (button is pressed) > red Led(pin4) is HIGH (not flashing)
    and no alternative flashing of both red Led's ...

So ... it will need some extra changes of the code.
BTW I owe you a beer for the help you already provided !! :wink:

!!
From the suggested adaptions, the change of "// pinMode(2, OUTPUT); // Should not be and output but an input" was weird to me: it was a change of the original code from the Arduino Projects Book p36 ?

Regards
K