LED sample

hello,

yesterday my first arduino nano arrived :slight_smile: so I just started playing with.
I´ve build some led´s with resistors and a button on to my experimental board.
When I push the button once, I want to set first led to high-status.
After another push I want to switch the next led into high-status and the first led into low-status.
I´ve not find out how to do that, why every led is just lightening very low?

Here is my code, I hope somebody could help?;

const int buttonPin = 2;

const int ledPin13 = 13;
const int ledPin12 = 12;
const int ledPin11 = 11;
const int ledPin10 = 10;
int merker=0;

int buttonState = 0;

void setup()
{
pinMode(ledPin13, OUTPUT);
pinMode(ledPin12, OUTPUT);
pinMode(ledPin11, OUTPUT);
pinMode(ledPin10, OUTPUT);

pinMode(buttonPin, INPUT);

Serial.begin(9600);
}

void loop()
{
buttonState = digitalRead(buttonPin);
//led13
if ((buttonState == HIGH)&&(merker==0))
{
digitalWrite(ledPin13, HIGH);
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin11, LOW);
digitalWrite(ledPin10, LOW);
merker=1;
delay(5000);

}

if ((buttonState == HIGH)&&(merker==1))
{
digitalWrite(ledPin12, HIGH);
digitalWrite(ledPin13, LOW);
digitalWrite(ledPin11, LOW);
digitalWrite(ledPin10, LOW);
merker=2;
}

if ((buttonState == HIGH)&&(merker==2))
{
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin13, LOW);
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin10, LOW);
merker=3;
}

if ((buttonState == HIGH)&&(merker==3))
{
digitalWrite(ledPin10, HIGH);
digitalWrite(ledPin13, LOW);
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin11, LOW);

merker=0;
}

Serial.print("Merker= ");
Serial.println(merker);
}

After the first five seconds, the code will race through the remaining states and turn off all but the LED on pin 13

Thank you for replay :slight_smile: but how can I fix that it will work?

Olga:
Thank you for replay :slight_smile: but how can I fix that it will work?

Check the state change example in the IDE

Thank you for this very helpful advice :slight_smile: how it´s possible to switch after one state-change also the output?
Is it possible to declarate he output with formulas Ledpin = i+9 and how can I begin from start again?
regards Olga

Hello again, I made an prog flow for better understanding what I want to know. I think there are a lot of ways to do that, how I can programm like the flow is show the correct state? Is it possible that someone show me a sample to do that?
Thank you

const int ledPin13 =  13;
const int ledPin12 =  12;
const int ledPin11 =  11;
const int ledPin10 =  10;

When you have two or more variables that are consecutively numbered it is often a good time to use an array. In this case:

const int LEDCount = 4;
const byte LEDPins[LEDCount] = {13, 12, 11, 10};

Then you can then use a ‘for’ loop to act on each pin:

for (int i = 0; i < LEDCount; i++) {
    digitalWrite(LEDPins[i], merker == i);  // The one in element 'merker' is on/true/HIGH, the others are off/false/LOW
}

HAllo Thank you very much, that are some really good advices. I´ve put that code inside my code but it´s actually not working. Can some one helb me please???
Thank you for your help.

const int LEDCount = 4;
const byte LEDPin[LEDCount] = {13, 12, 11, 10};

int merker=0;

int buttonState = 0;

void setup()
{
pinMode(LEDPin13, OUTPUT);
pinMode(LEDPin12, OUTPUT);
pinMode(LEDPin11, OUTPUT);
pinMode(LEDPin10, OUTPUT)
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}

void loop()
{

for (int i = 0; i < LEDCount; i++) {
digitalWrite(LEDPins*, merker == i); // The one in element ‘merker’ is on/true/HIGH, the others are off/false/LOW*

  • buttonState = digitalRead(buttonPin); *

  • if ((buttonState == HIGH)&&(merker==0))*

  • { *
    digitalWrite(LEDPins*, merker == i)*
    * }*

* if ((buttonState == HIGH)&&(merker==1)) *
* {*
digitalWrite(LEDPins*, merker == i)*
* }*

* if ((buttonState == HIGH)&&(merker==2))*
* { *
digitalWrite(LEDPins*, merker == i)*
* }*

* if ((buttonState == HIGH)&&(merker==3))*
* {*
digitalWrite(LEDPins*, merker == i)*
* } *
* Serial.print("Merker= ");*
* Serial.println(merker);*
}
}

pinMode(LEDPin13, OUTPUT);  Not compiling is not the same as.not working.

Hopefully after your last post, you can see why we ask you to use code tags when posting code.

Olga:
HAllo Thank you very much, that are some really good advices. I´ve put that code inside my code but it´s actually not working. Can some one helb me please???
Thank you for your help.

