I'm just learning the Serial Read function and my eventual goal is to use an RTC clock to tell time, then read the time from serial monitor and turn on LED's to make a clock that tells time in binary.
My question: I made code that allowed me to type "1" to turn on a LED and "0" to turn it off, then I had code to serial print the numbers "1" and "0" but they didn't activate the LED. Not sure what I'm missing?
How do you make what you serial print act on your code/ activate a pin?
void setup() {
Serial.begin(9600);
pinMode (12, OUTPUT);
pinMode (11, INPUT);
}
void loop() {
// int data = Serial.read;
if(Serial.available() > 0) { // Greater than zero or number of digits to read
char ledState = Serial.read();
if(ledState == '1'){
digitalWrite(12,HIGH);
}
if(ledState == '0') {
digitalWrite (12,LOW);
}
}
int buttonState = digitalRead(11);
if (buttonState == HIGH) {
Serial.println('1');
delay(500);
Serial.println();
}
}
Print knows how to print a char. It will send the ascii code so no need for the double quote version which uses up more memory.
@tomjarosz what’s the wiring ? Do you expect the button press to send 1 to the loop (ie the loop being able to read what you sent to the Serial monitor)?
no... what is sent out to whatever is connected to the Serial line out is gone (unless you connect Tx to Rx then you have a loopback). See that as putting something into an enveloppe and dropping it at the post office. unless you wrote you own address on the enveloppe you'll never going to receive this.
Just don't send it out if you want to use it... just use the variable you have to make a decision in the code, no need to print it out.
I'm pressing a button and it serial prints "1" or "0" (this is in replace of the RTC clock printing the time in the serial monitor to simplify before understanding strings fully)
I want the information in the serial monitor to activate pins controlling LED's (not me typing into my computer) based on Boolean logic in my code loop.
I don't know how to store information but this actually solved my problem I think. Just not printing to serial monitor and having a Boolean statement directly acting on rtc.getTimeStr() then using the monitor just to see what I'm doing but not in the loop.
If you want to do stuff based on what time it is, extracting the time string is not the best way to do so as you have to parse the string to extract the time… just use the other function getTime() and you have the data in numeric format into the time structure