Hello .
I've been trying for a few days to make two LEDs light up in the void loop() as follows... when the switch is HIGH, Led1 = ON for 10 seconds, then Led 2 = ON while the switch is HIGH.
If (Switch == HIGH)
{
}
Thank you for any guidance.
Hello .
I've been trying for a few days to make two LEDs light up in the void loop() as follows... when the switch is HIGH, Led1 = ON for 10 seconds, then Led 2 = ON while the switch is HIGH.
If (Switch == HIGH)
{
}
Thank you for any guidance.
what Arduino board are you using?
upload your code (using code tags </>)
upload a schematic of the circuit
For timing, study this Blink Without Delay tutorial.
Thank you for the answers.
Arduino Nano board.
The code is an example Button from the IDE, slightly modified.
const int buttonPin = 7;
const int ledPin1 = 12;
const int ledPin2 = 13;
int buttonState = 0;
void setup() {
Serial.begin(115200);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
{
digitalWrite(ledPin1, HIGH);
}
else
{
digitalWrite(ledPin1, LOW);
}
Serial.println(digitalRead(ledPin1));
//Serial.println(digitalRead(ledPin2));
}
But I don't know how to get the ones mentioned at the beginning.
If I didn't make myself understood, I want Led1 to light up only when the switch is HIGH, for 10 seconds, then only Led2 will light up.
The same every time when the switch is HIGH.
I will also try... Blink Without Delay.
This sounds like a class assignment, but if you want exactly what you say then this should do.
int switch = 3; // pin switch is on
int led1 = 5; // pin LED 1 is on
int led2 = 6; // pin LED 2 is on
void setup (){
}
void loop(){
if (digitalRead(switch) == HIGH)
{
digitalWrite(led1, HIGH);
delay(10000);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
while(digitalRead(switch) == HIGH) {}; // hold until not high
digitalWrite(led2, LOW);
}
}
Chang the pin values to suit your circuit.
Not only you didn't make yourself understood, but you totally used the wrong words and asked for something else. Any idea how this feels for me when you have me waste my time?
so both LEDs can be on at the same time?
No, no, only Led2 = On remains.
Led1 = On, only on the next press.
what if the switch goes LOW before 10 seconds?
assume by switch HIGH you mean pressed. switches normally connected between pin and ground with pin configured with pullup and therefore pull the pin LOW when pressed
const byte ButPin = A1;
const byte LedPin1 = 13;
const byte LedPin2 = 12;
unsigned long msecLst;
unsigned long MsecPeriod = 5000;
byte butState;
bool led1On = false;
enum { Off = HIGH, On = LOW };
// -----------------------------------------------------------------------------
void
loop (void)
{
unsigned long msec = millis ();
if (led1On && msec - msecLst >= MsecPeriod) {
led1On = false;
digitalWrite (LedPin1, Off);
}
byte but = digitalRead (ButPin);
if (butState != but) {
butState = but;
delay (20); // debounce
if (LOW == but) {
led1On = true;
msecLst = msec;
digitalWrite (LedPin1, On);
digitalWrite (LedPin2, On);
}
else
digitalWrite (LedPin2, Off);
}
}
void
setup (void)
{
Serial.begin (9600);
pinMode (ButPin, INPUT_PULLUP);
butState = digitalRead (ButPin);
pinMode (LedPin1, OUTPUT);
pinMode (LedPin2, OUTPUT);
digitalWrite (LedPin1, Off);
digitalWrite (LedPin2, Off);
}
You haven't got a clue how to describe what you want to do. So how are we supposed to know how to help you do it? Try using Google translate.
Let me remind you from the first post you said:-
Yes, some mistakes probably crept in.
The button is High by PULLUP,
When pressed it is LOW.
So, when the button is LOW, Led1 should be lit for only 10 seconds,
After that, Led2 lights up and stays lit until the button is HIGH again.
His code - gcjr - is close to what you want, I'm trying to correct it...
Thanks.
where are these requirements coming from? for what application?
if switch is released immediately, LED2 will come on but not turn off until next cycle
const byte ButPin = A1;
const byte LedPin1 = 13;
const byte LedPin2 = 12;
unsigned long msecLst;
unsigned long MsecPeriod = 5000;
byte butState;
bool led1On = false;
enum { Off = HIGH, On = LOW };
// -----------------------------------------------------------------------------
void
loop (void)
{
unsigned long msec = millis ();
if (led1On && msec - msecLst >= MsecPeriod) {
led1On = false;
digitalWrite (LedPin1, Off);
digitalWrite (LedPin2, On);
}
byte but = digitalRead (ButPin);
if (butState != but) {
butState = but;
delay (20); // debounce
if (LOW == but) {
led1On = true;
msecLst = msec;
digitalWrite (LedPin1, On);
}
else
digitalWrite (LedPin2, Off);
}
}
void
setup (void)
{
Serial.begin (9600);
pinMode (ButPin, INPUT_PULLUP);
butState = digitalRead (ButPin);
pinMode (LedPin1, OUTPUT);
pinMode (LedPin2, OUTPUT);
digitalWrite (LedPin1, Off);
digitalWrite (LedPin2, Off);
}
I want to use them for GPS navigation, where with a single button I can read the latitude + longitude and after a delay (when it calculates the course, 10ms) I can also save the course.
The code with the LEDs was to see how to adapt. I don't know otherwise.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.