skyjumper:
If you need to read those sensors every 100ms, you'll need a loop somewhere. You can out it anywhere, but you'll need a loop. Search for "blink without delay" and you'll see how this is done. Another option is to run a timer interrupt, and read the sensors in the ISR, but that's a little advanced.
Just do a loop, read the time using millis() and save this value. Each time through the loop, compare the current time with the saved time, and if the difference is at least 100ms, read the sensors.
Heres a section of the code...
menu.getRoot().add(menu1Item1);
menu1Item1.addRight(menu1Item2).addRight(menu1Item3);
menu1Item1.add(menuItem1SubItem1).addRight(menuItem1SubItem2);
menu1Item2.add(menuItem2SubItem1).addRight(menuItem2SubItem2).addRight(menuItem3SubItem3);
menu.toRoot();
} // setup()...
void loop()
{
readButtons(); //I splitted button reading and navigation in two procedures because
navigateMenus(); //in some situations I want to use the button for other purpose (eg. to change some settings)
readSensors();
Input = analogRead(A0);
myPID.Compute();
/************************************************
* turn the output pin on/off based on pid output
************************************************/
if(millis() - windowStartTime>WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;}
if(Output < millis() - windowStartTime) digitalWrite(RelayPin,HIGH);
else digitalWrite(RelayPin,LOW);
}
//loop()...
void menuChanged(MenuChangeEvent changed){
MenuItem newMenuItem=changed.to; //get the destination menu
lcd.clear();
lcd.setCursor(0,0); //set the start position for lcd printing to the second row
if(newMenuItem.getName()==menu.getRoot()){
lcd.setCursor(2, 1); // set the cursor to column 0, line 0
lcd.print("PPO1"); // Print a message to the LCD.
lcd.setCursor(8, 1); // set the cursor to column 6, line 0
lcd.print("PPO2"); // Print a message to the LCD
lcd.setCursor(14, 1); // set the cursor to column 12, line 0
lcd.print("PPO3"); // Print a message to the LCD.
}else if(newMenuItem.getName()=="SET POINT-->"){
lcd.print("SET POINT-->");
}else if(newMenuItem.getName()=="SET POINT MENU--->"){
lcd.setCursor(0, 0);
lcd.print(" SETPOINT MENU ");
lcd.setCursor(0,1);
lcd.print(" SV= PV= ");
}else if(newMenuItem.getName()=="Item1SubItem2"){
lcd.print("Item1SubItem2 ");
}else if(newMenuItem.getName()=="TIME SETTINGS"){
lcd.print("TIME SETTINGS");
}else if(newMenuItem.getName()=="Item2SubItem1"){
lcd.print("Item2SubItem1 ");
}else if(newMenuItem.getName()=="Item2SubItem2"){
lcd.print("Item2SubItem2 ");
}else if(newMenuItem.getName()=="Item2SubItem3"){
lcd.print("Item2SubItem3 ");
}else if(newMenuItem.getName()=="Item--->3"){
lcd.print("Item--->3 ");
}
}
void menuUsed(MenuUseEvent used){
}
void readButtons(){ //read buttons status
int reading;
int buttonEnterState=LOW; // the current reading from the Enter input pin
int buttonEscState=LOW; // the current reading from the input pin
int buttonLeftState=LOW; // the current reading from the input pin
int buttonRightState=LOW; // the current reading from the input pin
//Enter button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinEnter);
// check to see if you just pressed the enter button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonEnterState) {
// reset the debouncing timer
lastEnterDebounceTime = millis();
}
if ((millis() - lastEnterDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonEnterState=reading;
lastEnterDebounceTime=millis();
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonEnterState = reading;
//Esc button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinEsc);
// check to see if you just pressed the Down button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonEscState) {
// reset the debouncing timer
lastEscDebounceTime = millis();
}
if ((millis() - lastEscDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonEscState = reading;
lastEscDebounceTime=millis();
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonEscState = reading;
//records which button has been pressed
if (buttonEnterState==HIGH){
lastButtonPushed=buttonPinEnter;
}else if(buttonEscState==HIGH){
lastButtonPushed=buttonPinEsc;
}else if(buttonRightState==HIGH){
lastButtonPushed=buttonPinRight;
}else if(buttonLeftState==HIGH){
lastButtonPushed=buttonPinLeft;
}else{
lastButtonPushed=0;
}
}
void readSensors(){
lcd.setCursor(2, 2); //sets the cursor to column 0, line 1
inputReading1 = analogRead(sensor1); //getting the voltage reading from the 02 sensor
float voltage1 = inputReading1 * aref_voltage; // converting that reading to voltage
voltage1 /= 1024.0;
lcd.print(voltage1, 2);//print actual voltage to lcd
delay(200);
lcd.setCursor(8, 2); //sets the cursor to column 6, line 1
inputReading2 = analogRead(sensor2); //getting the voltage reading from the temperature sensor
float voltage2 = inputReading2 * aref_voltage; // converting that reading to voltage
voltage2 /= 1024.0;
lcd.print(voltage2, 2); //print actual voltage to lcd
delay(200);
lcd.setCursor(14, 2); //sets the cursor to column 12, line 2
inputReading3 = analogRead(sensor3); //getting the voltage reading from the temperature sensor
float voltage3 = inputReading3 * aref_voltage; // converting that reading to voltage
voltage3 /= 1024.0;
lcd.print(voltage3, 2); //print actual voltage to lcd
delay(200);}
if(DateTime.available()) {
lcd.setCursor(3, 1);
unsigned long prevtime = DateTime.now();
while( prevtime == DateTime.now() ); // wait for the second to rollover
DateTime.available(); //refresh the Date and time properties
digitalClockDisplay( ); // update digital clock
}
}
void navigateMenus() {
MenuItem currentMenu=menu.getCurrent();
switch (lastButtonPushed){
case buttonPinEnter:
if(!(currentMenu.moveDown())){ //if the current menu has a child and has been pressed enter then menu navigate to item below
menu.use();
}else{ //otherwise, if menu has no child and has been pressed enter the current menu is used
menu.moveDown();
}
break;
case buttonPinEsc:
menu.toRoot(); //back to main
break;
case buttonPinRight:
menu.moveRight();
break;
case buttonPinLeft:
menu.moveLeft();
break;
}
lastButtonPushed=0; //reset the lastButtonPushed variable
}