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

Matthewdupreez

Legendary member
temp control, humidity control, and lcd monitoring are all good, it's just to incorporate the switches and servos (with the delay) into the code...
 

Matthewdupreez

Legendary member
this is the code that is working so far.....
at the moment it is just using a delay for the length of time the motor must run for....
prefeably it should monitor the leftsw and rightsw so that when one of those switches are pressed it will stop turning...

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#include <DHT.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Servo.h>
Servo myservo;
//declare global variables
float dt, counter;
unsigned long current_time, prev_time;
bool toggle = 0;
#define heat 4
#define leftsw 5
#define rightsw 6
#define alarm 7
#define humid 8

//
void setup() {
Serial.begin(9600);
sensors.begin();
dht.begin();
lcd.begin();
lcd.backlight();
myservo.attach(9);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);

}


void temp()
{

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

}
}

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

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

}
}

void loop() {
prev_time = current_time; //set previous loop prev_time to current_time
current_time = micros(); //measure the current time
dt = (current_time - prev_time)/1000000.0; //calculate the change in time from the previous time this line was run until now, in seconds

counter = counter + dt; //add dt to the counter to count the time

if (counter >= 7200)
{
}

counter = 0; //reset the counter
if (toggle == 0)
{
toggle = 1;
myservo.attach(9);
myservo.write(180);
delay(200);// need to monitor switches here, must turn until right switch on. or use a delay....
myservo.detach();

}
else {
toggle = 0;
myservo.attach(9);
myservo.write(0);
delay(200);// need to monitor switches here, must turn until left switch on.
myservo.detach();
}

sensors.requestTemperatures(); // Send the command to get temperature readings

lcd.clear();
lcd.setCursor(0,0);
lcd.print (sensors.getTempCByIndex(0), " *");
lcd.setCursor(0,1);
lcd.print(dht.readHumidity(), " %");
temp();
humidity();

if (sensors.getTempCByIndex(0) >= 40 || sensors.getTempCByIndex(0) <= 35)
{
digitalWrite(alarm, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ALERT TEMP!");
lcd.setCursor(0,1);
lcd.print(sensors.getTempCByIndex(0), "*");
}


}
 

FlamingRCAirplanes

Elite member
this is the code that is working so far.....
at the moment it is just using a delay for the length of time the motor must run for....
prefeably it should monitor the leftsw and rightsw so that when one of those switches are pressed it will stop turning...

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#include <DHT.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Servo.h>
Servo myservo;
//declare global variables
float dt, counter;
unsigned long current_time, prev_time;
bool toggle = 0;
#define heat 4
#define leftsw 5
#define rightsw 6
#define alarm 7
#define humid 8

//
void setup() {
Serial.begin(9600);
sensors.begin();
dht.begin();
lcd.begin();
lcd.backlight();
myservo.attach(9);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);

}


void temp()
{

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

}
}

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

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

}
}

void loop() {
prev_time = current_time; //set previous loop prev_time to current_time
current_time = micros(); //measure the current time
dt = (current_time - prev_time)/1000000.0; //calculate the change in time from the previous time this line was run until now, in seconds

counter = counter + dt; //add dt to the counter to count the time

if (counter >= 7200)
{
}

counter = 0; //reset the counter
if (toggle == 0)
{
toggle = 1;
myservo.attach(9);
myservo.write(180);
delay(200);// need to monitor switches here, must turn until right switch on. or use a delay....
myservo.detach();

}
else {
toggle = 0;
myservo.attach(9);
myservo.write(0);
delay(200);// need to monitor switches here, must turn until left switch on.
myservo.detach();
}

sensors.requestTemperatures(); // Send the command to get temperature readings

lcd.clear();
lcd.setCursor(0,0);
lcd.print (sensors.getTempCByIndex(0), " *");
lcd.setCursor(0,1);
lcd.print(dht.readHumidity(), " %");
temp();
humidity();

if (sensors.getTempCByIndex(0) >= 40 || sensors.getTempCByIndex(0) <= 35)
{
digitalWrite(alarm, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ALERT TEMP!");
lcd.setCursor(0,1);
lcd.print(sensors.getTempCByIndex(0), "*");
}


}
Yep. I am working on that now.
 

Matthewdupreez

Legendary member
this might work? the ide says the code is fine... but it might not work properly irl
-----------------------------------------
void loop() {
prev_time = current_time; //set previous loop prev_time to current_time
current_time = micros(); //measure the current time
dt = (current_time - prev_time)/1000000.0; //calculate the change in time from the previous time this line was run until now, in seconds

counter = counter + dt; //add dt to the counter to count the time

if (counter >= 7200)
{
}

counter = 0; //reset the counter
if (toggle == 0)
{
toggle = 1;

while (rightsw < 400)
{
myservo.attach(9);
myservo.write(180);
}
/*else if(rightsw > 500)
{
*/myservo.detach();/*
}
*/
}
else {
toggle = 0;
while (leftsw < 400)
{
myservo.attach(9);
myservo.write(0);
}
/*else if(rightsw > 500)
{
*/myservo.detach();/*
}
*/
}
 

Matthewdupreez

Legendary member
complete code (maybe)

-------------------------------------------------------------
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#include <DHT.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Servo.h>
Servo myservo;
//declare global variables
float dt, counter;
unsigned long current_time, prev_time;
bool toggle = 0;
#define heat 4
#define leftsw 5
#define rightsw 6
#define alarm 7
#define humid 8

//
void setup() {
Serial.begin(9600);
sensors.begin();
dht.begin();
lcd.begin();
lcd.backlight();
myservo.attach(9);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);

}


void temp()
{

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

}
}

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

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

}
}

void loop() {
prev_time = current_time; //set previous loop prev_time to current_time
current_time = micros(); //measure the current time
dt = (current_time - prev_time)/1000000.0; //calculate the change in time from the previous time this line was run until now, in seconds

counter = counter + dt; //add dt to the counter to count the time

if (counter >= 7200)
{
}

counter = 0; //reset the counter
if (toggle == 0)
{
toggle = 1;

while (rightsw < 400)
{
myservo.attach(9);
myservo.write(180);
}
/*else if(rightsw > 500)
{
*/myservo.detach();/*
}
*/
}
else {
toggle = 0;
while (leftsw < 400)
{
myservo.attach(9);
myservo.write(0);
}
/*else if(rightsw > 500)
{
*/myservo.detach();/*
}
*/
}

sensors.requestTemperatures(); // Send the command to get temperature readings

lcd.clear();
lcd.setCursor(0,0);
lcd.print (sensors.getTempCByIndex(0), " *");
lcd.setCursor(0,1);
lcd.print(dht.readHumidity(), " %");
temp();
humidity();

if (sensors.getTempCByIndex(0) >= 40 || sensors.getTempCByIndex(0) <= 35)
{
digitalWrite(alarm, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ALERT TEMP!");
lcd.setCursor(0,1);
lcd.print(sensors.getTempCByIndex(0), "*");
}


}
 

JasonK

Participation Award Recipient
that whole while loop idea completely breaks your 'do 2+ things at once' initial request. Which is why you want to do some sort of FSM with changes
 

Matthewdupreez

Legendary member
that whole while loop idea completely breaks your 'do 2+ things at once' initial request. Which is why you want to do some sort of FSM with changes
that is true... but if it only halts the system for about 2 seconds (time it take to rotate egg trays by 60 degrees, i don't think it would really harm the eggs....
 

FlamingRCAirplanes

Elite member
Going good so far.
P1100074.JPG