Question
I have my code for arduino, I am working on making my LED turn its brightness up or down based on the user input from
I have my code for arduino, I am working on making my LED turn its brightness up or down based on the user input from the serial monitor. So what i need to do is, if i input 1, the LED light will turn on only 1%. If i input the number 60, then the LED will turn on 60% etc... I can only input numbers from 0 to 100. 0 being off completely, and 100 being the brightest. This is what i have so far: It turns off when i input 2 and turns on when i input 1. Not sure how to write what i want it to do.
String inData;
void setup() { // initialize serial: Serial.begin(115200); pinMode(D2, OUTPUT);
// reserve 200 bytes for the inputString: }
void loop() { while (Serial.available() > 0) { char recieved = Serial.read(); inData += recieved;
// Process message when new line character is recieved if (recieved == ' ') { Serial.print("Message Received: "); Serial.print(inData);
int x = inData.toInt(); if(x == 2){ digitalWrite(D2, LOW); // turn the LED on (HIGH is the voltage level) } else if (x == 1){ digitalWrite(D2, HIGH); // turn the LED off }
inData = ""; // Clear recieved buffer } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started