I only intermittently use Arduino to try and run simple sketches for experiments and am struggling with some of the placement of code.
The project I am trying to complete requires two LEDs connected to an Uno board. One LED should remain on and the other should blink at a specific rate. I would also like to control the brightness of both LEDs with a potentiometer, without affecting the blink rate.
I am able to achieve the blinking and dimming separately but not able to implement both using millis(). I can achieve both using delays.
Ultimately, I would like to add two push buttons that would increase or decrease the flash rate of the blinking LED by a measured amount when pressed. I would like to start at 2 Hz and increase/decrease by 1 Hz with each press.
Although I have looked through several tutorials that accomplish some of the blinking and dimming, I am not sure how to implement all three functions in the same sketch. Mostly what happens is one line will overwrite all the others and the LEDs will just blink or dim, but not both.
Is it possible to achieve all of these? Ideally, I would like the module to stand alone without having to re-upload changes from a computer because one will not be available. Any help in re-structuring this code and adding in the button presses would be appreciated.
Current code:
int potPin= A0; //Declare potPin to be analog pin A0
int LEDPin[]= {9, 10};// Declare LEDPin to be arduino pins 8,9,10,11
int LEDState = 0; // last state of each led
int readValue; // Use this variable to read Potentiometer
int writeValue; // Use this variable for writing to LED
int LEDPin9=9; // set LEDPin3 as output
int LEDPin10=10; // set LEDPin5 as output
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 100; // interval at which to blink (milliseconds)
//boolean LED10State = false; //state variable for LED
//boolean LED9State = false;
void setup() {
pinMode (LEDPin9, OUTPUT);
pinMode (LEDPin10, OUTPUT);
//set potPin to be an input
for (int i=0; i<=2; i++){ // set each led pin as an output
pinMode(LEDPin[i], OUTPUT);
}
}
void loop() {
unsigned long currentMillis = millis(); //grab current time
readValue = analogRead(potPin); //Read the voltage on the Potentiometer
writeValue = map(readValue, 0, 1023, 0, 255);
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 (LEDPin9 == 0) {
LEDState = writeValue;
} else {
LEDState = 0;
}
// set the LED with the ledState of the variable:
analogWrite(LEDPin9, LEDState);
analogWrite(LEDPin10, writeValue);
}
// //toggle LED10
// if ((unsigned long)(currentMillis-previousMillisLED10)>= intervalLED10){
// LED10State =!LED10State; //toggles the state
// analogWrite(LEDPin10, LED10State); //sets the LED based on ledState
// //save the 'current' time to pin 10's previousMillis
// previousMillisLED10 = currentMillis;
// }
//
// //toggle LED9
// if ((unsigned long)(currentMillis-previousMillisLED9)>= intervalLED9){
// LED9State =!LED9State; //toggles the state
// analogWrite(LEDPin9, LED9State); //sets the LED based on ledState
// //save the 'current' time to pin 9's previousMillis
// previousMillisLED9 = currentMillis;
// }
// analogWrite(LEDPin9,writeValue); // set the pwm value of pin 9
// analogWrite(LEDPin10,writeValue); // set the pwm value of pin 10
//
}