I need help with assigning button's to the code i have already written

i have written a code myself for the first time which kinda works.. - here's whats wrong the following code is for sequential turning lights using my Nodemcu 1.0 12E so that i can put up tur signal's for my bicycle. but i want to assign the both the left and right functions to a single button (or in my code labelled as tasks) could anyone help me on how to do it

#define led_one  16
#define led_two 5
#define led_three 4
#define led_four 0
#define led_five 2
#define led_six 14
#define led_seven 12
#define led_eight 13

int Delay = 70;

void setup() {
  pinMode(led_one, OUTPUT);
  pinMode(led_two, OUTPUT);
  pinMode(led_three, OUTPUT);
  pinMode(led_four, OUTPUT);
  pinMode(led_five, OUTPUT);
  pinMode(led_six, OUTPUT);
  pinMode(led_seven, OUTPUT);
  pinMode(led_eight, OUTPUT); 
  // put your setup code here, to run once:

}

void loop() {

//task 1 rrright
  LedChaser(1,0,0,0,0,0,0,0);
  delay(Delay);
  LedChaser(0,1,0,0,0,0,0,0);
  delay(Delay);
  LedChaser(0,0,1,0,0,0,0,0);
  delay(Delay);
  LedChaser(0,0,0,1,0,0,0,0);
  delay(Delay);
  LedChaser(0,0,0,0,1,0,0,0);
  delay(Delay);
  LedChaser(0,0,0,0,0,1,0,0);
  delay(Delay);
  LedChaser(0,0,0,0,0,0,1,0);
  delay(Delay);
  LedChaser(0,0,0,0,0,0,0,1);
  delay(Delay);

//task 2 llleft
  LedChaser(0,0,0,0,0,0,0,1);
  delay(Delay);
  LedChaser(0,0,0,0,0,0,1,0);
  delay(Delay);
  LedChaser(0,0,0,0,0,1,0,0);
  delay(Delay);
  LedChaser(0,0,0,0,1,0,0,0);
  delay(Delay);
  LedChaser(0,0,0,1,0,0,0,0);
  delay(Delay);
  LedChaser(0,1,0,0,0,0,0,0);
  delay(Delay);
  LedChaser(1,0,0,0,0,0,0,0);
  delay(Delay);
  
  // put your main code here, to run repeatedly:

}

void LedChaser(int i, int j, int k, int l, int m, int n, int o, int p)
{
  digitalWrite(led_one, i);
  digitalWrite(led_two, j);
  digitalWrite(led_three, k);
  digitalWrite(led_four, l);
  digitalWrite(led_five, m);
  digitalWrite(led_six, n);
  digitalWrite(led_seven, o);
  digitalWrite(led_eight, p);
}

Please describe how you envisage the turn indicator working with a single button input ?

I also suggest that you start reading up on arrays as using them will make your code simpler

okay i can use two buttons (although i had 1 button output by using press delay) but i want 1 button to trigger task 1 and vice versa

Do you know how to determine the state of an input (HIGH/LOW) using digitalRead() ?

Double-click for one of the directions could be another option. But I think it will not be easy, with any method, to reliably select the correct direction with a single button while riding, even on quite smooth roads.

I also not certain if the patterns you are using would be considered "road legal" in some countries!

Hello,
you can use one button with two switch functions:
(ON) - OFF - (ON)

You could use 1 switch with 3 positions

Could be, but doesn't sound like a winner in a traffic situation. You want something absolutely foolproof and reliable. Getting run over by a truck because you failed to double click fast enough is kind of sad.

1 Like

Probably easier to use short/ long click - with a threshold of 750 ms. Only one click required.

You will in any case want "repeater" LEDs where you can see them so you know what you have actually indicated.

1 Like

You also need a way to cancel the signal

A single button is going to be way too complicated to operate when in a busy situation

1 Like

hello @UKHeliBob - im sorry but i am very new to arduino's and programming. answer in short i don't know

@UKHeliBob i understand i bought a three way switch yesterdat the one with 2 on position's and one off position - turns out these are actually used for turn signals commonly. but i need help to assign both the on positions to the left and right signal could you help me?

Your switch probably has 3 pins. One pin is common, a d the others short to that common pin depending on the position of the switch. So commect the common pin to GND, and the others two 2 separate pins on your Arduino. Set those pins as inputs with pullup enabled and read them. Refer to one of the millions of tutorials on reading buttons.

@koraks yes absolutely, it has 3 pins.. could you forward any links regarding the tutorials ?
also thankyou very much

Have you looked at the examples in the IDE ? The Button example would be a good place to start

@UKHeliBob i have looked at the code - so far i understand it. now suppose that want the left toggle of the switch to do

//task 2 llleft
  LedChaser(0,0,0,0,0,0,0,1);
  delay(Delay);
  LedChaser(0,0,0,0,0,0,1,0);
  delay(Delay);
  LedChaser(0,0,0,0,0,1,0,0);
  delay(Delay);
  LedChaser(0,0,0,0,1,0,0,0);
  delay(Delay);
  LedChaser(0,0,0,1,0,0,0,0);
  delay(Delay);
  LedChaser(0,1,0,0,0,0,0,0);
  delay(Delay);
  LedChaser(1,0,0,0,0,0,0,0);
  delay(Delay);

and the right toggle of the switch to perform this

//task 1 rrright
  LedChaser(1,0,0,0,0,0,0,0);
  delay(Delay);
  LedChaser(0,1,0,0,0,0,0,0);
  delay(Delay);
  LedChaser(0,0,1,0,0,0,0,0);
  delay(Delay);
  LedChaser(0,0,0,1,0,0,0,0);
  delay(Delay);
  LedChaser(0,0,0,0,1,0,0,0);
  delay(Delay);
  LedChaser(0,0,0,0,0,1,0,0);
  delay(Delay);
  LedChaser(0,0,0,0,0,0,1,0);
  delay(Delay);
  LedChaser(0,0,0,0,0,0,0,1);
  delay(Delay);

how do i write the code ? i mean i understand reading button states but how to assign the button to do all the lines of code ?

@UKHeliBob i understand how to make a button to do simple tasks

if (buttonState == HIGH) {
   // turn LED on:
   digitalWrite(ledPin, HIGH);

but how do i do that for the whole array ?

Put the code for the left flasher in its own function and do the same for the right flasher. Wire the switch as suggested by @koraks

When the left input goes LOW call the leftFlasher() function. When the right input goes LOW call the rightFlasher() function.

Get that much working then the code can be changed to make it more efficient. Expect to make a lot changes

Certainly.
https://www.google.com/search?q=arduino+button+tutorial

thank you very much, will update about the changes !