Serial Read/ Print/ Write

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();
}


}

Hello tomjarosz

try

Serial.println("1");

didn't change anything "" is for more than one digit '' is for only one digit

Try:

Serial.println(buttonState);

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)?

Can you clarify

1 Like

Yes I want the loop to read the data on serial monitor. Is that possible?

My wiring is simple right now I have the RTC code in another file. I'm just trying to learn the concept serial read.

I´m confused.

Take some time and describe the desired function of the sketch in simple words and this very simple.

that prints the word "buttonState"

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.

1 Like

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.

This is just to understand so I can apply it to the RTC clock - its gives time in the serial monitor, unless there's another way I don't know.

my real question is how do I use the number from the RTC clock in a loop?

Today is not my day.
I´m out.

ok thanks still helped

Which RTC ? How is it connected to your Arduino?

You can't read back what was printed. You need to store what you print and process that stored data.

So on the Arduino:

  1. Read RTC and store time.
  2. Print time to serial monitor.
  3. Do something with stored time (e.g. LED on or off).
1 Like

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.

Ever heard of variables :wink:

Like you do here
char ledState = Serial.read();

1 Like

I suppose you are using this library?
http://www.rinkydinkelectronics.com/resource/DS3231/DS3231.pdf

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

thanks I'll try that, might be what I'm looking for