Go Down

Topic: Serial Print and Potentiometer Read sketch with Variable Delay Led (Read 1 time) previous topic - next topic

tyterh

This sketch uses a potentiometer and a led to
a.) delay the led on and off time by the value of the potentiomter &
b.) serial print the difference of the potentiometer values between loops and current value of the potentiometer.

Just made it to play around, might help beginners learn.

Code: [Select]
/*this sketch uses a potentiometer and a led to
a.) delay the led on and off time by the value of the potentiomter &
b.) serial print the difference between loops and current value of the potentiometer

Best results can be read between potentiometer values of 100 and 250.
ENJOY!
*/

int led=13; //flashing LED
int input=A5;// potentiometer input on analog pin 5
int value=0; //current potentiometer value
int lastvalue=0; // the last loops potentiometer value
int difference=0; // the difference between the potentiometer value of the last loop and the current one


void setup()
{
  pinMode(led,OUTPUT); // sets led pin as an output
  pinMode(input,INPUT); // sets potentiometer as an input on analog pin 5
  Serial.begin(9600); // beings serial connection
}
void loop ()
{
 lastvalue=value; //sets the last value to the value of the last loop
  value=analogRead(input);// reads the potentiometer ands sets the value as value
 
  difference=abs(value-lastvalue); //sets the difference to the absolute value of current value minus the last value
  if(value!=lastvalue){  //if the value is not the last value "!=" means not equal to or different than
    Serial.print("WHOA THE INPUT CHANGED BY "); //print
    Serial.print(difference); // print the difference as defined above in "difference=abs(value-lastvalue);"
    Serial.println();// prints a new line
  }
  digitalWrite(led, LOW); //turn the led on
  delay(value); // delay for the value in milliseconds
  digitalWrite(led,HIGH);// turn the led off
  delay(value);// delay for the value in milliseconds
  Serial.print(value);// print the current value
  Serial.println();//print a new line
 
}

Pedro147

Very well commented code that you have done there, congratulations for a job well done. I must be potentiometer delay month  8)  I did this adjust delay with pot to show multiplexing on a 7Seg LED that you might like to take a look at.
http://www.pedroduino.com

Go Up