execute once within loop?

HI everyone,

I hope you can help me with something. please forgive me if I have left out some important info, I am a complete newbie to arduino and this ( my first project) is also
a learning project for me. The sketch below is a mash up of various other sketches - I welcome any suggestions to improve it :slight_smile:

What the sketch is for:
recording temperature of two points on a alcohol distillation column, one at the top and the other half way down the column. display them on an lcd screen and log the data
on a sd card.
then switch two servos that open gate valves at specific times (actually specific temperatures) which allow the flow of alcohol vapor into different condensers.

what I would like to add to this sketch is the switching of two servos 'once' when the differences in the two sensors are at a defined point.
I can get it all to work but The problem I'm having is with turning the servos only once. they turn every time the loop executes. I have played around with

for (int i = 0; i < 1; i++)
{
servo()
}

but can't get it to work as desired within the main loop ( is this because the int i; is reset back to 0 at the start of each loop?)

I have also used it in 'setup' which worked once of course but this isn't much use to me.

thanks in advance for your help :slight_smile:

cheers
Jim

The code so far is

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
#include <Servo.h> 

RTC_DS1307 RTC;

File myFile;

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 12

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// arrays to hold device addresses
DeviceAddress insideThermometer, outsideThermometer;

LiquidCrystal lcd(4, 5, 6, 7, 8, 9);

Servo myservo; 

int angle = 180;    // variable to store the servo direction
int angle2 = 90;
int angle3 = 0;

void setup(void)
{
  lcd.begin(16, 2);
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");
  pinMode(10, OUTPUT);
  myservo.attach(3);  // attaches the servo on pin 3 to the servo object 

  
    if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  myFile = SD.open("test.txt", FILE_WRITE);
  
  Wire.begin();
  RTC.begin();
  
  // Start up the library
  sensors.begin();
  



  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: "); 
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");


  // show the addresses we found on the bus
  Serial.print("Device 0 Address: ");
  printAddress(insideThermometer);
  Serial.println();

  Serial.print("Device 1 Address: ");
  printAddress(outsideThermometer);
  Serial.println();

  // set the resolution to 9 bit
  sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
  sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);

  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(insideThermometer), DEC); 
  Serial.println();

  Serial.print("Device 1 Resolution: ");
  Serial.print(sensors.getResolution(outsideThermometer), DEC); 
  Serial.println();
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    // zero pad the address if necessary
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);
}

// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
{
  Serial.print("Resolution: ");
  Serial.print(sensors.getResolution(deviceAddress));
  Serial.println();    
}

// main function to print information about a device
void printData(DeviceAddress deviceAddress)
{
  Serial.print("Device Address: ");
  printAddress(deviceAddress);
  Serial.print(" ");
  printTemperature(deviceAddress);
  Serial.println();
}

void loop(void)
{ 
  
  
  sensors.requestTemperatures();

  float T1 = sensors.getTempC(insideThermometer);
  T1 = T1 + sensors.getTempC(insideThermometer);
  T1 = T1 + sensors.getTempC(insideThermometer);
  T1 = T1/3;
  
  float T2 = sensors.getTempC(outsideThermometer);
  T2 = T2 + sensors.getTempC(outsideThermometer);
  T2 = T2 + sensors.getTempC(outsideThermometer);
  T2 = T2/3;
  
 // int X = 1;
  
//  if (T1 > T2+2 && X > 0 )
//{
 //   myservo.write(angle);              // tell servo to go to position in variable 'pos' 
//    delay(1240);
  //  myservo.write(angle2);
  //  X = X - 1;
//}

  
  Serial.begin(57600);
  DateTime now = RTC.now();
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print(", ");
    
    
  Serial.begin(9600);
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.print(", ");
  
  myFile.print("inside, ");
  myFile.print(T1);
  myFile.print(",");
  
  Serial.print("Inside, ");
  Serial.print(T1);
  Serial.print(", ");
  
  myFile.print("Outside, ");
  myFile.println(T2);
  myFile.print(", ");
  
  Serial.print("Outside, ");
  Serial.println(T2);
  Serial.print(", ");
  
  lcd.setCursor(0, 0);
  lcd.print("inside ");
  lcd.print(T1);
  
  lcd.setCursor(0, 1);
  lcd.print("outside ");
  lcd.print(T2);
  
  
  delay(2000);
  
}

Check this pattern ...

void setup() 
{ 
  Serial.begin(115200); 
  Serial.println("Start..."); 
} 

boolean once = true;

void loop() 
{ 
  if ( once == true ) 
  {
    Serial.println("hi"); 
    once = false;
  }
}

Hi Rob,
thanks for that, I will give it a try and see how I go :slight_smile:

cheers
Jim

More general is to use an integer

void setup() 
{ 
  Serial.begin(115200); 
  Serial.println("Start..."); 
} 

uint8_t howManyTimes = 3;

void loop() 
{ 
  if ( howManyTimes  > 0 ) 
  {
    Serial.println("hi"); 
    howManyTimes--;
  }
}

Just to be clear - your requirement is to use one continuous rotation server to open a valve at a certain temp condition and another servo to do a second valve later? There is no need to close them? Do you need to be careful to ensure that valve two can't open until valve one has?

RobTillart's pattern gives you all you need, but if the requirement becomes more complex, consider a state machine (search the forum for examples).

Regarding your repeated reads of the temperature sensors to get an average - it is not actually doing anything useful. The calls to getTempC just ask the sensor what temperature it read last time you called requestTemperatures, so those three calls will all return the same thing. If you do want to do averaging, consider putting it in a function that you pass the sensor address to just to de-clutter loop a bit.