New guy

Hey guys

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.

Please help!

Doing your homework for you … isn’t that cheating?

if you used your brain as much as you type you would have this done by now... srsly

you know a fair bit of C yet you can't do this on your own?

why don't you try reading these pages first

http://arduino.cc/en/Reference/HomePage

http://arduino.cc/en/Reference/Serial

http://arduino.cc/en/Reference/Millis

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.

I wonder if it's one of Massimo's students - he does read these threads occasionally :P

If you search the post titles for serial going back a couple of days, you will probably find some example code I've posted that might be useful.

uhhh - I love doing other peoples homework $)

This should get you though the night:

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

:astonished:

-Fletcher

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.

and fletcher your a life saver thanks.

Any other comments will still be appreciated

thank you

Btw fletcher your code doesnt seem to do anything at all :s

Hi

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

-Fletcher

Thanks for all your help fletcher,

could you explain the last three points in a bit more detail please

thanks

Hi turbo910

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 :disappointed_relieved:
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);
  }
}

Hey fletcher sorry to be a pain but i still dont have a clue what your on about :s

This is how I would write it with a variable to control delay

unsigned long myTime = 0;
long timeDelay = 1000;

void setup(){
  Serial.begin(9600);
}

void loop(){
  if (millis() - myTime >= timeDelay){
     myTime = millis();
     Serial.println(myTime/1000);

  }
}

Turbo910, I'm not above helping you, but you have to do us a favor and help yourself.

and don't lie, you say you know a "fair bit of C" yet this is all alien to you?

did you read this page? http://arduino.cc/en/Reference/Millis

you can't get any simpler, millis() is a function of the Arduino that will "Return number of milliseconds since the program started"

I'll help you with some of the time

milliseconds / 1000 = second milliseconds /(1000*60 = minute milliseconds /(1000*60*60) = hour

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.

cheers, Dave.

Hi

SilverCG:

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

-Fletcher