Hi folks, I'm new to Arduino and need some help to finish a project for a Mark Rober course. Here's what I want to do: write a code to run three things at once on an Uno. Two are exactly the same: having a momentary switch turn on 4 LEDs one at a time having them stay on after being turned on - so there are two switches, each controlling 4 LEDs separately; I also want to control a servo having it turn when all 8 (2 sets of 4) LEDs are on. I need the LEDs to stay on once turned on up to a maximum of 10 hours. In effect, I have a counter of two banks of LEDs, each turned on at random times with one of the two switches that triggers the servo when all 8 LEDs are on. I would really appreciate the assistance as I have a "due date" in a few days with Mark's course. I have code that allows one switch to control 4 or more LEDs the way I want (i.e. turn on and then stay on), but can't figure out the rest. Thanks for any assistance. 2022-02-04T05:00:00Z→2022-02-18T05:00:00Z
Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.
A flowchart of you plan would be helpful
Indeed. Your word salad is somewhat difficult to untangle.
Perhaps the way the assignment was assigned was clear, can you post the specifications for this system you are trying to code up?
Oh, and welcome to the community!
a7
A what?
Wassat?
Thanks Larry. It took me a while to find how to reply. I had to switch from Safari to Chrome. The code I have is below. It doesn't have the servo part because I got stuck before that. I took a code that successfully used a momentary switch to turn on a LED and keep it on, then turn on the next and keep it on, etc. When I added the second switch I tried modifying the code, but obviously got tangled up in my own shoe laces.
I've been working from a schematic that you posted back on May 17th of 2017 Larry, trying to modify that. I just copied and pasted it here, but the below came up instead of the schematic.
I think the link to your schematic is: Switch On Multiple LEDs with Single Switch - #10 by LarryD
const byte leds[] = {3, 4, 5, 6,}; //HIGH = ON
const byte leds2[] = {7, 8, 9, 10,}; //HIGH = ON
const byte pushButton = 2; //pushed = LOW tells you the switch is wired between GND and the pin.
const byte pushButton2 = 11; //pushed = LOW tells you the switch is wired between GND and the pin.
unsigned long switchMillis;
unsigned long yswitchMillis;
void setup() //first set of LEDs
{
for (byte x = 0; x < sizeof(leds); x++)
{
pinMode(leds[x], OUTPUT);
digitalWrite(leds[x], LOW);
}
pinMode(pushButton, INPUT_PULLUP);
//second set of LEDs
for (byte y = 0; y < sizeof(leds2); y++)
{
pinMode(leds2[y], OUTPUT);
digitalWrite(leds2[y], LOW);
}
pinMode(pushButton, INPUT_PULLUP);
}
void loop()
{
if (millis() - switchMillis >= 20)
{
checkSwitches();
switchMillis = millis();
}
}
void checkSwitches()
{
static byte counter = 0;
static byte lastState = HIGH;
if (digitalRead(pushButton) != lastState && counter < sizeof(leds))
{
lastState = !lastState;
if (lastState == LOW)
{
digitalWrite(leds[counter], HIGH);
counter++;
}
}
}
And now with code tags, please.
Here's a link to Mark Rober: https://www.youtube.com/channel/UCY1kMZp36IQSyNx_9h4mpCg
He offers a 30 day course in "engineering" which I received as a gift. Part of the course involves Arduino (or similar product) use in a build. It has been fun and challenging, but I have never used Arduino code before. I have done some Fortran and COBOL decades ago.
const byte leds[] = {3, 4, 5, 6,}; //HIGH = ON
const byte leds2[] = {7, 8, 9, 10,}; //HIGH = ON
const byte pushButton = 2; //pushed = LOW tells you the switch is wired between GND and the pin.
const byte pushButton2 = 11; //pushed = LOW tells you the switch is wired between GND and the pin.
unsigned long switchMillis;
unsigned long yswitchMillis;
void setup() //first set of LEDs
{
for (byte x = 0; x < sizeof(leds); x++)
{
pinMode(leds[x], OUTPUT);
digitalWrite(leds[x], LOW);
}
pinMode(pushButton, INPUT_PULLUP);
//second set of LEDs
for (byte y = 0; y < sizeof(leds2); y++)
{
pinMode(leds2[y], OUTPUT);
digitalWrite(leds2[y], LOW);
}
pinMode(pushButton, INPUT_PULLUP);
}
void loop()
{
if (millis() - switchMillis >= 20)
{
checkSwitches();
switchMillis = millis();
}
}
void checkSwitches()
{
static byte counter = 0;
static byte lastState = HIGH;
if (digitalRead(pushButton) != lastState && counter < sizeof(leds))
{
lastState = !lastState;
if (lastState == LOW)
{
digitalWrite(leds[counter], HIGH);
counter++;
}
}
}
Is this correct?
Sorry everyone. I just figured out how to reply to the topic not just an individual. I apologize to whoever has seen this code.
const byte leds[] = {3, 4, 5, 6,}; //HIGH = ON
const byte leds2[] = {7, 8, 9, 10,}; //HIGH = ON
const byte pushButton = 2; //pushed = LOW tells you the switch is wired between GND and the pin.
const byte pushButton2 = 11; //pushed = LOW tells you the switch is wired between GND and the pin.
unsigned long switchMillis;
unsigned long yswitchMillis;
void setup() //first set of LEDs
{
for (byte x = 0; x < sizeof(leds); x++)
{
pinMode(leds[x], OUTPUT);
digitalWrite(leds[x], LOW);
}
pinMode(pushButton, INPUT_PULLUP);
//second set of LEDs
for (byte y = 0; y < sizeof(leds2); y++)
{
pinMode(leds2[y], OUTPUT);
digitalWrite(leds2[y], LOW);
}
pinMode(pushButton, INPUT_PULLUP);
}
void loop()
{
if (millis() - switchMillis >= 20)
{
checkSwitches();
switchMillis = millis();
}
}
void checkSwitches()
{
static byte counter = 0;
static byte lastState = HIGH;
if (digitalRead(pushButton) != lastState && counter < sizeof(leds))
{
lastState = !lastState;
if (lastState == LOW)
{
digitalWrite(leds[counter], HIGH);
counter++;
}
}
}
You have the following line 2 times in setup():
pinMode(pushButton, INPUT_PULLUP);
I assume the 2nd one should be:
pinMode(pushButton2, INPUT_PULLUP);
I would recommend the following changes to checkSwitches():
void checkSwitches()
{
static byte counter = 0;
static byte lastState = digitalRead(pushButton);
byte currState = digitalRead(pushButton);
if (currState != lastState)
{
lastState = currState;
if (currState == LOW && counter < sizeof(leds))
{
digitalWrite(leds[counter], HIGH);
counter++;
}
}
}
I assume the second button should do the same thing for the second set of LEDs.
Do you understand everything in this sketch ?
#define LEDon HIGH
#define LEDoff LOW
#define switchPushed LOW
#define switchReleased HIGH
const byte leds[] = {3, 4, 5, 6,};
const byte leds2[] = {7, 8, 9, 10,};
const byte heartbeatLED = 13;
const byte pushButton1 = 2;
const byte pushButton2 = 11;
byte counter1 = 0;
byte counter2 = 0;
byte lastPushButton1 = switchReleased;
byte lastPushButton2 = switchReleased;
unsigned long switchMillis;
unsigned long yswitchMillis;
unsigned long lastHeartbeatMillis;
//************************************************************************
void setup()
{
Serial.begin(115200);
pinMode(heartbeatLED, OUTPUT);
//**********************************
for (byte x = 0; x < sizeof(leds); x++)
{
pinMode(leds[x], OUTPUT);
digitalWrite(leds[x], LOW);
}
//**********************************
for (byte x = 0; x < sizeof(leds2); x++)
{
pinMode(leds2[x], OUTPUT);
digitalWrite(leds2[x], LOW);
}
pinMode(pushButton1, INPUT_PULLUP);
pinMode(pushButton2, INPUT_PULLUP);
} //END of setup()
//************************************************************************
void loop()
{
//********************************** h e a r t b e a t T I M E R
if (millis() - lastHeartbeatMillis >= 500)
{
//restart this TIMER
lastHeartbeatMillis = millis();
//toggle the LED
digitalWrite(heartbeatLED, ! digitalRead(heartbeatLED));
}
//********************************** c h e c k S w i t c h e s T I M E R
//is it time to check the switches ?
if (millis() - switchMillis >= 50)
{
//restart this TIMER
switchMillis = millis();
checkSwitches();
}
} //END of loop()
//************************************************************************
void checkSwitches()
{
byte state;
//********************************** s w i t c h 1
state = digitalRead(pushButton1);
if (lastPushButton1 != state)
{
//update to the new state
lastPushButton1 = state;
//*****************
//is the switch pushed ?
if (state == switchPushed)
{
digitalWrite(leds[counter1], LEDon);
counter1++;
if (counter1 > sizeof(leds))
{
counter1 = sizeof(leds);
}
}
} //END of switch1
//********************************** s w i t c h 2
state = digitalRead(pushButton2);
if (lastPushButton2 != state)
{
//update to the new state
lastPushButton2 = state;
//*****************
//is the switch pushed ?
if (state == switchPushed)
{
digitalWrite(leds2[counter2], LEDon);
counter2++;
if (counter2 > sizeof(leds2))
{
counter2 = sizeof(leds2);
}
}
} //END of switch 2
} //END of checkSwitches()
Thanks Larry. No, I'm too new to the code to understand all of it, but I'm trying to learn. Can you explain the functions of the parts? Does the heartbeat timer keep track of how many of the 8 LEDs are on? Are the switches being checked every 50 milliseconds? I appreciate the help.
The purpose of the heartbeatLED to to visually show you if code is executing and confirms it is not being blocked from running.
This code section is not needed for the sketch to operate, it is there to just help you see if things are normal.
The LED will toggle every 500ms if there is no blocking code.
Yes, the switches are being checked every 50ms.
For mechanical switches, 50ms scanning is more than adequate.
Hello danq84
Did you received information about the IPO model during the 30 day course in "engineering" for systems design already?
Engineering includes structured analyzis of the desired functionality too. You can do this during coding but this will lead to spaghetti code.
For the task:
INPUT: botton
PROCESSING: time controlled counter
OUTPUT: leds
Multiple identical functions are calling for coding in C++.
When is your due date?
Have a nice day and enjoy coding in C++.
Sweet code ... the lights work exactly how I want! Thank you so much Larry! You will definitely get "shoutout" in my final presentation. Now I just need to figure out how the end state of 8 LEDs on turns a servo 90 degrees. Would that be some kind of "if - then" code? BTW, how long have you been coding Arduino.
Hi Paul. This 30 day Mark Rober course in engineering is a "fly over" from 30,000 feet". We don't get into coding beyond being shown what it can do, and then trying ourselves, or seeking what we want online, or getting help from someone else in the class (if someone has the expertise and motivation). The due date for this build (which includes the code being used to "do something") is this coming Tuesday 2/8. I still have to build the device that uses the code and the Arduino. Larry has show me a solution for the question I first asked ... I'm super thankful for that. Oh yeah, we did not receive info about the IPO model. Thank you for bringing it to my attention.
byte counter 1 and byte counter 2?
YES
if (counter1 == 4 && counter2 == 4)
{
//do your stuff
}
