I am triyng to implement a little menu that read values from the EEPROM and display it.
If we what to change paramenteres edit it and write new value to EEPROM.
The problem is the waiting for the response in the serial port.
Have already tried diferent aproaches and search the web but did not found a solution.
Here is the code.
#include <EEPROM.h>
int MODE;
int PERIOD;
int DELAY;
int THRESHOLD;
int TIME;
int START;
int inByte = 0;
void setup()
{
// initialize serial:
Serial.begin(9600);
pinMode(8, INPUT);
digitalWrite(8, LOW);
if (digitalRead(8) == HIGH) {
dados();
// test serial:
if (Serial.available()) {
Serial.println("");
Serial.println("Dados OK 1_yes / 0_no ");
inByte = Serial.read();
if (inByte == 0){
inByte = 0;
Serial.print("Mode :");
Serial.println(EEPROM.read(0));
Serial.println("Dados OK 1_yes / 0_no ");
inByte = Serial.read();
if (inByte == 0){
Serial.print("Mode (0 - 5)");
MODE = Serial.read();
EEPROM.write(0, MODE);}
Serial.print("Period :");
Serial.println(EEPROM.read(1));
Serial.println("Dados OK 1_yes / 0_no ");
inByte = Serial.read();
if (inByte == 0){
Serial.print("Period (0 - 5)");
PERIOD = Serial.read();
EEPROM.write(1, PERIOD);}
Serial.print("Delay :");
Serial.println(EEPROM.read(2));
Serial.println("Dados OK 1_yes / 0_no ");
inByte = Serial.read();
if (inByte == 0){
Serial.print("Delay (0 - 5)");
DELAY = Serial.read();
EEPROM.write(2, DELAY);}
Serial.print("Threshold :");
Serial.println(EEPROM.read(3));
Serial.println("Dados OK 1_yes / 0_no ");
inByte = Serial.read();
if (inByte == 0){
Serial.print("Thereshold (0 - 5)");
THRESHOLD = Serial.read();
EEPROM.write(3, THRESHOLD);}
Serial.print("Start :");
Serial.print(EEPROM.read(4));
Serial.print("-");
Serial.print(EEPROM.read(5));
Serial.print("-");
Serial.print(EEPROM.read(6));
Serial.print(" ");
Serial.print(EEPROM.read(7));
Serial.print(":");
Serial.println(EEPROM.read(8));
Serial.println("Dados OK 1_yes / 0_no ");
inByte = Serial.read();
if (inByte == 0){
Serial.print("Start (dd-mm-yy hh:mm)");
Serial.println("dd");
START = Serial.read();
EEPROM.write(4, START);
Serial.println("mm");
START = Serial.read();
EEPROM.write(5, START);
Serial.println("yy");
START = Serial.read();
EEPROM.write(6, START);
Serial.println("hh");
START = Serial.read();
EEPROM.write(7, START);
Serial.println("mm");
START = Serial.read();
EEPROM.write(8, START);
dados();}
}
}
}
// write a 0 to all 512 bytes of the EEPROM
//EEPROM.write(0, dia);
delay(500);
// turn the LED on when we're done
digitalWrite(13, HIGH);
}
void loop()
{
}
void dados(){
Serial.print("Mode :");
Serial.println(EEPROM.read(0));
Serial.print("Period :");
Serial.println(EEPROM.read(1));
Serial.print("Delay :");
Serial.println(EEPROM.read(2));
Serial.print("Threshold :");
Serial.println(EEPROM.read(3));
Serial.println("Time (dd-mm-yy hh:mm) :");
Serial.print("Start (dd-mm-yy hh:mm) :");
Serial.print(EEPROM.read(4));
Serial.print("-");
Serial.print(EEPROM.read(5));
Serial.print("-");
Serial.print(EEPROM.read(6));
Serial.print(" ");
Serial.print(EEPROM.read(7));
Serial.print(":");
Serial.println(EEPROM.read(8));
}
Serial.available() lets you know as soon as the first character is received. So in this part:
if (Serial.available()) {
Serial.println("");
Serial.println("Dados OK 1_yes / 0_no ");
inByte = Serial.read();
You write something only after the user wrote something, and then you don't give him time to reply - you immediately read what he wrote previously....
Print your prompt.
Wait for user input on the Serial (e.g. while (!Serial.available()) ;)
Read the input (Serial.read()) and act according to it.
Be careful if you have responses that are longer than a single character! In that case you need some delay before executing the menu commands, to make sure all the characters arrived.
If s/he is using the Serial monitor your Arduino code could keep reading data until a carriage-return is received. That way you can get 1 or more characters.
If the user is inputting data into a purpose made PC program (which would be a much better idea) you can get the PC program to wrap the data between start and end markers (maybe < and >) for even greater reliability. That is the technique in this demo.
You could modify the code in the demo to only use an end marker and use carriage-return as the marker.
The goal is to use a dedicated program to change the parameters but for now just use the arduino serial monitor.
Was expeting to be an easier solution but it cames to be a real problem.
Can you sugest a code to start from?
until now all i have tried did not work.
Thanks
The ideia is to compare an number or a letter to start an action.
Have started with Y and N but to be more simple change it to boolean 0 or 1.
then i can change the eeprom memory adress to the new value.
Thanks
timoteo_mendes:
The ideia is to compare an number or a letter to start an action.
Have started with Y and N but to be more simple change it to boolean 0 or 1.
then i can change the eeprom memory adress to the new value.
Thanks
But what is sending the 0 that you're expecting to find.
that is not the problem.
Detect the '0' or 48 could be ok.
The problem is.
I what to start the cycle by testing a I/O port and if is high go to the next step that will be send/receave data using the serial monitor.
to do that i need to text if the serial is available and that is the first problem don't know what is the best option an while with (!Serial.available()) or a serial.available() >= 0...
than the other problem i have is very simple, after print a text i what it to wait for a response in from the serial monitor.
And than depend on the answer i perform a cycle.
That is the first goal.
Than i plan to use the processing to make a simple a sketch to comunicate with arduino and change parameteres in the arduino.
Did you understand.
Thanks
timoteo_mendes:
The problem is.
I what to start the cycle by testing a I/O port and if is high go to the next step that will be send/receave data using the serial monitor.
to do that i need to text if the serial is available and that is the first problem don't know what is the best option an while with (!Serial.available()) or a serial.available() >= 0...
Serial.available() will return 0 if there is no data waiting to be read.
so while( !Serial.available()); will wait until there IS serial available.
than the other problem i have is very simple, after print a text i what it to wait for a response in from the serial monitor.
Maybe the examples in this demo would be easier to follow.
From the various advices you have received you should do your best to make a program and, if it does not work, then post your new code here. It will make it easier to help you.
Thanks for your suport but i am really mising some part.
The problem is, as soon i input a value it start to cycle the while (digitalRead(8) == HIGH) {...
I what it to regist a value then stop for the next input and so on.
the code is:
#include <EEPROM.h>
int MODE;
int PERIOD;
int DELAY;
int THRESHOLD;
int TIME;
int START;
int inByte = 0;
void setup()
{
// initialize serial:
Serial.begin(9600);
pinMode(8, INPUT);
digitalWrite(8, LOW);
while (digitalRead(8) == HIGH) {
dados();
// test serial:
while (!Serial.available()){
Serial.println("");
Serial.println("Dados OK 1_yes / 0_no ");
while (!Serial.available()){
inByte = Serial.read();}
if (inByte == '0'){
Serial.print("Mode :");
Serial.println(EEPROM.read(0));
Serial.println("Dados OK 1_yes / 0_no ");
while (!Serial.available()){
inByte = Serial.read();}
if (inByte == '0'){
Serial.print("Mode (0 - 5)");
MODE = Serial.read();
EEPROM.write(0, MODE);}
Serial.print("Period :");
Serial.println(EEPROM.read(1));
Serial.println("Dados OK 1_yes / 0_no ");
while (!Serial.available()){
inByte = Serial.read();}
if (inByte == '0'){
Serial.print("Period (0 - 5)");
PERIOD = Serial.read();
EEPROM.write(1, PERIOD);}
Serial.print("Delay :");
Serial.println(EEPROM.read(2));
Serial.println("Dados OK 1_yes / 0_no ");
while (!Serial.available()){
inByte = Serial.read();}
if (inByte == '0'){
Serial.print("Delay (0 - 5)");
DELAY = Serial.read();
EEPROM.write(2, DELAY);}
Serial.print("Threshold :");
Serial.println(EEPROM.read(3));
Serial.println("Dados OK 1_yes / 0_no ");
while (!Serial.available()){
inByte = Serial.read();}
if (inByte == '0'){
Serial.print("Thereshold (0 - 5)");
THRESHOLD = Serial.read();
EEPROM.write(3, THRESHOLD);}
Serial.print("Start :");
Serial.print(EEPROM.read(4));
Serial.print("-");
Serial.print(EEPROM.read(5));
Serial.print("-");
Serial.print(EEPROM.read(6));
Serial.print(" ");
Serial.print(EEPROM.read(7));
Serial.print(":");
Serial.println(EEPROM.read(8));
Serial.println("Dados OK 1_yes / 0_no ");
while (!Serial.available()){
inByte = Serial.read();
Serial.flush();}
if (inByte == '0'){
Serial.print("Start (dd-mm-yy hh:mm)");
Serial.println("dd");
START = Serial.read();
EEPROM.write(4, START);
Serial.println("mm");
START = Serial.read();
EEPROM.write(5, START);
Serial.println("yy");
START = Serial.read();
EEPROM.write(6, START);
Serial.println("hh");
START = Serial.read();
EEPROM.write(7, START);
Serial.println("mm");
START = Serial.read();
EEPROM.write(8, START);
dados();}
}
}
}
// write a 0 to all 512 bytes of the EEPROM
//EEPROM.write(0, dia);
delay(500);
// turn the LED on when we're done
digitalWrite(13, HIGH);
}
void loop()
{
}
void dados(){
Serial.print("Mode :");
Serial.println(EEPROM.read(0));
Serial.print("Period :");
Serial.println(EEPROM.read(1));
Serial.print("Delay :");
Serial.println(EEPROM.read(2));
Serial.print("Threshold :");
Serial.println(EEPROM.read(3));
Serial.println("Time (dd-mm-yy hh:mm) :");
Serial.print("Start (dd-mm-yy hh:mm) :");
Serial.print(EEPROM.read(4));
Serial.print("-");
Serial.print(EEPROM.read(5));
Serial.print("-");
Serial.print(EEPROM.read(6));
Serial.print(" ");
Serial.print(EEPROM.read(7));
Serial.print(":");
Serial.println(EEPROM.read(8));
}
timoteo_mendes:
The problem is, as soon i input a value it start to cycle the while (digitalRead(8) == HIGH) {...
I what it to regist a value then stop for the next input and so on.
Sorry. I don't understand.
Can you give some examples of what the user will send and what you want the Arduino to do? - just described in plain language without code.
The ideia is:
print a message that ask to put a y for yes and a n for no.
wait the user enter an y or n
depend on the answer it generate a sequence of events.
Just that.
De problem now is, when i introduce the first character it start an infinit loop.
Please try the code to undertand the error.
Thanks.
Timóteo
I think you missunderstand the ! symbol That means NOT
So in this section of code, FIRST you check that serial is NOT available,
do some printing then immediately do a pontless check that there's
NOT serial available (of course there's not you just checked )
So then having established beyond any doubt that there's NOT serial available, you try to read from it!
while (!Serial.available()){
Serial.println("");
Serial.println("Dados OK 1_yes / 0_no ");
while (!Serial.available()){
inByte = Serial.read();}
that way was the only that it stops after "Serial.println("Dados OK 1_yes / 0_no ");"
and it supose to acept the 0 or 1 and do someting
but after i introduce a number (can be 0) it just print the dados();
Did you understand what i need?
The problem is that i don't know how to clear the serial.read() or another option...
Thanks
Timóteo