Skip to content

Instantly share code, notes, and snippets.

@robherley
Created March 15, 2019 03:06
Show Gist options
  • Save robherley/384d89f7216e33869fc10264a333c516 to your computer and use it in GitHub Desktop.
Save robherley/384d89f7216e33869fc10264a333c516 to your computer and use it in GitHub Desktop.
Reading Ints over Serial Monitor
int MOTORPIN = 9;
int analog_input = 82;
String input;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Current Analog Value:");
Serial.print(analog_input);
Serial.print("\n");
analogWrite(MOTORPIN, analog_input);
Serial.println("Enter a number...");
while(Serial.available() == 0){ } // wait for input
while(Serial.available()) {
// Read in a single character to the variable input
char ch = Serial.read();
input += ch;
delay(10); // Slow down loop a bit
}
int temp = input.toInt();
if(temp > 255 || temp == 0){
Serial.println("Invalid number entered!");
} else {
analog_input = temp;
}
input = "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment