简体   繁体   中英

Program help on Arduino Uno

So I was building this: http://www.instructables.com/id/Automatically-water-your-small-indoor-plant-using-/?ALLSTEPS

Problem: I set the water time to 5 min and the wait time to 1 hour, the water still kept running and hasn't stop after 5 min.

Is there another way to write this program?

The program he provide was

int motorPin = A0;
int blinkPin = 13;

int watertime = 300000; // how long to water in miliseconds
int waittime = 3600000; // how long to wait between watering

void setup()
{
    pinMode(motorPin, OUTPUT);
    pinMode(blinkPin, OUTPUT);
}

void loop()
{
    digitalWrite(motorPin, HIGH);
    digitalWrite(blinkPin, HIGH);
    delay(watertime);
    digitalWrite(motorPin, LOW);
    digitalWrite(blinkPin, LOW);
    delay(waittime);
}

Arduino UNO maximum integer value is 32767 (16-bit signed integer). So both watertime and waittime are too large to store in int variables. Try slowing the timebase by using delay(1000) to control a loop that runs once every second, then express watertime and waittime using seconds instead of miliseconds.

Incidentally, there is a stackoverflow site that specializes in Arduino: https://arduino.stackexchange.com/

Arduino UNO integers are 16bit wide, just use unsigned long variables or a #define.

#define watertime 300000
#define waittime 3600000

// some code...
delay(watertime);
// more code...
delay(waittime);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM