So currently I am on work placement away from home and I do not have an arduino with me, however, I have a fair bit of spare time and a couple of projects that I want to finish in my short break so I decided to get the coding out of the way (I plan to use https://circuits.io/ to emulate the arduino).
I am trying to input different variables through the serial monitor as if I was calling on an RTC. I had planned to either enter a string like <11> and set an hours value to 11 or <34> to set minutes to 34
I found someone's code (sorry for the bad call out, I cant seem to find it again) and tried modifying the showNewData function at the bottom:
const byte numChars = 32;
char receivedChars[numChars];
boolean hrs = false;
char hoursinput = 'h';
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithStartEndMarkers();
showNewData();
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
// if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
if (hrs == true){
Serial.println("hours set to");
Serial.println(receivedChars);
hrs = false;
}
if (receivedChars[] == 'h'){
hrs = true;
}
}
I know this is probably beginner grade stuff and it will probably be something simple like declaring the wrong type of variable but it has been quite awhile since my last programming class. If there is a better way of doing it (say inputting 'h8' and somehow slicing the string after the h) please let me know.
Thanks in advanced,
SimpleJoe
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.