Skip to content

Instantly share code, notes, and snippets.

@amriunix
Created August 1, 2015 21:55
Show Gist options
  • Save amriunix/742dcfacc45a64f4308e to your computer and use it in GitHub Desktop.
Save amriunix/742dcfacc45a64f4308e to your computer and use it in GitHub Desktop.
Arduino potentiometer and led problem
int potval;
int led = 10;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void turnled(int potval){
float pv = potval/((1023/10)/10);
float lv = ((255/10)/10)*pv;
Serial.println(potval); // arduino says 1023 - Ok!
Serial.println(pv); //arduino says 102.00 - Normally it's 100 not 102.00
Serial.println(lv); //arduino says 104.00 - Normally it's 255 not 104.00
analogWrite(led, lv);
}
void loop() {
potval = analogRead(A0);
turnled(potval);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment