Hey, I have a Spaceship Interface tutorial that I am trying to play with.
I am using the same schematics as the tutorial.
I have changed the code little bit.I have to keep the button down to keep the lights to got front and back.
However, I want to change it, so the lights stops right away when I release the button.
Since I am new to this, I want to learn it, so what kind of commands or codes do I have to use or play with?
I hope you understand my question.
Here is the code:
int switchState = 0;
void setup() {
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
}
void loop(){
switchState = digitalRead(2);
if (switchState == LOW){
// the button is not pressed
digitalWrite(3, LOW); // green LED
digitalWrite(4, LOW); // red LED
digitalWrite(5, LOW); // red LED
}
else { // the button is pressed
digitalWrite(3, HIGH);
delay(150);
digitalWrite(4, HIGH);
delay(150);
digitalWrite(5, HIGH);
delay(150);
delay(150);
digitalWrite(5, LOW);
delay(150);
digitalWrite(4, LOW);
delay(150);
digitalWrite(3, LOW);
delay(150);
}
} // go back to the beginning of the loop
@naluhat Why do users choose to ignore the advice on posting code ?
The easier you make it to read and copy your code the more likely it is that you will get help
Please follow the advice given in the link below when posting code , use code tags and post the code here
See also FAQ - Arduino Forum for general rules on forum behaviour and etiquette.
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…
Your post was MOVED to its current location as it is more suitable.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.
xr8022
June 14, 2021, 1:47pm
3
Adding if judgment may solve your problem
1 Like
So you should get rid of all the delays and use a millis based state machine for your blinking.
1 Like
Well, I thing a timer() function and may be a small FSM could help as well.
1 Like
gcjr
June 14, 2021, 2:07pm
6
consider
#undef MyHW
#ifdef MyHW
byte ledPins [] = { 10, 11, 12, 13 };
byte butPin = A1;
#else
byte ledPins [] = { 3, 4, 5 };
byte butPin = 2;
#endif
#define N_LEDS sizeof(ledPins)
enum { Off = HIGH, On = LOW };
int idx = 0;
int dir = 1;
void setup()
{
Serial.begin (9600);
for (unsigned n = 0; n < N_LEDS; n++) {
digitalWrite (ledPins [n], Off);
pinMode (ledPins [n], OUTPUT);
}
pinMode (butPin, INPUT_PULLUP);
}
void loop()
{
static unsigned long msecLst = 0;
unsigned long msec = millis();
if (Off == digitalRead (butPin)) {
for (unsigned n = 0; n < N_LEDS; n++)
digitalWrite (ledPins [n], Off);
}
else {
if ((msec - msecLst) > 150) {
msecLst = msec;
if (0 < dir)
digitalWrite (ledPins [idx], On);
else
digitalWrite (ledPins [idx], Off);
idx += dir;
if (0 > idx) {
idx = 0;
dir = 1;
}
if ((int)N_LEDS <= idx) {
idx = N_LEDS;
dir = -1;
}
}
}
}
1 Like
Hello
I have made a small sketch using a blinkPattern array and a timer to control led sequence.
const byte green = 3;
const byte red1 = 4;
const byte red2 = 5;
const byte button = 2;
//new
const byte blinkPattern[] = {
B00000001,
B00000011,
B00000111,
B00000110,
B00000100,
B00000110,
B00000111,
B00000011,
};
const byte LedGreen = 1;
const byte LedRed1 = 2;
const byte LedRed2 = 4;
unsigned long blinkMillis;
unsigned long blinkDuration = 150;
void setup() {
Serial.begin(9600);
pinMode(green, OUTPUT);
pinMode(red1, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
bool switchState = digitalRead(button);
if (switchState == HIGH) {
// the button is not pressed
digitalWrite(green, LOW); // green LED
digitalWrite(red1, LOW); // red LED
digitalWrite(red2, LOW); // red LED
}
else { // the button is pressed
// now we build a timer for the blink function
if (millis() - blinkMillis >= blinkDuration) {
static byte counter;
blinkMillis = millis();
digitalWrite(green, blinkPattern[counter]&LedGreen); // green LED
digitalWrite(red1, blinkPattern[counter]&LedRed1); // red LED
digitalWrite(red2, blinkPattern[counter]&LedRed2); // red LED
counter++;
counter = counter % sizeof(blinkPattern);
}
}
}
To do it right you have to stop using delay(). Because your delays are short you can get fairly quick response by checking the state of the button after each delay():
else { // the button is pressed
digitalWrite(3, HIGH);
delay(150);
if (digitalRead(2) == LOW)
return; // Start loop() over
digitalWrite(4, HIGH);
delay(150);
if (digitalRead(2) == LOW)
return; // Start loop() over
system
Closed
October 13, 2021, 8:03pm
9
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.