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;
}
}
}
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);
}
}
}
@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.
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.
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?
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.
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?
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.