Struggling getting arduino to do "2" thing at "once"

Matthewdupreez

Legendary member
Yes but over time that little delay will turn into a big delay... Like the turning time will become hours off.
but if the timer gets reset every 2 hours, then it should be turning every 12o minutes and 3 seconds....
obviously it would slowly start to change the time that the turns start at, but they will always be 120min 3seconds apart...
eg: if it is turning at 12 am sharp today... in a months time it will be executing that cycle at, 12:01 (&1/2) ... but still at 120 minute intervals
 

FlamingRCAirplanes

Elite member
Code so far.

#include <OneWire.h>
#include <DallasTemperature.h>
//#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <MyDelay.h>

#define heat 4
#define leftsw 5
#define rightsw 6
#define alarm 7
#define humid 8
#define ONE_WIRE_BUS 3
//#define DHTPIN 2 // Digital pin connected to the DHT sensor
//#define DHTTYPE DHT11 // DHT 11

MyDelay TurnDelay (5000);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;
//DHT dht(DHTPIN, DHTTYPE);

//declare global variables
float dt, counter;
unsigned long current_time, prev_time;
bool toggle = false;
bool busy = false;
float Temp;
bool delaystarted = false;


void temp()
{

if (sensors.getTempCByIndex(0) < 37)
{
digitalWrite (heat, LOW);
}
else if (sensors.getTempCByIndex(0) >= 38)
{
digitalWrite (heat, HIGH);
}
}

void checktemp()
{
lcd.setCursor(0, 1);
sensors.requestTemperatures(); // Send the command to get temperature readings
Temp = sensors.getTempCByIndex(0);
lcd.print(Temp);
lcd.print("* ");
if (Temp >= 20 && Temp <= 30)
{
digitalWrite(alarm, LOW);
lcd.setCursor(0, 0);
lcd.print("ALERT TEMP! ");
}
else {
lcd.setCursor(0, 0);
lcd.print(" .");
}
}

void rotate()
{
toggle = true;
if(digitalRead(rightsw) == LOW)
{
myservo.write(180);
busy = true;
rotate();
}
else if (digitalRead(rightsw) == HIGH)
{
myservo.write(90);
busy = false;
}

/*else {
toggle = false;
myservo.attach(9);
myservo.write(0);
delay(200);// need to monitor switches here, must turn until left switch on.
myservo.detach();
}*/
}
void setup() {
Serial.begin(9600);
sensors.begin();
lcd.init();
lcd.backlight();
myservo.attach(9);
myservo.write(90);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
lcd.clear();
checktemp();

}

void loop() {
// put your main code here, to run repeatedly:
delay(500);
checktemp();
temp();
Serial.println(digitalRead(rightsw));
if (delaystarted == false && busy == false)
{
TurnDelay.start();
delaystarted = true;
}
if (TurnDelay.update())
{
delaystarted = false;

rotate();
TurnDelay.stop();
}
}
 

JasonK

Participation Award Recipient
how do you make sure your continuous servo is actually not moving when you want it to stop?
 

JasonK

Participation Award Recipient
When we set it up we set it up to be at the middle pos 90deg. Then we fine tune it after install.
yah, over 2 hours, you would need that very finely tune... or hope that there is a bit of an effective dead zone in the servo driver.

And because this is an 'analog'/real part, it has thermal variations, so temperature changes will change (ever so slightly) the 'zero' point.
 

FlamingRCAirplanes

Elite member
OK!!!! Try this out. It should work...


#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <MyDelay.h>

#define heat 4
#define leftsw 5
#define rightsw 6
#define alarm 7
#define humid 8
#define ONE_WIRE_BUS 3
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11

MyDelay TurnDelay (5000);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;
DHT dht(DHTPIN, DHTTYPE);

//declare global variables
float dt, counter;
unsigned long current_time, prev_time;
bool toggle = false;
bool busy = false;
float Temp;
float Humidity;
bool delaystarted = false;

void humidity()
{
if (dht.readHumidity() < 65)
{
digitalWrite(humid, LOW);
}

if (dht.readHumidity() >= 70)
{
digitalWrite(humid, HIGH);

}
}

void temp()
{

if (sensors.getTempCByIndex(0) < 30)
{
digitalWrite (heat, LOW);
}
else if (sensors.getTempCByIndex(0) >= 30)
{
digitalWrite (heat, HIGH);
}
}

void checktemp()
{
lcd.setCursor(0, 1);
sensors.requestTemperatures(); // Send the command to get temperature readings
Temp = sensors.getTempCByIndex(0);
lcd.print(Temp);
lcd.print("* ");
if (Temp >= 20 && Temp <= 30)
{
digitalWrite(alarm, LOW);
lcd.setCursor(0, 0);
lcd.print("ALERT TEMP! ");
}
}
void checkhumidity()
{
lcd.setCursor(0, 0);
Humidity = dht.readHumidity();
lcd.print(Humidity);
lcd.print("% ");
if (Humidity >= 20 && Humidity <= 30)
{
digitalWrite(alarm, LOW);
lcd.setCursor(0, 1);
lcd.print("ALERT HUMIDITY ");
}
}

void rotate()
{
if (toggle == false)
{
if (digitalRead(rightsw) == LOW)
{
myservo.write(180);
busy = true;

}
else if (digitalRead(rightsw) == HIGH)
{
toggle = true;
myservo.write(90);
busy = false;
}
}
else if (toggle == true)
{
if (digitalRead(leftsw) == LOW)
{
myservo.write(0);
busy = true;

}
else if (digitalRead(leftsw) == HIGH)
{
toggle = false;
myservo.write(90);
busy = false;
}
}
}
void setup() {
Serial.begin(9600);
sensors.begin();
lcd.init();
lcd.backlight();
myservo.attach(9);
myservo.write(90);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
lcd.clear();
checktemp();
toggle = false;

}

void loop() {
// put your main code here, to run repeatedly:
checktemp();
temp();
humidity();
checkhumidity();
Serial.println(digitalRead(leftsw));
if (delaystarted == false && busy == false)
{
TurnDelay.start();
delaystarted = true;
}
if (TurnDelay.update())
{
delaystarted = false;

rotate();
TurnDelay.stop();
}
if (busy == true)
{
rotate();
}
}