help change direction of leds

hello everybody,

i am programming on arduino uno R3 with rich shield component.
for my study i need to make that when i push the button 9 te direction will switch eacht time from led1 ,2,3 and 3,2,1

my code i will put downstart can someone help me , and show what i did wront or forgot :slight_smile:

const int buttonPin = 8; // the pin that the pushbutton is attached to
const int buttonPin1 = 9;
const int ledPin1 = 4; // the pin that the LED is attached to
const int ledPin2 = 5; // the pin that the LED is attached to
const int ledPin3 = 6; // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin1, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
bool forward = true ;
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {

if (buttonState == LOW) {
if (forward == true )
buttonPushCounter++;

} else {

}

delay(200);
} if (digitalRead(9) == LOW)
// if (forward == true )
buttonPushCounter--;
// switch (buttonState);
{

lastButtonState = buttonState;
}
if (buttonPushCounter % 3 == 0) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
} else {

}
if (buttonPushCounter % 3 == 1) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
} else {

} if (buttonPushCounter % 3 == 2) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);

} else {

}

}

i also have made a new program which works better for me

int buttonPushCounter = 0;
void setup() {
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
Serial.begin(9600);

}

void loop() {

bool forward = true ;

if (digitalRead(8) == LOW) {
delay(300);
if (forward == true )
{
buttonPushCounter++;

} else {

}

if (digitalRead(9) == LOW)
delay(300);
if (forward == true );

} else {
}
if (buttonPushCounter == 1) {
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);

} else if (buttonPushCounter == 2) {
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);

} else if (buttonPushCounter == 3) {
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);

} else {
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);

buttonPushCounter = 0;

}

}

Please post your code using code tags. Otherwise the formatting is messed up.

Read this before posting a programming question ...

Make sure unnecessary lines are removed and code is formatted for easy reading.

I'm not sure why your second version of the program uses the pin numbers rather than the constants but there are a number of issues with your code making it hard for me to know what you really want to do.

I assume you want the first button to light the next LED and you want the second button to change the direction that the LEDs are lit. Here are my suggestions:

  1. Check to see if the second button is pressed and change the sign of an integer which is set to 1 when the button is pressed. For example, if the integer is a 1 and the button is pressed make it a -1. If it is a -1 make it a 1.
  2. Add the integer in step one to a counter and use the modulo function to make sure the number is between 0 and 2.
  3. Light the LED corresponding to the counter

dear ToddL1962
true when i each time push second button (9 pinmode ) it has to change the dirrection of the leds normal wil be when i push hole time first button (pinmode 8 ) 1,2,3 when i push second button (pinmode 9) it has to go 3,2,1 . And when i push again (pinmode 9 ) 1,2,3 .

the problem is i can find the right variable to change it it tried many option like direction / swicht

Here is a version I did just for fun. There is a shorter way to do it but it involves arrays.

const int  buttonPin = 8;    // the pin that the pushbutton is attached to
const int  buttonPin1 = 9;
const int ledPin1 = 4;       // the pin that the LED is attached to
const int ledPin2 = 5;       // the pin that the LED is attached to
const int ledPin3 = 6;       // the pin that the LED is attached to


// Variables will change:
int counter = 0;   // counter for the number of button presses
int counterDir = 1;
byte lastButtonState;     // previous state of the button
byte lastButtonState1;     // previous state of the button


void setup() 
{
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin1, INPUT_PULLUP);
  lastButtonState = digitalRead(buttonPin);
  lastButtonState1 = digitalRead(buttonPin1);
  // initialize the LED as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() 
{
  byte buttonState = digitalRead(buttonPin1);
  if (buttonState != lastButtonState1) 
  {
    if (buttonState == LOW) 
    {
      counterDir = -counterDir;
    }
    lastButtonState1 = buttonState;
    delay(200);
  }

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) 
  {
    if (buttonState == LOW) 
    {
      counter += counterDir;
      if (counter < 0) counter = 2;
      if (counter > 2) counter = 0;
    }
    lastButtonState = buttonState;
    delay(200);
  }

  if (counter == 0) 
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
  }

  if (counter == 1) 
  {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
  }

  if (counter == 2) 
  {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, HIGH);
  }
}

