I want some advice


This is my board.
In the initial state, all LEDs are off, and when you press the button associated with number 3, the left switch (turning on the 4th LED, turning off the 4th LED and turning on the 5th LED, turning off the 5th LED and turning on the 6th LED, turning off the 6th LED and turning on the 7th LED, turning off the 7th LED and turning on the 4th LED again...) repeats. In this state, when you press the button associated with number 2, I would like the right switch to proceed from the last lit LED in the left switch. For example, if the last lit LED in the left switch state before pressing the button 2 is number 6, then the 6th LED is turned off, the 5th LED is turned on, the 5th LED is turned off, the 4th LED is turned on, and the 4th LED is turned off, and the 7th LED is turned on.

  pinMode(3, INPUT);
  pinMode(2, INPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
}

int previous_sw1 = 0;
int current_sw1;
int previous_sw2 = 0;
int current_sw2;
int direction = 1;
int findLastLED(){
  for (int i = 7; i >= 4, i--){
    if (digitalRead(i) == HIGH){
      return i;
    }
  }
}

void loop() {
  current_sw1 = digitalRead(3);
  current_sw2 = digitalRead(2);

  if (previous_sw1 == 1 && current_sw1 == 0) {
    direction += 1;
  }

  if (previous_sw1 == 1 && current_sw1 == 0) {
    direction -= 1;
  }

  if (direction % 2 == 0){
    for (int i = last_led; i <= 7; i++){
      for (int j = last_led; j <= 7; j++){
        if (j==i) digitalWrite(j, HIGH);
        else     digitalWrite(j, LOW);
      }
      delay(500);
    }
    last_led = findLastLED();
  }
  else if(direction % 2 == 1 || direction == 0){
    for (int i = last_led; i >= 4; i--){
      for (int j = last_led; j >= 7; j--){
        if(j==i) digitalWrite(j, HIGH);
        else     digitalWrite(j, LOW);
      }
      delay(500);
    }
    last_led = findLastLED();
  }

previous_sw1 = current_sw1;
previous_sw2 = current_sw2;

}
int findLastLED(){
  for (int i = 7; i >= 4, i--){
    if (digitalRead(i) == HIGH){
      return i;
    }
  }
}

I want some advice.

versus

Please help me with my project.

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the < CODE / > icon from the ā€˜posting menuā€™ to attach the copied sketch.

Please post the entire program.

What does the code actually do? What is the problem?

You gave the pins names didn't you? Why not use the names? It would be easier to follow your code.

Hi @cloudg ,

Welcome to the forum..
Ouch, wouldn't compile, missing some code..
Took a guess and added what I thought should be there but must have got something wrong as ended up having to change just few more things to get it to compile..

const byte ledPins[4] = {4, 5, 6, 7};
const byte btnPins[2] = {3, 2};
byte stateMode = 0;
unsigned long debounceTimes[2];
unsigned long intervalDebounce = 50ul;
unsigned long lastSwitch;
int intervalSwitch = 1000;
byte lastLed = 3;
byte lastButtons[2] = {1, 1};
byte direction = 0;

