im trying to have flshing lights inside a costem loop and need a secondary buton to tern the loop of or change to a nother loop. how do i get it to work? pleas fix my problem and send it back. the delays and all of the other stuf is corect for my need
this is the code so far:
const int buttonPin = 2;
const int buttonPin2 = 3;
const int led_blixt1 = 8;
const int led_blixt2 = 9;
int buttonState = 0;
int button2State = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(led_blixt1, OUTPUT);
pinMode(led_blixt2, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
button2State = digitalRead(buttonPin2);
if (buttonState == HIGH) {
loop();{
digitalWrite (led_blixt1, HIGH);
digitalWrite (led_blixt2, LOW);
delay(20);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, LOW);
delay(100);
digitalWrite (led_blixt1, HIGH);
digitalWrite (led_blixt2, LOW);
delay(20);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, LOW);
delay(100);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, HIGH);
delay(20);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, LOW);
delay(100);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, HIGH);
delay(20);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, LOW);
delay(100);
}
}
}
J-M-L
October 30, 2022, 11:25am
2
add the button, listen to the button push in the loop, react to button change...
And get rid of the delay in favour of a millsi() based approach if you want a responsive code.
Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software will display it correctly.
J-M-L
October 30, 2022, 7:02pm
4
don't call loop() form the loop(), just let it loop automagically
use a button library to make your code simpler. There are many
Why did you call loop(); within the loop? Do you know what the void loop() does?
If i just use the void loop the program only runs wile biton state high i want a simple click on the button to make it start and keep going and a click from a nother button to make it stop
J-M-L
October 30, 2022, 8:15pm
7
Just detect the button clicks and toggle a variable . Then use that variable to drive the right output
gcjr
October 30, 2022, 8:52pm
8
you could use a single button to change modes from off, mode1, mode2, off ...
the code under this if statement is only executed when the button input is HIGH, not detecting a button press or based on some mode
why execute loop() again within loop()
consider
#undef MyHW
#ifdef MyHW
const byte buttonPin = A1;
const byte led_blixt1 = 10;
const byte led_blixt2 = 11;
#else
const byte buttonPin = 2;
const byte led_blixt1 = 8;
const byte led_blixt2 = 9;
#endif
byte butState;
enum { MODE_OFF, MODE_1, MODE_LAST };
int mode = MODE_1;
// -----------------------------------------------------------------------------
void mode1 ()
{
digitalWrite (led_blixt1, HIGH);
digitalWrite (led_blixt2, LOW);
delay (20);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, LOW);
delay (100);
digitalWrite (led_blixt1, HIGH);
digitalWrite (led_blixt2, LOW);
delay (20);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, LOW);
delay (100);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, HIGH);
delay (20);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, LOW);
delay (100);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, HIGH);
delay (20);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, LOW);
delay (100);
}
// -------------------------------------
enum { Off = HIGH, On = LOW };
void off ()
{
digitalWrite (led_blixt1, Off);
digitalWrite (led_blixt2, Off);
}
// -----------------------------------------------------------------------------
void loop ()
{
byte but = digitalRead (buttonPin);
if (butState != but) {
Serial.println ("button");
butState = but;
delay (10); // debounce if needed
if (LOW == but) { // pressed
if (MODE_LAST == ++mode)
mode = MODE_OFF;
}
}
// -----------------
switch (mode) {
case MODE_1:
mode1 ();
break;
default:
off ();
break;
}
}
// -----------------------------------------------------------------------------
void setup ()
{
Serial.begin (9600);
pinMode (led_blixt1, OUTPUT);
pinMode (led_blixt2, OUTPUT);
pinMode (buttonPin, INPUT_PULLUP);
butState = digitalRead (buttonPin);
}
pleas consider that im a beginer with programing and dont know lots pf the comands
What help are you expecting from the forum?
how to stop a custom loop with a secondary button
J-M-L
October 31, 2022, 9:12am
12
as you do in the primary loop : test the secondary button in the said loop
can you add coments that say what the difrends comands do
the code makes the lights ether stuck on or flashing and i prefer a simplefyed code thats rather easy to understand
J-M-L
October 31, 2022, 10:10am
15
consider something like this
Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!
click on the green circle with the "play" arrow to start the simulation
and use the red and green buttons then in the interface to start and stop the code
The console will show the actions
const byte startPin = 2; // first button
const byte stopPin = 3; // second button
bool isRunning = false;
void setup() {
pinMode(startPin, INPUT_PULLUP);
pinMode(stopPin, INPUT_PULLUP);
Serial.begin(115200); Serial.println();
Serial.println(F("Ready. Press the start button (green)"));
Serial.println(F("- Press the green button to start running"));
Serial.println(F("- Press the red button to stop once running"));
}
void loop() {
if (isRunning) { // we are running test if the secondary button is pressed
if (digitalRead(stopPin) == LOW) {
isRunning = false;
Serial.println(F("Secondary button pressed, stop."));
} else {
// do what you need to do in a non blocking way when running
}
} else {
if (digitalRead(startPin) == LOW) {
isRunning = true;
Serial.println(F("Primary button pressed, Running."));
} else {
// do what you need to do in a non blocking way when notrunning
}
}
}
gcjr
October 31, 2022, 10:23am
16
// use ifdef to configure for "my" or OP's hardware
#undef MyHW
#ifdef MyHW
const byte buttonPin = A1;
const byte led_blixt1 = 10;
const byte led_blixt2 = 11;
#else
const byte buttonPin = 2;
const byte led_blixt1 = 8;
const byte led_blixt2 = 9;
#endif
byte butState; // used to detect when button is pressed
// define symbols for each mode
enum { MODE_OFF, MODE_1, MODE_2, MODE_LAST };
int mode = MODE_1;
// -----------------------------------------------------------------------------
// execute LED code from original post
void mode1 ()
{
digitalWrite (led_blixt1, HIGH);
digitalWrite (led_blixt2, LOW);
delay (20);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, LOW);
delay (100);
digitalWrite (led_blixt1, HIGH);
digitalWrite (led_blixt2, LOW);
delay (20);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, LOW);
delay (100);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, HIGH);
delay (20);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, LOW);
delay (100);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, HIGH);
delay (20);
digitalWrite (led_blixt1, LOW);
digitalWrite (led_blixt2, LOW);
delay (100);
}
// -----------------------------------------------------------------------------
// alternately turn on LEDs
void mode2 ()
{
digitalWrite (led_blixt1, ! digitalRead (led_blixt1));
digitalWrite (led_blixt2, ! digitalRead (led_blixt1));
delay (200);
}
// -------------------------------------
enum { Off = HIGH, On = LOW };
void off ()
{
digitalWrite (led_blixt1, Off);
digitalWrite (led_blixt2, Off);
}
// -----------------------------------------------------------------------------
void loop ()
{
// read button pin
byte but = digitalRead (buttonPin);
// check if there's a change in button pin state
if (butState != but) {
Serial.println ("button");
butState = but;
delay (10); // debounce if needed
// check if button is pressed, ignore release
if (LOW == but) {
// advance mode and wrap to zero at max value
if (MODE_LAST == ++mode)
mode = MODE_OFF;
}
}
// -----------------
// execute led sub-function depending on mode
switch (mode) {
case MODE_1:
mode1 ();
break;
case MODE_2:
mode2 ();
break;
default:
off ();
break;
}
}
// -----------------------------------------------------------------------------
void setup ()
{
Serial.begin (9600);
pinMode (led_blixt1, OUTPUT);
pinMode (led_blixt2, OUTPUT);
// configure button pin with pullup and capture initial state
pinMode (buttonPin, INPUT_PULLUP);
butState = digitalRead (buttonPin);
}
suggesting you want additional "modes"
i can not see eny flashing lights only the buttons
J-M-L
October 31, 2022, 10:35am
18
It's here
// do what you need to do in a non blocking way when running
or
// do what you need to do in a non blocking way when notrunning
➜ you need to do your part.... this is not a forum where people code for you
study blink without delay for example, it's in the builtin examples
i dident come hear for the whole code i came hear to find out how to stop a loop and not a void loop
Hello
Try this small example
/// pseudo code
if (startStop) runStartStop();