Help

Hello to everyone, I am designing a circuit project that required time delay to turn off a DC motor with arduino, when a switch connected to a digital pin is pressed which bridged the pin to +5v, there would be a time delay of about 1 hour before the motor is powered off. I can easily program with delay() but it will pause the entire code, so I want someone in his/her good offices to help me translate this delay() code to millis() code. Below is the code

Const int switchpin = 13; // switch connected to +5v regulator and to DP 13
Const int motorpin = 11; // DP 11 drives motor through tip122

Void setup() {
Pinmode(switchpin, INPUT);
Pinmode(motorpin, OUTPUT);
}

Void loop() {
If(digitalRead(switchpin) ==0)
Digitalwrite(motorpin, HIGH);
Else if (digitalRead(switchpin) ==1)
Delay(60000); // allow motor to run for an hour before turning it off
Digitalwrrite(motorpin, LOW);
}

Please I need this code in millis() function in stead of delay() function. Notice that when the +5v is applied to DP 13, it allowed motor to still run for approximately an hour before it shut the motor down. Thanks in advance

Help, I need somebody
Help, not just anybody
Help, you know I need someone, help!!!

Aka, give your thread a useful name :wink:

Next, the compiler IS case sensitive :wink:

Ikennaj:
a switch connected to a digital pin is pressed which bridged the pin to +5v

Do you have external pull downs installed? It's easier if you connect the button to GND and use the internal pull ups :wink:

Ikennaj:
help me translate this delay() code to millis() code.

Help, of course. Do it, no... What have you tried?

Hint: Look at the "Blink without delay" example and the "State change" example (or use a library like Bounce2 to do the work for you).

PS Code indentation is a great tool for you to make code a heck lot more readable :wink:

Mind you, he did use "code" tags in the first place. Kudos for that! :sunglasses:

True, unfortunately for uncompilable code :frowning:

Ya just can't have it all! :grinning:


+1 for the Beatles reference.

Yes. The reason for giving your post a meaningful name, is so when somebody else needs Help with that topic, they are more likely to find this thread, and get the Help they need.

And do us all a favor and read: How To Use This Forum

That said, perhaps this is the Help you seek:

/*****************************************************
 *            1 Hour Motor Off Delay
 *          
 *         This code assumes Arduino UNO
 *  
 *       !!!!!!!!!!!!  UNTESTED  !!!!!!!!!!!!
 ******************************************************/

#define switchpin 13 // switch connected to ground and to DP 13
#define motorpin 11  // DP 11 drives motor through tip122

const unsigned long DURATION_HOUR = 3600000;  // Number milliseconds in an hour

// Define these here, so if the "logic" changes, you only have to adjust the code in one place [here].
// For instance if you insist on connecting that button to +5, instead of to ground, then  
// SW_PRESSED would be 1, instead of 0, etc.
// Also, I guessing at the states of MOTOR_OFF & MOTOR_ON, but, you said TIP122, so it's probably right.
const byte MOTOR_OFF  = 0;
const byte MOTOR_ON   = 1;
const byte SW_PRESSED = 0;
const byte SW_OPEN    = 1;

unsigned long duration = 0;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;

boolean was_switch_pressed = false;

void setup() {
  pinMode(switchpin, INPUT_PULLUP);
  pinMode(motorpin, OUTPUT);

  digitalWrite(motorpin, MOTOR_ON); // I'm assuming... otherwise, what's the point.
  previousMillis = millis();  // Capture this here, as an initialization
}

void loop() {
  // Listen for switch press, and capture state if so.
  // This would better be done with an interrupt, but that would mean the need to 
  // move the button/switch to pin 2, or pin 3. 
  if (digitalRead(switchpin) == SW_PRESSED)
  {
    // No need for a debounce, since this is a trigger event that is only captured 
    // once [i.e. on the first instance]
    was_switch_pressed = true;
  }

  if (was_switch_pressed)
  {
    currentMillis = millis();
    if(currentMillis - previousMillis > duration) 
    {
      // It's been an hour, so turn the motor off!
      digitalWrite(motorpin, MOTOR_OFF);
   
      // Hide in an endless loop.  The reasoning here is, the OP said nothing about turning the
      // motor back on, so desired functionality is unknown at this point ;)
      // Also, it might be a Warm Fuzzy, if an Indicator, of some sort, was presented, to let the
      // poor sot, who pressed the button, know that, indeed, something actually happened, and 
      // the motor is, indeed, destined, after an hour, to actually turn off -- just saying.
      while(1) {;}
    }
  }
  else
  {
    // Place concurrent code here [i.e. code that will be run while waiting for a Motor Turn Off Timeout sequence]
 
  }
}

Which Arduino

#define switchpin 13
pinMode(switchpin, INPUT_PULLUP); // works on some Arduinos, but not on others.

Small Arduinos use an unbuffered LED on pin13, so you can't use it for a switch with internal pull up.
Leo..

Void setup() {

VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

Paul__B:
+1 for the Beatles reference.

I knew I had seen something similar before. This one even had a picture:

not_a_noob:
When I was younger, so much younger than todaaaayyyy, I never needed anybody's help in any way....