void setup() {
  for (int i = 0; i < sizeof(ledPins); i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  for (int i = 0; i < sizeof(btnPins); i++) {
    pinMode(btnPins[i], INPUT_PULLUP);
  }
}

void loop() {
  unsigned long now = millis();
  for (int i = 0; i < sizeof(btnPins); i++) {
    if (now - debounceTimes[i] >= intervalDebounce) {
      byte btn = digitalRead(btnPins[i]);
      if (btn != lastButtons[i]) {
        debounceTimes[i] = now;
        lastButtons[i] = btn;
        if (btn == LOW) {
          //button pushed..
          if (i == 0) {
            stateMode = 1;
            direction = 0;
          } else if (i == 1 && stateMode == 1) {
            //button 2 pressed in mode 1
            stateMode = 2;
            direction = 1;
          }
        }
      }
    }
  }

  if (stateMode > 0) {
    if (now - lastSwitch >= intervalSwitch) {
      lastSwitch = now;
      digitalWrite(ledPins[lastLed], LOW);
      if (direction == 0) {
        lastLed++;
        if (lastLed > 3) lastLed = 0;
      }
      else {
        if (lastLed > 0) lastLed--; else lastLed = 3;
      }
      digitalWrite(ledPins[lastLed], HIGH);
    }
  }
}

think it does what you described..

Uno buttons blink leds..

have fun.. ~q

What is the task of this sketch in real life?

In general, you should not use magic numbers. The I/O pins love to have a functional name.

Don't use fritzing for "circuit or schematic" diagrams....it's confusing to say the least and no help at all.

@cloudg welcome to the forum. I hope we can help out.

For another viewpoint, your diagram is a fairly simple one and easy enough to understand. For anything much more complex a real schematic would be needed. A picture of a hand drawn schematic works well. There are free schematic drawing programs available. Look up KiCAD. You will want to document your circuits for future reference anyway.

Any time you use switches with a microcontroller you will need to debounce the switches. This can be done using hardware or software. Software is cheaper but adds a slight bit of complexity to the code. Hardware adds somewhat to the complexity of the circuit but makes the code easier. I have been experimenting with ā€œdebounce.hā€ and having good success.

If you could tell us what issues you are having that would help us help you.

I want to create code that operates like this. but Pressing the switch connected to Arduino pin 3 to activate the "left switch" while in that state, pressing the switch connected to pin 2 doesn't switch to the "right switch" as expected.

I'm relatively new to Arduino, so I'm a bit inexperienced. Thank you very much for your guidance.

1 Like

That is not the problem....the problem with fritzing is indeterminate connections, use of breadboard which have differing internal layouts etc. etc....... just follow the previous advice and don't use fritzing.

void setup()
{
  pinMode(3, INPUT);
  pinMode(2, INPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

  for(int i=4; i>=1; i--){
      for(int k=4; k>=1; k--){
        if(k==i) digitalWrite(k+3, HIGH);
        else     digitalWrite(k+3, LOW);
      }
      delay(500);
    }
}

int previous_sw1=0;
int current_sw1;
int previous_sw2=0;
int current_sw2;


void loop()
{
  current_sw1 = digitalRead(3);
  current_sw2 = digitalRead(2);
  if(previous_sw1==0&&current_sw1==0&&previous_sw2==0&&current_sw2==0){
    for(int i=4; i>=1; i--){
      for(int k=4; k>=1; k--){
        if(k==i) digitalWrite(k+3, HIGH);
        else     digitalWrite(k+3, LOW);
      }
      delay(500);
    }
  }

  current_sw1 = digitalRead(3);
  current_sw2 = digitalRead(2);

  if((previous_sw1==1&&current_sw1==0)||(previous_sw1==0&&current_sw1==0)){
    for(int i=4; i>=1; i--){
      for(int k=4; k>=1; k--){
        if(k==i) digitalWrite(k+3, HIGH);
        else     digitalWrite(k+3, LOW);
      }
      delay(500);
    }
  }
 
  previous_sw1 = current_sw1;
  previous_sw2 = current_sw2;

  current_sw1 = digitalRead(3);
  current_sw2 = digitalRead(2);

  if((previous_sw2==1&&current_sw1==0)||(previous_sw2==0&&current_sw1==0)){
    for(int i=1; i<=4; i++){
      for(int k=1; k<=4; k++){
        if(k==i) digitalWrite(k+3, HIGH);
        else     digitalWrite(k+3, LOW);
      }
      delay(500);
   }
  }

  previous_sw1 = current_sw1;
  previous_sw2 = current_sw2;
  
}

When power is supplied to the Arduino initially, the LEDs start from number 7 and move sequentially to the right. This action repeats until the 3rd button is pressed. Upon pressing the 3rd button, the direction of the LEDs changes to the left. (While the LEDs are moving to the right with 6th LED turned on, pressing the 3rd button should turn off the 6th LED and turn on the 7th LED.)

In the current code, the LEDs do not move to the right upon the initial power supply.

If possible, I would like the current code structure to remain largely unchanged. Is it possible?

Hello cloudg

In general, you should not use magic numbers. The I/O pins love to have a functional name.

I assume that you have written the programme by yourself, then it is quite easy to find the error.

There's a trick to figuring out why something isn't working:

Use a logic analyzer to see what happens.
Put Serial.print statements at various places in the code as diagnostic prints to see the values of variables, especially ones that control the motors, and determine whether they meet your expectations.

Have a nice day and enjoy coding in C++.

1 Like

How are the buttons wired? A little schematics please...

Why?
Your code structure is not well.
First of all, your code is blocking. It means that each of your 3 cycles lasts two seconds, during which you do not read the buttons. Reading the buttons occurs only between cycles and lasts a few moments. As a result, you have to guess at what moment to press the button or, more likely, press and hold it for a long time.
Blocking loops and working with buttons are incompatible; the code must be rewritten without using delays.
Secondly, you obviously wrote your code by simply copying blocks - this is a sign of poor programming style. For example, a block of code with loops

is repeated three times in your sketch without any changes.
Why not design it as a function?

Please don't create duplicate topics....See below for more info.

Hello @cloudg,

Your two or more topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you wonā€™t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Your code does what you wanted... the LEDs move to the right at start-up. Also, the LEDs change direction after a few button presses.

Note how I changed your code from "2" and "3" to SW2pin and SW1pin to separate 2/3 from "+3" when using the LEDs.

#define SW1pin 3
#define SW2pin 2

void setup()
{
  pinMode(SW1pin, INPUT);
  pinMode(SW2pin, INPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

  for (int i = 4; i >= 1; i--) {
    for (int k = 4; k >= 1; k--) {
      if (k == i) digitalWrite(k + 3, HIGH);
      else     digitalWrite(k + 3, LOW);
    }
    delay(500);
  }
}

int previous_sw1 = 0;
int current_sw1;
int previous_sw2 = 0;
int current_sw2;

void loop()
{
  current_sw1 = digitalRead(SW1pin);
  current_sw2 = digitalRead(SW2pin);
  if (previous_sw1 == 0 && current_sw1 == 0 && previous_sw2 == 0 && current_sw2 == 0) {
    for (int i = 4; i >= 1; i--) {
      for (int k = 4; k >= 1; k--) {
        if (k == i) digitalWrite(k + 3, HIGH);
        else digitalWrite(k + 3, LOW);
      }
      delay(500);
    }
  }

  current_sw1 = digitalRead(SW1pin);
  current_sw2 = digitalRead(2);

  if ((previous_sw1 == 1 && current_sw1 == 0) || (previous_sw1 == 0 && current_sw1 == 0)) {
    for (int i = 4; i >= 1; i--) {
      for (int k = 4; k >= 1; k--) {
        if (k == i) digitalWrite(k + 3, HIGH);
        else digitalWrite(k + 3, LOW);
      }
      delay(500);
    }
  }

  previous_sw1 = current_sw1;
  previous_sw2 = current_sw2;

  current_sw1 = digitalRead(SW1pin);
  current_sw2 = digitalRead(2);

  if ((previous_sw2 == 1 && current_sw1 == 0) || (previous_sw2 == 0 && current_sw1 == 0)) {
    for (int i = 1; i <= 4; i++) {
      for (int k = 1; k <= 4; k++) {
        if (k == i) digitalWrite(k + 3, HIGH);
        else digitalWrite(k + 3, LOW);
      }
      delay(500);
    }
  }
  previous_sw1 = current_sw1;
  previous_sw2 = current_sw2;
}

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