88Views5Replies
Help with basic arduino sketch for Halloween
Hello all,
I'm very new to Arduino programming, and was trying to write a basic sketch to flash some LEDs in a jack o lantern for halloween, but I'm struggling with the code.
Essentially what I wanted to accomplish was blinking a LED from pin 13 x times, followed by fading (pulsing) another LED from pin 9 x times and then repeat.
If anyone would be so kind as to help me accomplish this, I would be very grateful.
Thanks in advance.
Discussions
8 years ago
cut and paste from the blink and analog out sketch.
A for loop running a sine wave gives a nice fade with PWM.
Reply 8 years ago
Thank you.
Sorry, I should have been a little more clear in my original post. I can accomplish the two tasks individually w/o any issues... where I get stuck is putting them together in a sequence.
For example, blink 10 times, fade in and out 10 times, blink 10 times, fade in and out 10 times... (forever).
Reply 8 years ago
Put all the code for blink in the loop(), then put all the code for the fade right after it. (where all the code means copy all the variables to the same place, all the setup() to the new setup, and all the loop(). )
8 years ago
I think I finally got it... here's what I came up with:
If there's a better way to accomplish this, I'd love to see other examples.
Thanks again.
int strobe = 13;
int fade = 10;
void setup() {
pinMode(strobe, OUTPUT);
}
void loop() {
int strobecount=0;
while (strobecount < 10) {
digitalWrite(strobe, HIGH);
delay(50);
digitalWrite(strobe, LOW);
delay(50);
strobecount++;
}
int fadecount=0;
while (fadecount < 3) {
int x = 1;
for (int i = 0; i > -1; i = i + x){
analogWrite(fade, i);
if (i == 255) x = -1;
delay(10);
}
fadecount++;
}
}