Hello, I'm trying to write a code that prints voltage and time data with a delay(X) but I also have a stepper motor connected to the circuit with a desired delay(Y) and delay(Z). I'm not sure how to approach this problem because right now the delays stack up so I'm seeing the data being printed with a delay(X+Y+Z). Here is the code I'm dealing with right now:
#include <Servo.h>
Servo myservo;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
myservo.attach(13); // Pin input for servo motor
}
// the loop routine runs over and over again forever:
void loop() {
unsigned long time;
time = millis();
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
///////////////////////////////////////////////////////////////////////
Serial.print(time);
Serial.print(',');
Serial.println(voltage);
delay(10);
////////////////////////////////////////////////////////////////////
//STEPPER SERVO CONTROL CODE
myservo.writeMicroseconds(0);
delay(1500);
myservo.writeMicroseconds(2700);
delay(1500);
}
#include <Servo.h>
Servo myservo;
const unsigned long TaskAppleWait = 1500UL; //Runs every 1.5 Sec
const unsigned long TaskOrangeWait = 1500UL; //Runs every 1.5 Sec
//add more as needed
unsigned long TimeApple; //Times up, run TaskApple
unsigned long TimeOrange; //Times up, run TaskOrange
//add more as needed
unsigned long currentmillis;
//other variables
//define the available states that we can have for this sketch
enum States{
stateStart, stateApple, stateOrange};
//add more states as needed
States mState = stateStart; //we start out in this machine state
//==========================================================
void setup()
{
Serial.begin(9600);
currentmillis = millis();
TimeApple = currentmillis; //initailize all times
TimeOrange = currentmillis; //
pinMode(13,OUTPUT); //
myservo.attach(13); // Pin input for servo motor
mState = stateStart; //we start out in this machine state
} // >>>>>>>>>>>>>> END OF setup() <<<<<<<<<<<<<<<<<
void loop()
{
//leave this line here
currentmillis = millis();
unsigned long time = millis();
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.print(time);
Serial.print(", ");
Serial.println(voltage);
//*********** Other loop stuff goes here ************
//************** State Machine section **************
switch (mState)
{
//***************************
case stateStart:
//go to servo 0
myservo.writeMicroseconds(0);
mState = stateApple;
TimeApple = millis();
break;
//***************************
case stateApple:
//is it time to run this section of code?
if (CheckTime(TimeApple, TaskAppleWait))
{
//go to servo 2700
myservo.writeMicroseconds(2700);
mState = stateOrange;
TimeOrange = millis();
}
break;
//***************************
case stateOrange:
//is it time to run this section of code?
if (CheckTime(TimeOrange, TaskOrangeWait))
{
//go to servo 0
myservo.writeMicroseconds(0);
mState = stateApple;
TimeApple = millis();
}
break;
//***************************
default:
// statements
break;
//***************************
} // end of switch/case
} // >>>>>>>>>>>>>> END OF loop() <<<<<<<<<<<<<<<<<
// F U N C T I O N S
//==========================================================
//Delay time expired function
boolean CheckTime(unsigned long &lastMillis, unsigned long wait) {
//has the time expired?
if (currentmillis - lastMillis >= wait)
{
lastMillis += wait; //get ready for the next timing cycle
return true;
}
return false;
} //END of CheckTime()
//==================
//======================================================================
// END OF CODE
//======================================================================
You have been a GREAT help! I don't think I would have figured that out on my own.
But is there a way to make simplify this code using multiple loops? Before I came back to the forum I was trying to get the "Scheduler" and "Scheduler-master" library import add-ons to work but I got nothing but errors even when trying to verify the examples.
I'm back. Is there a way to adjust the delay of the data reading as well? I need to add a DHT11 temperature and Humidity sensor to the circuit but it requires at least 250 milliseconds to get an accurate reading. Would you adjust the "Functions" section of the code and define "wait" to be a numerical value?
It seems you are not grasping the blink without delay concept.
Please answer this, what does the CheckTime() function actually do?
Try to modify this code:
#include <Servo.h>
Servo myservo;
const unsigned long TaskAppleWait = 1500UL; //Runs every 1.5 Sec
const unsigned long TaskOrangeWait = 1500UL; //Runs every 1.5 Sec
const unsigned long TaskDH11Wait = 250UL; //Runs every 250ms
//add more as needed
unsigned long TimeApple; //Times up, run TaskApple
unsigned long TimeOrange; //Times up, run TaskOrange
unsigned long TimeDH11; //Times up, run TaskDH11
//add more as needed
unsigned long currentmillis;
//other variables
//define the available states that we can have for this sketch
enum States{
stateStart, stateApple, stateOrange};
//add more states as needed
States mState = stateStart; //we start out in this machine state
//==========================================================
void setup()
{
Serial.begin(9600);
currentmillis = millis();
TimeApple = currentmillis; //initailize all times
TimeOrange = currentmillis; //
TimeDH11 = currentmillis; //
pinMode(13,OUTPUT); //
myservo.attach(13); // Pin input for servo motor
mState = stateStart; //we start out in this machine state
} // >>>>>>>>>>>>>> END OF setup() <<<<<<<<<<<<<<<<<
void loop()
{
//leave this line here
currentmillis = millis();
unsigned long time = millis();
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.print(time);
Serial.print(", ");
Serial.println(voltage);
//is it time to handle DH11 stuff yet?
if (CheckTime(TimeDH11, TaskDH11Wait))
{
//Do your DH11 stuff here
}
//*********** Other loop stuff goes here ************
//************** State Machine section **************
switch (mState)
{
//***************************
case stateStart:
//go to servo 0
myservo.writeMicroseconds(0);
mState = stateApple;
TimeApple = millis();
break;
//***************************
case stateApple:
//is it time to run this section of code?
if (CheckTime(TimeApple, TaskAppleWait))
{
//go to servo 2700
myservo.writeMicroseconds(2700);
mState = stateOrange;
TimeOrange = millis();
}
break;
//***************************
case stateOrange:
//is it time to run this section of code?
if (CheckTime(TimeOrange, TaskOrangeWait))
{
//go to servo 0
myservo.writeMicroseconds(0);
mState = stateApple;
TimeApple = millis();
}
break;
//***************************
default:
// statements
break;
//***************************
} // end of switch/case
} // >>>>>>>>>>>>>> END OF loop() <<<<<<<<<<<<<<<<<
// F U N C T I O N S
//==========================================================
//Delay time expired function
boolean CheckTime(unsigned long &lastMillis, unsigned long wait) {
//has the time expired?
if (currentmillis - lastMillis >= wait)
{
lastMillis += wait; //get ready for the next timing cycle
return true;
}
return false;
} //END of CheckTime()
//==================
//======================================================================
// END OF CODE
//======================================================================
Doesn't the CheckTime() function just look at the current millis() and once it hits either TimeApple or TimeOrange it from the previous case it applies the servo delay?
I think I understand the concept of Blink Without Delay concept but that isn't what I want to do. The temp/humidity sensor works much like the voltage and time as shown below. The data I'm logging is intended to be run for 2 hours and then saved the data to a file. I would rather not have a reading every 1/1000th - 1/100th of a second but instead be able to set a delay for the Serial.Print data that is being read. Is this possible? I'm sorry that my current goal is different than what I initially wanted when I first posted this forum. Thanks
#include "DHT.h"
#define DHTPIN TempHumidPin // The pin the temp/humid is plugged into
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600);
currentmillis = millis();
TimeApple = currentmillis; //initailize all times
TimeOrange = currentmillis; //
myservo.attach(SERVOpin); // Pin input for servo motor
pinMode(SERVOpin,OUTPUT);
dht.begin();
}
void loop()
{
//leave this line here
currentmillis = millis();
time = millis();
// read the input on analog pin 0:
int sensorValue = analogRead(VoltagePin);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
float humidity = dht.readHumidity();
// Read temperature as Celsius
float TCel = dht.readTemperature();
// print out the value you read:
Serial.print(time);
Serial.print(", ");
Serial.print(voltage);
Serial.print(",");
Serial.print(TCel);
Serial.print(",");
Serial.println(humidity);
//SERVO CODE PLACED HERE
}
CheckTime() function is nothing more than blink without delay set in a function.
be able to set a delay for the Serial.Print data that is being read. Is this possible
Sure.
One way is to use Serial.parseInt() - Arduino Reference to get the interger value you send using the serial monitor. Once you have the value, use it with the CheckTime() function to set your interval.