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.
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)}
}
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));
}
}
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)
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.