Sunday, 19 March 2017

Arduino Dimmable LED - Linear Equations

 Dimmable LED Project
 
This brings up the subject of Linear Equations.
 0 to 1023 - this is the read value (potValue)
0 to 255  - this is the write value range. (LEDvalue)
 
we need to read one value range and convert to another value range. 
 
Led Val = y axis
Pot Val = x axis

m= (y2 -y1)/(x2-x1)
m = (LED Val2 - LED val 1) / (pot val2 - potVal 1)
m = (255-0) / (1023-0) = 255/1023
or
y-y1 =m(x-x1)
or
LED Val - LED val 1 =m(pot val - potVal 1)
LED Val - 0 =m(pot val - 0)
LED Val =m(pot val)
 LED Val =m * pot val
 
Thus 
LED Val = (255/1023)pot val
 

 
 The code:
------------------
// declare your variables
int potPin=A1;// 0 to 255
int gPin=6; // green pin
int potVal; // 0 to 1023 - potentiometer
float LEDVal; // will calculate it depending on potVal
 
void setup()
{
  pinMode(potPin, INPUT);
  pinMode(gPin, OUTPUT);
  Serial.begin(9600); // opens serial monitor
 
}

void loop()
{
  potVal=analogRead(potPin);
  LEDVal=(255./1023.)*potVal;
  analogWrite(gPin,LEDVal);
  Serial.println(LEDVal);
}
--------------------------------------------------------------

Thanks to Paul McWhorter (Tutorial 14)

 ---------------------------------
-------------------------------------

No comments:

Post a Comment