Im a real beginner at Arduino and i have to complete some work for friday. If you could help me out it would be great,
there are two questions:
1) Write a program which reads one character/byte entered at the serial monitor and echoes this back to the serial monitor. There is an example covering this in the language reference.
Extend this program to read a string of characters entered at the serial monitor and echo this.
2)Write a program which displays elapsed time on the serial monitor. Your program should print a message of the form:
Elapsed time - Hours: xx Minutes: yy
At one minute intervals.
Estimate the accuracy of your timer by letting it run for a few hours and checking it against an accurate clock.
As i said it needs to be handed in on friday so it's quite urgent. I know a fair bit of C but i can't get my head around this language or where to even start.
edit: I can write this up in less then ten minutes, you have tell Friday so you have plenty of time. I have faith in you, and that you're smart enough to figure this out on your own.
// Time is money
// Made by turbo910
int yy = 0;
int xx = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
for (int i = 0; i<59;i++){
delay(1000);
}
yy = yy + 1;
if (yy >60){
xx = xx + 1;
yy = 0;
}
Serial.print("Elapsed time - Hours: ");
Serial.print(xx);
Serial.print(" Minutes: ");
Serial.println(yy);
}
Nono i wasnt looking for you to do my homework just help me out a bit on what i should be looking at, and well im sorry but i cant do it in ten minutes i find it difficult.
Btw fletcher your code doesnt seem to do anything at all :s
Sure it does. Just waite (roughly ) 1 min for the first output.
and fletcher your a life saver thanks.
... no I'm not. The code is flawed :roll_eyes:
Since I got you into this I might as well get you out.
Using a 1 sec delay 60 times (or rather 59 times in the code) is not the right way to time 1 min since it does not take into account the time it takes to go though your loop.
This is the right way:
There is a function that will return the milliseseconds since the Arduino was started: millis()
This value is a unsinged int.
1) If more that 1 min has elapsed since last print (compare time for last print will millis()). If yes procede to 2)
2) calculate the hrs and min based on millis(). Do a print of the values.
3) Store the time for this print so you have somthing to compare with during 1).
Try to look at this code. It should print the milliseconds since the Arduino was started ever second … it compiles but I have no hardware to test it on at the moment… :~
unsigned long myTime = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
if (millis() - myTime < 0){
myTime = myTime + 1000;
Serial.print("Milliseconds since start: ");
Serial.println(millis());
}
}
This should get you on the right track.
-Fletcher
Update:
Code had an error
This works:
unsigned long myTime = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
if ((myTime - millis()) <= 0){
myTime = myTime + 1000;
Serial.print("Milliseconds since start: ");
Serial.println(millis());
Serial.print(myTime);
}
}
however this won't give you exactly what you want because if you have 2 minutes you'll have 120 seconds and 120000 milliseconds
so I'm going to leave that up to you too figure out. if you can't figure it out by the end of the night, then post your code and the things you've tried. and then I'll help you the rest of the way.
there is a difference between wanting help and wanting a handout.
trial and error is essential to learning any programing language I don't know any programmer that hasn't learned from trial and error. so if you show me that you're trying then I have no problem helping you out.
Going by: myTime = millis()
is probaly a better idea.
Had this been a micros() timer like: myTime = micros()
there would be a slowly increase in the time for printing.
turbo910:
Once you get your code working you will have another question. The answer to that question can be found here: http://arduino.cc/en/Reference/Modulo