Any help would be much appreciated

I am very new to arduino and am slowley learning but I am struggling at the moment , I am trying to make a indicator programme for my motorcycle , it has a momentary switch for left and one for right, I am using the outputs to control relays as the indicators are 12v.

I have looked everywhere for a code so I can control both indicators off of one mano but I cannot find one. I have found a code to turn both relays on and off with momentary switches which is great but I also need them to flash when they are on, I have spent hours looking at programmes which make leds flash but I cannot for the life of me add this code to to the above programme and get it to work, would anyone be able to help me please.

Which program?

For the life of me I cannot find that programme now I have looked everywhere , it looks like it didnt save !

Please read the forum guide so that you know how to post code correctly on the forum. Then please fix your post above. Thanks!

The forum guide is in the sticky post at the top of most forum sections.

Do you wish to keep pressing while turning, or press it once to turn, and press it again to stop turning)?

Would you want it to auto cancel to stop blinking after 10 seconds?

I would really like it so i push it once to start and then again to stop

This code?

const int ledlPin = 12;
const int buttonApin = 4;
const int buttonBpin = 2;
const unsigned long interval = 700;

boolean Blinking = false;
boolean ledlState = false;
boolean ledrState = false;
unsigned long previousMillis = 0;

void setup()
{
pinMode(ledlPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
}

void loop()
{
if (digitalRead(buttonApin) == LOW)
Blinking = true;

if (digitalRead(buttonBpin) == LOW)
Blinking = false;

if (Blinking)
{

unsigned long currentMillis = millis();


if (currentMillis - previousMillis >= interval)
{
  // save the last time you blinked the LED
  previousMillis = currentMillis;


  // if the LED is off turn it on and vice-versa:
  ledlState = !ledlState;

  // set the LED with the ledState of the variable:
  digitalWrite(ledlPin, ledlState);
}
} // end if (Blinking)}
}

Hello wippy

Consider this sketch derived from the BlinkWithOutDelay example from the IDE.

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin
const int LeftLedPin =  9 ;// the number of the LED pin
const int RightLedPin =  10 ;// the number of the LED pin
const int LeftButtonPin =  A0;
const int RightButtonPin =  A1;

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 500;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(LeftLedPin, OUTPUT);
  pinMode(RightLedPin, OUTPUT);
  pinMode(LeftButtonPin, INPUT_PULLUP);
  pinMode(RightButtonPin, INPUT_PULLUP);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    digitalWrite(LeftLedPin, ledState && (digitalRead(LeftButtonPin)?LOW:HIGH));
    digitalWrite(RightLedPin, ledState && (digitalRead(RightButtonPin)?LOW:HIGH));
  }
}

Have a nice day and enjoy coding in C++.

1 Like

I made some code to match your application.

Code:

const uint8_t leftIndicator = 2;
const uint8_t rightIndicator = 3;
const uint8_t leftButton = 4;
const uint8_t rightButton = 5;
const uint16_t delayTime = 500;
uint32_t time;
uint8_t left;
uint8_t right;



void setup() {
  // put your setup code here, to run once:
  pinMode(leftIndicator, OUTPUT);
  pinMode(rightIndicator, OUTPUT);
  pinMode(leftButton, INPUT_PULLUP);
  pinMode(rightButton, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  left = !digitalRead(leftButton);
  right = !digitalRead(rightButton);

  if (millis() - time >= delayTime) {
    if (left == HIGH && right == LOW) {
    digitalWrite(leftIndicator, !digitalRead(leftIndicator));
    digitalWrite(rightIndicator, LOW);
  } else if (left == LOW && right == HIGH) {
    digitalWrite(leftIndicator, LOW);
    digitalWrite(rightIndicator, !digitalRead(rightIndicator));
  } else {
    digitalWrite(leftIndicator, LOW);
    digitalWrite(rightIndicator, LOW);
  }
    time = millis();
  }
}

Schematic:

Btw I tested the arduino side of things with the coding, but the relay part is just what's in a commercial relay module but without the led.(If you have a relay module, use it instead)

image

1 Like

millis returns uint32_t

Oh, ok I changed it!

There's also no good reason why the variables left and right should have global scope.
Or time for that matter, assuming it is assigned in the correct place.

Because most people just declare their variables global. Doesn't matter.

Fixed that for you.

Does it really make a big difference?

Yes, it really does.
Keep scope as local as possible.

Should I declare it static in the loop, or not?

The only item that needs to be static in loop() is "time".
(a better name too, maybe, like "timeStamp" or "lastEventAt")

(Someone will be along shortly to declare the undesirability of using the logical NOT operator with HIGH and LOW)

Ok, here is the code:

const uint8_t leftIndicator = 2;
const uint8_t rightIndicator = 3;
const uint8_t leftButton = 4;
const uint8_t rightButton = 5;
const uint16_t delayTime = 500;

void setup() {
  // put your setup code here, to run once:
  pinMode(leftIndicator, OUTPUT);
  pinMode(rightIndicator, OUTPUT);
  pinMode(leftButton, INPUT_PULLUP);
  pinMode(rightButton, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  static uint32_t timeStamp;
  uint8_t left = !digitalRead(leftButton);
  uint8_t right = !digitalRead(rightButton);

  if (millis() - timeStamp >= delayTime) {
    if (left == HIGH && right == LOW) {
    digitalWrite(leftIndicator, !digitalRead(leftIndicator));
    digitalWrite(rightIndicator, LOW);
  } else if (left == LOW && right == HIGH) {
    digitalWrite(leftIndicator, LOW);
    digitalWrite(rightIndicator, !digitalRead(rightIndicator));
  } else {
    digitalWrite(leftIndicator, LOW);
    digitalWrite(rightIndicator, LOW);
  }
    timeStamp = millis();
  }
}