/* * POT_in_Serial_Out BJ Furman 09APR2012 Demonstrates analog input by reading an analog sensor (pot) using the analogRead() functionon analog pin 1 and turns on and off the blue LED on pin 6 at a rate proportional to the pot voltage: higher voltage, faster blinking; lower voltage, slower blinking. The millis() function is used to control when blinking occurs instead of delay(), which can lead to degraded performance for long delay times. Also demonstrates the use of #ifdef for conditional compilation. based on the AnalogInput example Created by David Cuartielles Modified 16 Jun 2009 By Tom Igoe http://arduino.cc/en/Tutorial/AnalogInput */ //#define SER_MON #define MAX_DELAY_TIME 1000 // max delay in milliseconds #define MIN_DELAY_TIME 10 // min delay in milliseconds #define MAX_POT_VALUE 1023 // max pot reading #define MIN_POT_VALUE 0 // min pot reading const byte potPin = 1; // potentiometer output on pin 1 const byte ledPin = 6; // blue LED on pin 6 unsigned int potVoltage = 0; // variable to store the value from the pot unsigned int delay_ms = MAX_DELAY_TIME; static unsigned long LWaitMillis; // variable for next blink time void setup() { // declare the ledPin as an output, potPin as an input: pinMode(ledPin, OUTPUT); pinMode(potPin, INPUT); // initialize serial communications at 9600 bps: Serial.begin(9600); LWaitMillis = millis() + delay_ms; } void loop() { // read the value from the sensor: potVoltage = analogRead(potPin); delay_ms = map(potVoltage,MIN_POT_VALUE,MAX_POT_VALUE,MIN_DELAY_TIME,MAX_DELAY_TIME); // print the results to the serial monitor: #ifdef SER_MON // if defined, then include serial monitor printing Serial.print("sensor = " ); Serial.print(potVoltage); // print voltage Serial.print(" delay, ms = " ); Serial.println(delay_ms); // print delay value and end-of-line #endif // Is it time to blink? (Note: non-blocking) if( (signed long) ( millis() - LWaitMillis ) >= 0 ) // if TRUE, blink and reset time { digitalWrite( ledPin, (digitalRead(ledPin) ^ 1) ); // toggle the LED LWaitMillis = millis() + delay_ms; // reset the time to blink } }