Skip to content

Instantly share code, notes, and snippets.

@bowserthewizard
Created October 27, 2014 18:52
Show Gist options
  • Save bowserthewizard/c73b15a3d0c2512bfa92 to your computer and use it in GitHub Desktop.
Save bowserthewizard/c73b15a3d0c2512bfa92 to your computer and use it in GitHub Desktop.
/////////////////////////////////////////////////////////////////
// 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(11, 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 = 240;
//now we want to take the input and map it to [a,b] for our duty cycle
int scale = (b-a+1)/(1023-0+1);
int offset = a;
displayBinary(interval);
return scale*addata + offset;
}
void displayBinary(byte num)
{
int out = 2;
for (int i =0;i<4;i++)
{
if (num&(1<<i))
{
digitalWrite(out+i, HIGH);
}
else
{
digitalWrite(out+i, LOW);
}
}
}
void motorcontrol()
{
int dutycycle = readinput();
analogWrite(11, dutycycle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment