Skip to content

Instantly share code, notes, and snippets.

@makersylvia
Last active October 3, 2021 05:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makersylvia/9b133cb2d38c4052bd55 to your computer and use it in GitHub Desktop.
Save makersylvia/9b133cb2d38c4052bd55 to your computer and use it in GitHub Desktop.
Code for the Super-Simple strobe Arduino project
// Gray text are just helpful comments, you don’t need to type them. :)
/******* -----=====!! EASY STUFF TO MESS WITH !!=====------ ******/
// What analog pin should we use to read the value from the potentiometer?
int analogPin = 2; // Yep, you heard right: The coolest of the Analog pins...
// What pin is the LED connected to?
int ledPin = 13; // Contains a built in resistor!
// How much time should the light stay on between delays, in Microseconds (millionths of a second)?
/* Big number = more blur, more perceived brightness
* Small number = less blur, less perceived brightness */
long onTime = 250;
// What should the minimum delay be in milliseconds (thousandths of a second)?
// This sets the bottom delay range of the strobe, as a delay of 0 doesn't actually flash =P
// The strobe starts with this as the "fastest" mode, and goes slower from there, adding to the delay
int minDelay = 1; // 1 is the lowest we can actually do without a better delay function
// What should the maximum delay be in milliseconds?
// This is the longest time that the biggest potentiometer value will be mapped to, and longest
// time between strobe flashes.
int maxDelay = 100;
/******* -----=====^^ EASY STUFF TO MESS WITH ^^=====------ ******/
// Initialize the number to hold our strobe delay. Isn't used till we get to the main loop
long strobeDelay = 0;
void setup() {
pinMode(ledPin, OUTPUT); // Setup ledPin as an output.
}
void loop() {
// To make the math easier, we use map(value, fromMin, fromMax, toMin, toMax) to convert the
// 0 to 1023 range we get from analogRead, into our strobe delay range of 1 to 100 :D
strobeDelay = map(analogRead(analogPin), 0, 1023, minDelay, maxDelay);
digitalWrite(ledPin, HIGH); // Switch the ledPin to HIGH, turn it on!
delayMicroseconds(onTime); // Delay while on, for the given onTime.
digitalWrite(ledPin, LOW); // Switch the ledPin to LOW, turn if off!
delay(strobeDelay); // Delay while off, for given strobeDelay.
}
@ezeeetm
Copy link

ezeeetm commented Jan 3, 2015

Hey Sylvia....you are super awesome, but want to share a small issue with this code.

Note that you declare strobe_delay, but then its used as strobeDelay in the main loop. This causes a compile error when you get to strobeDelay, since it hasn't been declared.

Also, delayMicroseconds won't really allow one to to get a good delay in between blinks. It has a max value of 16383, which (in millionths of a second) isn't very long! http://arduino.cc/en/Reference/delayMicroseconds
I didn't know this either, I just figured it out playing with your program and wondering why I couldn't get the off time to get noticeably longer even when I configured HUGE values for this.

I thought it might be cool to re-write your version using the same value for the off time that is being used for the on time. That is, the value being determined by the potentiometer is used for both the on and off times.

It makes a pretty cool looking effect when you change the values with the potentiometer...as the on delay goes up/down so does the off delay! I changed maxDelay to 1000 to get as much of a one second on / one second off time.

Here is is with both changes made. Use it as you see fit!

-Claire and Ian's dad

https://gist.github.com/ezeeetm/5bf0c519b5e1fcf124a0

@makersylvia
Copy link
Author

Didn't see this here! Thanks for the note. Fixed the strobe_delay typo. We used delayMicroseconds() just for the flash because it's less blurry on fans. Our old code used to have the cutoff problem, but this one works better, even if it's not as good for really fast stuff. Thanks for the comment, and keep up the hacking!

@glibg10b
Copy link

glibg10b commented Oct 3, 2021

One design issue is that the loop waits for the delay to finish before continuing, so if the pot is near 0, the LED won't instantly respond when the pot gets adjusted. This causes the strobe to feel unresponsive at low frequencies. I'll see if I can do something about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment