Created
October 23, 2012 16:22
-
-
Save buildcircuit/3939855 to your computer and use it in GitHub Desktop.
PWM LED Control using Android serial emulator
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
/* buildcircuit.com*/ | |
int ledPin = 11; | |
String inString = ""; // string to hold input | |
int value; | |
void setup() { | |
// Initialize serial communications: | |
Serial.begin(9600); | |
} | |
void loop() { | |
// Read serial input: | |
while (Serial.available() > 0) { | |
int inChar = Serial.read(); | |
if (isDigit(inChar)) { | |
// convert the incoming byte to a char | |
// and add it to the string: | |
inString += (char)inChar; | |
} | |
// if you get a newline, print the string, | |
// then the string's value: | |
if (inChar == '?') { | |
Serial.print("Value:"); | |
// Serial.println(" "); | |
Serial.println(inString.toInt()); | |
value = inString.toInt(); | |
if (value>255) | |
{ | |
//value = 255; | |
Serial.println("Enter 0-255"); | |
Serial.println(" "); | |
// analogWrite(ledPin, value); | |
for(value = 0 ; value <= 255; value +=5) { | |
// sets the value (range from 0 to 255): | |
analogWrite(ledPin, value); | |
// wait for 30 milliseconds to see the dimming effect | |
delay(30); | |
} | |
inString = " "; | |
} | |
//Serial.print("String: "); | |
// Serial.println(inString); | |
// clear the string for new input: | |
else if (value==0) { | |
for(value = inChar ; value = 0; value -=5) { | |
// sets the value (range from 0 to 255): | |
analogWrite(ledPin, value); | |
delay(30); | |
} | |
value == 0; | |
analogWrite(ledPin, value); | |
} | |
else { | |
inString = " "; | |
analogWrite(ledPin, value); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment