Created
March 15, 2019 03:06
-
-
Save robherley/384d89f7216e33869fc10264a333c516 to your computer and use it in GitHub Desktop.
Reading Ints over Serial Monitor
This file contains hidden or 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
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