Hello, I'm an Arduino very beginner, and I'm trying to make an animation with 5 Leds, one piezzo buzzer and one button with the millis() function. I would wish, when i press the button, to switch on the leds one by one with differents delays, and activate the buzzer at the same time, , like a "light saber". (And after that, I did a random blinkwith the analogWrite function, (I use PWM pins)).
Actualy, with my Sketch, I managed to make a sound that ranges from high to low. But it goes on the loop, i prefered the sound start from high and goes to low, and after that stay on low. And the leds are not switching on, one by one, like i would wish.
Thanks a lot in advance for your time.
const int LED_A = 5;
const int LED_B = 6;
const int LED_C = 9;
const int LED_D = 10;
const int buttonPin = 13;
const int buttonPin02 = 8;
const int buzzer = 12;
unsigned long TempsActuel ;
int TempsSauvegarde = 0;
int buttonState = LOW;
void setup() {
pinMode(LED_A, OUTPUT);
pinMode(LED_B, OUTPUT);
pinMode(LED_C, OUTPUT);
pinMode(LED_D, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
}
void loop() {
TempsActuel = millis();
buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
{
TempsSauvegarde = TempsActuel;
if (TempsActuel - TempsSauvegarde >= 0)
{
for (int x = 500; x > 0 ; x--) {
tone (12, x, TempsActuel);
delay(1);
}
digitalWrite(LED_A, HIGH);
TempsSauvegarde = TempsActuel;
}
if (TempsActuel - TempsSauvegarde >= 300)
{
digitalWrite(LED_B, HIGH);
TempsSauvegarde = TempsActuel;
}
if (TempsActuel - TempsSauvegarde >= 150)
{
digitalWrite(LED_C, HIGH);
TempsSauvegarde = TempsActuel;
}
if (TempsActuel - TempsSauvegarde >= 150)
{
digitalWrite(LED_D, HIGH);
TempsSauvegarde = TempsActuel;
}
analogWrite(LED_A, random(50, 255));
analogWrite(LED_B, random(50, 255));
analogWrite(LED_C, random(50, 255));
analogWrite(LED_D, random(50, 255));
delay(random(30, 120));
}
else
{
noTone (12);
digitalWrite(LED_D, LOW);
delay(150);
digitalWrite(LED_C, LOW);
delay(150);
digitalWrite(LED_B, LOW);
delay(150);
digitalWrite(LED_A, LOW);
}
}