Created
October 27, 2014 20:11
-
-
Save bowserthewizard/b3bdad258ef2bd9303d5 to your computer and use it in GitHub Desktop.
This file contains 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
///////////////////////////////////////////////////////////////// | |
// Analog_Input | |
// A program that turns on an LED on and off based on the value | |
// read from the A/D | |
// Arduino version 1.05 | |
///////////////////////////////////////////////////////////////// | |
int ledPin[] = {2,3,4,5}; | |
void setup() //This function executes only once | |
{ | |
for (int i =0;i<4;i++) | |
{ | |
pinMode(ledPin[i], OUTPUT); | |
pinMode(3, OUTPUT); | |
} | |
} | |
void loop(){ | |
while (1) { | |
readinput(); | |
delay(300); | |
motorcontrol(); | |
} | |
} | |
int readinput() // This function executes repeatedly | |
{ | |
int addata = analogRead(0); | |
int interval = 0; | |
int upper = 64; | |
while (upper <= 1024) { | |
if (addata < upper) { | |
break; | |
} else { | |
interval += 1; | |
upper += 64; | |
} | |
} | |
int a = 0; | |
int b = 255; | |
//now we want to take the input and map it to [a,b] for our pulse width | |
float scale = ((float)(b-a))/(1023-0); | |
int offset = a; | |
return scale*addata + offset; | |
} | |
void motorcontrol() | |
{ | |
int dutycycle = readinput(); | |
analogWrite(3, dutycycle); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment