Hi everyone in this forum, first of all thank you for the work you do here helping people like me to advance and learn more about arduino.
I Just code a pretty simple program to control an autoclave motor and a poppet valve for my research project. However, in this program every time i want to stop the stirrer, open the poppet valve and trigger the gc(for sampling). I need to send a string to the serial monitor. How i can code it, to use the timer of the computer and run this string in a regular time interval. As far I know arduino doesn't have an on board clock, so how i can include the regular time for this particular string ?
The code with the explanation is below:
// This program control the speed of an autoclave motor and samples via a poppet valve.
// Data is set the com port in the format:-
// RPM Set, Quiescent Time (sec), Poppet Valve Time (millisec), Wait Time before starting the GC (sec)
// If you send only RPM, it will set new RPM Value and NOT sample.
// The RPM Valve that you send will round down to a multiple of 30.
// Note that the program will guess at the correct voltage for a requested RPM and adjust the voltage
// up or down to match the RPM settting.
// The RPM Values are constrained to a maximum of 1200 RPM.
// If you send the current RPM again (as for sampling) it will return to the correct voltage for that RPM.
// Also note that sending an RPM Value of 361 is NOT the same as sending 360.
// Sampling will begin if you send a Poppet Valve Time in milliseconds (i.e. poppet > 0)
// Then the motor will stop for the quiescent period (sec), the poppet will open for Poppet Valve
// Time (millisec). This will fill the GC sample loop. Wait Time (sec) later the GC run will start.
#include <Wire.h>
int rpmSETPOINT = 360; // Initial RPM Setpoint; Will round down to a multiple of 30
int quiet = 0; // Quiecient time (seconds) between stopping motor and sampling
int poppet = 0; // Time (milliseconds) the sampling valve remains open
int SamDelay = 0; // Time (seconds) between sampling and starting the GC
const int motorpin = 9; // the pin that switches the MOSFET (PWM) for motor
const int poppetpin = 8; // the pin that opens the sampling valve
const int GCpin = 13; // the pin that starts the GC (reed relay)
const int LEDpin = 12; // the indicator LED on the Unit
volatile byte half_revolutions; // pulses from Hall sensor
unsigned int Hallpps;
unsigned int motor_control;
int rpm_previous;
unsigned int rpm;
unsigned long timeold;
void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the motor pin as an output:
pinMode(motorpin, OUTPUT);
pinMode(3, INPUT); // set Hall sensor pin to input
pinMode(poppetpin, OUTPUT);
pinMode(GCpin, OUTPUT);
pinMode(LEDpin, OUTPUT);
digitalWrite(3, LOW); // turn off pullup resistors
digitalWrite(poppetpin, LOW);
digitalWrite(GCpin, LOW);
digitalWrite(LEDpin, LOW);
attachInterrupt(1, rpm_fun, RISING);
Hallpps = rpmSETPOINT / 30;
// constrain the values from 0 to 40; in case you entered something stupid
// this gives a maximum RPM of 1200
Hallpps = constrain(Hallpps, 0, 40);
rpmSETPOINT = Hallpps * 30;
motor_control = Hallpps * 12;
analogWrite(motorpin,motor_control); // sets the Autoclave motor to an initial value
half_revolutions = 0;
rpm = 0;
timeold = 0;
}
void loop() {
// if there's any serial information available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
rpmSETPOINT = Serial.parseInt();
quiet = Serial.parseInt(); // read quiet(seconds) if available
poppet = Serial.parseInt(); // read poppet(milliseconds) if available
SamDelay = Serial.parseInt(); // read Sample Delay(seconds) if available
// look for the next value, then timeout
Serial.setTimeout(250);
if (poppet > 0)
{
digitalWrite(LEDpin, HIGH); // indicates that the Unit is doing a sampling cycle
analogWrite(motorpin,0); // stops the autoclave motor
Serial.println("Autoclave Motor Stopped - Settling Period");
delay((quiet * 1000)); // wait for quiet seconds
digitalWrite(poppetpin, HIGH); // start sampling
Serial.println("Valve Open - Sampling");
delay(poppet); // sample
digitalWrite(poppetpin, LOW); // end sampling
Serial.println("Valve Closed - Sampling Completed");
delay((SamDelay * 1000)); // delay before GC starts
digitalWrite(GCpin, HIGH); // start GC
Serial.println("GC Started - Stirring Resumed");
delay(500);
digitalWrite(GCpin, LOW);
analogWrite(motorpin,motor_control); // turn the autoclave stirrer back on
poppet = 0; // set the poppet time back to zero
digitalWrite(LEDpin, LOW); // turns off the sampling LED
}
if (rpmSETPOINT != rpm_previous)
{
Hallpps = rpmSETPOINT / 30;
// constrain the values from 0 to 40; in case you entered something stupid
// this gives a maximum RPM of 1200
Hallpps = constrain(Hallpps, 0, 40);
rpmSETPOINT = Hallpps * 30;
motor_control = Hallpps * 12;
analogWrite(motorpin,motor_control); // sets the Autoclave motor to an initial value
}
}
if (half_revolutions >= 60 || (millis() - timeold) > 1000)
{
//Update RPM every 60 counts or every second,
//increase half_revolutions for better RPM resolution
rpm = (30*1000/(millis() - timeold))*half_revolutions;
timeold = millis();
half_revolutions = 0;
Serial.print(rpmSETPOINT);
Serial.print(" , ");
Serial.print(rpm);
Serial.print(" , ");
float volts0 = (motor_control / 255.0) * 24.0;
Serial.println(volts0);
if (rpm < rpmSETPOINT)
{
motor_control = motor_control + 1;
motor_control = constrain(motor_control,0,255);
analogWrite(motorpin,motor_control);
}
if (rpm > rpmSETPOINT && motor_control != 0)
{
motor_control = motor_control - 1;
motor_control = constrain(motor_control,0,255);
analogWrite(motorpin,motor_control);
}
rpm_previous = rpmSETPOINT;
}
}
void rpm_fun()
{
half_revolutions++;
//Each rotation, this interrupt function is run twice
}
Thank you beforehand,