Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PWM LED Control using Android serial emulator
/* 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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.