dearToddL1962

thank you for the example now i want to make this whitout arrays is it possible
because i didnt made it yet to arrays . for now i only knew basic variables , i want to understand it i a student way

i undestand that you can make it whit a buttonPushCounter++; and buttonPushCounter--; i cant get in my
void loop to generate button 9 to reverse the direction because i stuck.

opelvectra:
dearToddL1962

thank you for the example now i want to make this whitout arrays is it possible
because i didnt made it yet to arrays . for now i only knew basic variables , i want to understand it i a student way

i undestand that you can make it whit a buttonPushCounter++; and buttonPushCounter--; i cant get in my
void loop to generate button 9 to reverse the direction because i stuck.

The example I gave you does not use arrays.

opelvectra:
i undestand that you can make it whit a buttonPushCounter++; and buttonPushCounter--; i cant get in my
void loop to generate button 9 to reverse the direction because i stuck.

Here is version with counter++ and counter--:

const int  buttonPin = 8;    // the pin that the pushbutton is attached to
const int  buttonPin1 = 9;
const int ledPin1 = 4;       // the pin that the LED is attached to
const int ledPin2 = 5;       // the pin that the LED is attached to
const int ledPin3 = 6;       // the pin that the LED is attached to


// Variables will change:
int counter = 0;   // counter for the number of button presses
bool counterDir = true;
byte lastButtonState;     // previous state of the button
byte lastButtonState1;     // previous state of the button


void setup()
{
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin1, INPUT_PULLUP);
  lastButtonState = digitalRead(buttonPin);
  lastButtonState1 = digitalRead(buttonPin1);
  // initialize the LED as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop()
{
  byte buttonState = digitalRead(buttonPin1);
  if (buttonState != lastButtonState1)
  {
    if (buttonState == LOW)
    {
      counterDir = !counterDir;
    }
    lastButtonState1 = buttonState;
    delay(200);
  }

  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    if (buttonState == LOW)
    {
      if (counterDir == true)
      {
	counter++;
      }
      else
      {
        counter--;
      }
      if (counter < 0) counter = 2;
      if (counter > 2) counter = 0;
    }
    lastButtonState = buttonState;
    delay(200);
  }

  if (counter == 0)
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
  }

  if (counter == 1)
  {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
  }

  if (counter == 2)
  {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, HIGH);
  }
}

dear ToddL1962

thank you , i tried to make it in a flowchart to understand the step you made . can you rename counterDir to another name like counterstep ? or wil it lose his function .

opelvectra:
dear ToddL1962

thank you , i tried to make it in a flowchart to understand the step you made . can you rename counterDir to another name like counterstep ? or wil it lose his function .

You should be able to edit the code yourself.

ToddL1962

i tried to reprogram your code (it didnt work ) that he save the direction which he is like button 8 each click goes to led button 9 swicht direction possible i need to make a variable to save the direction maybe dont use a counter ?

for me it hard to understand lastbuttonstate buttonstate as a begginer

opelvectra:
ToddL1962

i tried to reprogram your code (it didnt work ) that he save the direction which he is like button 8 each click goes to led button 9 swicht direction possible i need to make a variable to save the direction maybe dont use a counter ?

for me it hard to understand lastbuttonstate buttonstate as a begginer

I don't understand what you want. The variable 'counterDir' is a boolean which tells you the direction. When counterDir is true the counter increments, when counterDir is false the counter decrements. You can rename any of the variables to whatever you want.

buttonState != lastButtonState1) Is it possible to change to something different to make it shorter i need to understand read buttonstate

opelvectra:
buttonState != lastButtonState1) Is it possible to change to something different to make it shorter i need to understand read buttonstate

Make what shorter? The variable name? I honestly don't understand what you need. The code is pretty easy to understand.

i translated the question :add another button and make sure to include it
pressing this button will reverse the direction of the LED's lighting up each time.
After the first press of the "reverse" button, the sequence becomes 3 - 2 - 1 - 3 - 2 etc.
After another press on the "reverse" button, the sequence is 1 - 2 - 3 - 1 - 2, etc.
Tip:
Use an extra variable in which you store the direction.

button 8 and 9 button 9 is for reverse

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.