Go Down

Topic: Summer Homework -need some help fast ! (Read 360 times) previous topic - next topic

TraianGrg

Ok so i really really need someone to write a piece of code for me, that was supposed to be my homework for the entire summer and now i am out of time and it's not that easy learning code in like 3 days. If you are not the type that would help people in this case, please ignore this post and return to whatever you did before. If you are a nice guy willing to help a stranger out, help me with this staff please.

Sooooo i have been trying to set up a count up/down timer in arduino uno, and i feel like each time i change something in my code i make it even worse. The thing is that i need it to run count up/down without a clock (i need it tomorrow 9 A.M. i can't get a clock in that time), for more than hours. It has to include days months and years, or at least days in a 4 digit format (like 3650 days, and you know it's 10 years or so), and while doing that constantly checking on 3 voltmeters ( 30V max, using 2 resistors <10k and 100k> to create a voltage divider, on analog 1,2,3) and 2 ammeters. There is a second part of the code including some LEDs and a TFT LCD to display all that data on, but if it's possible just do it with Serial.print () and i will try to do the rest. This is the code i wrote/copied from the internet so far, it doesn't really work.



Code: [Select]
unsigned long Watch, _micro, time = micros();
unsigned int Clock = 0, R_clock;
boolean Reset = false, Stop = false, Paused = false, _type;
volatile boolean timeFlag = false;

#define DOWN 0
#define UP 1

void setup()
{
  Serial.begin(9600);
  SetTimer(0,23,59,59); // 10 seconds
  StartTimer();
}

void loop()
{
  CountUpDownTimer(DOWN); // run the timer

  // this prevents the time from being constantly shown.
  if (TimeHasChanged() )
    Serial.print(ShowDays());
    Serial.print(" days");
    Serial.print("   ");
    Serial.print(ShowHours());
    Serial.print(":");
    Serial.print(ShowMinutes());
    Serial.print(":");
    Serial.print(ShowSeconds());
    Serial.print(" ");
    delay(4000);

    // This DOES NOT format the time to 0:0x when seconds is less than 10.
    // if you need to format the time to standard format, use the sprintf() function.
  }


boolean CountUpDownTimer(boolean Type)
{
  _type = Type;
  static unsigned long duration = 1000000; // 1 second
  timeFlag = false;

  if (!Stop && !Paused) // if not Stopped or Paused, run timer
  {
    // check the time difference and see if 1 second has elapsed
    if ((_micro = micros()) - time > duration )
    {
      _type == UP? Clock++ : Clock--;
      timeFlag = true;

      if (_type == DOWN && Clock == 0) // check to see if the clock is 0
        Stop = true; // If so, stop the timer

     // check to see if micros() has rolled over, if not,
     // then increment "time" by duration
      _micro < time ? time = _micro : time += duration;
    }
  }
  return !Stop; // return the state of the timer
}

void ResetTimer()
{
  if(_type)
    Clock = 0;
  else
    SetTimer(R_clock);

  Stop = false;
}

void StartTimer()
{
  Watch = micros(); // get the initial microseconds at the start of the timer
  Stop = false;
  Paused = false;
  if(_type == UP)
    Clock = 0;
}

void StopTimer()
{
  Stop = true;
}

void StopTimerAt(unsigned int days, unsigned int hours, unsigned int minutes, unsigned int seconds)
{
  if (TimeCheck(days, hours, minutes, seconds) )
    Stop = true;
}

void PauseTimer()
{
  Paused = true;
}

void ResumeTimer() // You can resume the timer if you ever stop it.
{
  Paused = false;
}

void SetTimer(unsigned int days, unsigned int hours, unsigned int minutes, unsigned int seconds)
{
  // This handles invalid time overflow ie 1(H), 0(M), 120(S) -> 1, 2, 0
  unsigned int _H = (seconds / 3600),_S = (seconds / 60), _M = (minutes / 60);
  if(_S) minutes += _S;
  if(_M) hours += _M;
  if(_H) days += _H;
  Clock = (days * hours*23 + hours * 3600) + (minutes * 60) + (seconds % 60);
  R_clock = Clock;
  Stop = false;
}

void SetTimer(unsigned int seconds)
{
 // StartTimer(seconds / 3600, (seconds / 3600) / 60, seconds % 60);
 Clock = seconds;
 R_clock = Clock;
 Stop = false;
}

int ShowDays ()
{
  return Clock / 86400;
 
}
int ShowHours()
{
  return Clock / 3600;
}

int ShowMinutes()
{
  return (Clock / 60) % 60;
}

int ShowSeconds()
{
  return Clock % 60;
}

unsigned long ShowMilliSeconds()
{
  return (_micro - Watch)/ 1000.0;
}

unsigned long ShowMicroSeconds()
{
  return _micro - Watch;
}

boolean TimeHasChanged()
{
  return timeFlag;
}

// output true if timer equals requested time
boolean TimeCheck(unsigned int days, unsigned int hours, unsigned int minutes, unsigned int seconds)
{
  return (days == ShowDays() && hours == ShowHours() && minutes == ShowMinutes() && seconds == ShowSeconds());
}



*It's first year in college, i spent my summer drunk on the beach and someone reminded me that we are supposed to do this box with LED's and a display counter on it that tells what led get's what voltage and after 4 years he will give it back and it will serve as the first memory made in college. Help me people, i don't wan't to f**k it up day 1. Thank you in advance !

6v6gt

int or unsigned int is too small for the big numbers. This appears to work:

Code: [Select]

unsigned long Watch, _micro, time = micros();
unsigned long Clock = 0, R_clock;
boolean Reset = false, Stop = false, Paused = false, _type;
volatile boolean timeFlag = false;

#define DOWN 0
#define UP 1

void setup()
{
  Serial.begin(9600);
  SetTimer(3999, 23, 59, 59); //  <<<<<<<<<<<<<<<<<<<<<<<<<<<
  StartTimer();
}

void loop()
{
  CountUpDownTimer(DOWN); // run the timer

  // this prevents the time from being constantly shown.
  if (TimeHasChanged() ) {
    Serial.print(ShowDays());
    Serial.print(" days");
    Serial.print("   ");
    if( ShowHours() < 10 ) Serial.print( '0' ) ;
    Serial.print(ShowHours());
    Serial.print(":");
    if( ShowMinutes() < 10 ) Serial.print( '0' ) ;
    Serial.print(ShowMinutes());
    Serial.print(":");
    if( ShowSeconds() < 10 ) Serial.print( '0' ) ;
    Serial.print(ShowSeconds());
    Serial.println(" ");
    // delay(4000);

    // This DOES NOT format the time to 0:0x when seconds is less than 10.
    // if you need to format the time to standard format, use the sprintf() function.
  }
}

boolean CountUpDownTimer(boolean Type)
{
  _type = Type;
  static unsigned long duration = 1000000; // 1 second
  timeFlag = false;

  if (!Stop && !Paused) // if not Stopped or Paused, run timer
  {
    // check the time difference and see if 1 second has elapsed
    if ((_micro = micros()) - time > duration )
    {
      _type == UP ? Clock++ : Clock--;
      timeFlag = true;

      if (_type == DOWN && Clock == 0) // check to see if the clock is 0
        Stop = true; // If so, stop the timer

      // check to see if micros() has rolled over, if not,
      // then increment "time" by duration
      _micro < time ? time = _micro : time += duration;
    }
  }
  return !Stop; // return the state of the timer
}

void ResetTimer()
{
  if (_type)
    Clock = 0;
  else
    SetTimer(R_clock);

  Stop = false;
}

void StartTimer()
{
  Watch = micros(); // get the initial microseconds at the start of the timer
  Stop = false;
  Paused = false;
  if (_type == UP)
    Clock = 0;
}

void StopTimer()
{
  Stop = true;
}

void StopTimerAt(unsigned long days, unsigned long hours, unsigned long minutes, unsigned long seconds)
{
  if (TimeCheck(days, hours, minutes, seconds) )
    Stop = true;
}

void PauseTimer()
{
  Paused = true;
}

void ResumeTimer() // You can resume the timer if you ever stop it.
{
  Paused = false;
}

void SetTimer(unsigned long days, unsigned long hours, unsigned long minutes, unsigned long seconds)
{
  // This handles invalid time overflow ie 1(H), 0(M), 120(S) -> 1, 2, 0
  unsigned long _H = (seconds / 3600), _S = (seconds / 60), _M = (minutes / 60);
  if (_S) minutes += _S;
  if (_M) hours += _M;
  if (_H) days += _H;
  //f Clock = (days * hours * 23 + hours * 3600) + (minutes * 60) + (seconds % 60);
  Clock = (days * 24UL * 3600UL) + ( hours * 3600UL) + (minutes * 60UL) + (seconds % 60UL);
  R_clock = Clock;
  Stop = false;
}

void SetTimer(unsigned long seconds)
{
  // StartTimer(seconds / 3600, (seconds / 3600) / 60, seconds % 60);
  Clock = seconds;
  R_clock = Clock;
  Stop = false;
}

unsigned long ShowDays ()
{
  return Clock / 86400;

}
unsigned long  ShowHours()
{
  return (Clock / 3600) % 24 ;
}

unsigned long  ShowMinutes()
{
  return (Clock / 60) % 60;
}

unsigned long  ShowSeconds()
{
  return Clock % 60;
}

unsigned long ShowMilliSeconds()
{
  return (_micro - Watch) / 1000.0;
}

unsigned long ShowMicroSeconds()
{
  return _micro - Watch;
}

boolean TimeHasChanged()
{
  return timeFlag;
}

// output true if timer equals requested time
boolean TimeCheck(unsigned long days, unsigned long hours, unsigned long minutes, unsigned long seconds)
{
  return (days == ShowDays() && hours == ShowHours() && minutes == ShowMinutes() && seconds == ShowSeconds());
}

PaulMurrayCbr

#2
Oct 03, 2017, 05:37 am Last Edit: Oct 03, 2017, 04:58 pm by PaulMurrayCbr
Ok so i really really need someone to write a piece of code for me, that was supposed to be my homework for the entire summer and now i am out of time and it's not that easy learning code in like 3 days. If you are not the type that would help people in this case, please ignore this post and return to whatever you did before. If you are a nice guy willing to help a stranger out, help me with this staff please.
Despite your asking me to not reply, I'll reply anyway. Because I literally laughed out loud. And when I say literally, I mean spell-out-LOL-the-long-way literally.

*It's first year in college, i spent my summer drunk on the beach and someone reminded me that we are supposed to do this box with LED's and a display counter on it that tells what led get's what voltage and after 4 years he will give it back and it will serve as the first memory made in college. Help me people, i don't wan't to f**k it up day 1. Thank you in advance !
Dude, you have already f**ked it up day 1. The point of these assignments is not to have an assignment ready for the prof, the point of them is that doing them steps you through the process of learning stuff. Your prof sounds like a chill dude - the whole "you get it back in four years" is a really nice touch. I mean - the guy is comitting to storing his student's projects for four freaking years. He cares, you know?

You would be doing your future self a favour if you came clean, agreed on - say - points off your mark for handing it in late, and actually did the work involved in building this thing.

But if, as you say, you learned nothing at all last semester and would have to learn to code in 3 days … tough titty, man. By all means cheat on your assignments. I don't have a degree, so the job market being flooded with guys with degrees who can't actually code works out great for me. Just so long as you are not working in my section and I'm supposed to rely on you doing your job.

http://paulmurraycbr.github.io/ArduinoTheOOWay.html

Go Up