Created
May 25, 2025 17:34
-
-
Save greed9/c95d173e8d592f91c2899289d04c371e to your computer and use it in GitHub Desktop.
Starter Sketch for Flicker Fusion A/D project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
AnalogReadSerial | |
Reads an analog input on pin 0, prints the result to the Serial Monitor. | |
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu). | |
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. | |
This example code is in the public domain. | |
https://docs.arduino.cc/built-in-examples/basics/AnalogReadSerial/ | |
*/ | |
// Using names instead of "magic numbers" is a good practice | |
#define POT_PIN A0 | |
#define LED_PIN 3 | |
// Utility routine to hack the automatic scaling off for the plot | |
void plotSpeed (float max_value, float sensorValue) | |
{ | |
Serial.print(0); // To freeze the lower limit | |
Serial.print(" "); | |
Serial.print(max_value); // To freeze the upper limit | |
Serial.print(" "); | |
Serial.println(sensorValue); // To send all three 'data' points to the plotter | |
} | |
// the setup routine runs once when you press reset | |
// or upload code | |
void setup() { | |
// initialize serial communication at 115200 bits per second. | |
// Be sure to set the serial plotter to this same speed. | |
Serial.begin(115200); | |
pinMode( LED_PIN, OUTPUT ) ; | |
} | |
// the loop routine runs over and over again forever: | |
void loop() { | |
// Don't worry, this gets changed to the measured value when | |
// you complete the sketch. This just makes the sketch blink | |
// the LED for your intial testing. | |
int led_on_time = 100 ; | |
//********************************************************** | |
// Insert code to read the input on analog pin 0: | |
//********************************************************** | |
//********************************************************** | |
// Insert code to | |
// Scale raw a/d (0-1023) to 0-100 because 100ms is | |
// plenty for the flicker experiment | |
// Using the Arduino map( ) method is eas. Assign the result to | |
// the led_on_time variable | |
//********************************************************** | |
// Here's the actual ON/OFF logic. | |
digitalWrite( LED_PIN, LOW) ; | |
delay( led_on_time ) ; | |
digitalWrite ( LED_PIN, HIGH ) ; | |
// convert period of delay to freq in Hz | |
float freq = 1.0 / ((( float ) led_on_time) / 1000.0) ; | |
// print out the value you read, forcing auto-scale off in the | |
// serial plotter | |
plotSpeed( freq, 100 ) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment