Last active
April 7, 2020 20:09
-
-
Save danjperron/ba135bfa0269a5584e788748b5f8a6e1 to your computer and use it in GitHub Desktop.
Serial check for arduino pwm
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
int pwm; | |
const int controlPin=9; | |
String inString; // string to hold input | |
int DisplayInfo=1; | |
void setup() { | |
pinMode(controlPin, OUTPUT); | |
Serial.begin(9600); | |
pwm = 0; | |
analogWrite(controlPin, pwm); | |
} | |
void loop() { | |
if(DisplayInfo) | |
{ | |
inString=""; | |
Serial.print("PWM(0..100)?"); | |
DisplayInfo=0; | |
} | |
if(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 == '\n') { | |
if(inString.length() > 0){ | |
int value= inString.toInt(); | |
if((value < 0) || ( value >100)) | |
Serial.println("*** 0 à 100 ***"); | |
else | |
{ | |
pwm = value * 255 / 100; | |
analogWrite(controlPin, pwm); | |
Serial.print(" PWM de "); | |
Serial.print(value); | |
Serial.println("% accepté"); | |
} | |
DisplayInfo=1; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment