Programm shuts down after a random amount of time.


hey guys,

i'm finish master in fine arts, made serious mistake.
i have arduino controlling small motor. motor supposed run 1min, stop 3min or, 4 min.

the mistake did, overwrote working code old one. problem old code is, although motor seems run fine, know there chance, code stop working after random amount of time (it did year ago , had programmer @ school on it. found little mistake , corrected it, but, said, accidentely deleted corrected version).

so, i'm asking guys huge favour... please on code , tell me, couse error? have done in 3 days , object running in musuem, can't can't can't stop working...


a thousand already.

-maikell

here's code:
code: [select]

#define motor 9
void setpwmfrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if(pin == 5 || pin == 6) {
      tccr0b = tccr0b & 0b11111000 | mode;
    } else {
      tccr1b = tccr1b & 0b11111000 | mode;
    }
  } else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x7; break;
      default: return;
    }
    tccr2b = tccr2b & 0b11111000 | mode;
  }
}

void setup()
{
  analogwrite(motor,0); 
  pinmode(13,output); 
  digitalwrite(13,true);
  serial.begin(115200);
}

void loop()
{
  analogwrite(motor,130);
  delay(getmotortimeon());
  analogwrite(motor,0);
  delay(getmotortimeoff());
}

long getmotortimeon()
{
  int rand = random(0, 1);
  if (rand == 0) {
    return (38*1000l);
  } 
  if (rand == 1) {
    return (48*1000l);
  } 
}

long getmotortimeoff()
{
  int rand = random(0, 4);
  if (rand == 0 || rand == 1) {
    return (3*60*1000l);
  } 
  if (rand == 2 || rand == 3) {
    return (3*60*1000l);
  } 
  if (rand == 4) {
    return (3*60*1000l);
  }   
 
  return (60*1000l); 

you seem missing closing } @ end of file prevent sketch compiling.

as far can see, setpwmfrequency() unused. that's because have no idea whether it's correct.

the upper bound of random() exclusive, random(0, 1) returns value in range 0 ... 0. not useful; in getmotortimeon() want random(0,2);

also in getmotortimeon() should use if/else because if random value isn't 0 can assume 1. (as is, if random() ever returned value other 0 or 1 motor time on undefined.

similarly in getmotortimeoff() call random(0, 4) return value in range 0 ...3 code handle 4 or greater never execute. means motor time off 3 minutes. that's not meant.

the code may not meant, don't see here cause 'stop working' (whatever means). expect turn motor on 38 seconds , turn off 180 seconds, continuously, long powered up.



Arduino Forum > Using Arduino > Programming Questions > Programm shuts down after a random amount of time.


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support