The sketch won’t compile. You took out the definition of the names LEDPin13,12,11,10 but still left them in the code. When you switch to an array you should use it everywhere, including setup().

const int LEDCount = 4;
const byte LEDPins[LEDCount] = {13, 12, 11, 10};
const byte ButtonPin = 2;
int merker = 0;
void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < LEDCount; i++) {
    digitalWrite(LEDPins[i], LOW);
    pinMode(LEDPins[i], OUTPUT);
  }
  pinMode(ButtonPin, INPUT_PULLUP);  // Connect button between pin and Ground.  LOW means pressed.
}
void loop()
{
  static boolean previousButtonState = HIGH;
  static unsigned long buttonChangeTime = 0;
  
  boolean buttonState = digitalRead(ButtonPin);
  // Look for, and debounce, button state changes
  if ((buttonState != previousButtonState) && millis() - buttonChangeTime > 10) {
    previousButtonState = buttonState;
    buttonChangeTime = millis();
    if (buttonState == LOW)  // Button pressed
      merker = (merker + 1) % LEDCount;
  }
  
  for (int i = 0; i < LEDCount; i++) {
    digitalWrite(LEDPins[i], merker == i);  // The one in element 'merker' is on/true/HIGH, the others are off/false/LOW
  }
}

:slight_smile:
WOW Thank you very much. It works very fine. Now I must understand this code. I hope I can ask again after think about that code :slight_smile:

Olga:
:slight_smile:
WOW Thank you very much. It works very fine. Now I must understand this code. I hope I can ask again after think about that code :slight_smile:

Yes, you may ask questions about the code.
Some things you need to remember for C++ programming:
The first element of an array is index 0.
The last element in an array with N elements is index N-1.
When talking about true/false values, ‘false’ evaluates as 0 and ‘true’ evaluates as 1. Any non-zero value is also ‘true’.
LOW is 0 and HIGH is 1 so you can use ‘false’ and ‘true’ values for LOW and HIGH.
% is the ‘modulo’ operator: the remainder after integer division.
‘if’, ‘for’, ‘while’… statements can contain only one statement. If you want to put more than one statement inside an ‘if’, etc you surround them in brackets (’{’ and ‘}’) to make multiple statements into one statement.

Hallo, thank you for your great explanation. What does "static boolean" and "static unsigned long" mean and why I have to use them in his way?

static boolean previousButtonState = HIGH;
static unsigned long buttonChangeTime = 0;

Some more questions in code:

boolean buttonState = digitalRead(ButtonPin); // the state at ButtonPin is now the state of buttonState
// Look for-> what is the meaning of this?, and debounce, button state changes
if ((buttonState != previousButtonState) && millis() - buttonChangeTime > 10) {
previousButtonState = buttonState; // if this is true then the program step into the next if condition and set merker
buttonChangeTime = millis();
if (buttonState == LOW) // Button pressed (equal to LOW) merker + 1 after

Thank you

Olga:
Hallo, thank you for your great explanation. What does "static boolean" and "static unsigned long" mean and why I have to use them in his way?

static (in this context) is used to make a variable persistent within its scope. given this example:

void doSomething(void)
{

  int value = 0;
  Serial.println(value);
  value++;
}

the function always prints zero.

If in some function you create a variable using static keyword, its value is retained even after the function ends.

void doSomething(void)
{

  static int value = 0;
  Serial.println(value);
  value++;
}

will print 0 then 1 then 2, etc...

Its scope is limited to doSomething()

there are other uses for the static keyword in C++

Olga:
Hallo, thank you for your great explanation. What does "static boolean" and "static unsigned long" mean and why I have to use them in his way?

Local variables (variables declared inside a function) are created fresh each time the function starts UNLESS they are marked 'static'. Then they act more like global variables which retain their value even when a function returns. Because you need to keep track of the previous state of the input and the time the state last changed you have to use the 'static' keyword. All of the Arduino examples make such variables global instead of static local but that makes the program harder to understand. Any global an be changed in any function but you can be sure that local variables can only be changed inside that one function.

Olga:
Some more questions in code:

 boolean buttonState = digitalRead(ButtonPin);

// Look for, and debounce, button state changes   (((what is the meaning of this?)))
 if ((buttonState != previousButtonState) && millis() - buttonChangeTime > 10) {
   previousButtonState = buttonState;
   buttonChangeTime = millis();

Mechanical button contacts tend to bounce when you close or open them. Instead of a clean HIGH to LOW change you will see HIGH to LOW to HIGH to LOW to HIGH to LOW change. The (buttonState != previousButtonState) part looks for a change in the button. The millis() - buttonChangeTime > 10 part ignores changes if it has been less than 10 milliseconds since the last change. That ignores the bounces which should last no more than a few milliseconds.