Hey everyone, I'm almost finished with a school project and I need some help...I have a thermocouple which acts as an input to control a servo. When the temperature of the thermocouple reaches 115 the servo turns 90deg. Two problems:
-
The LCD keeps flashing rapidly during the loop. I know its because of the delay and the loop, but is there a way around this? The reason we put it inside the loop is because when the temperature exceeded 3-digits and went back down to 2-digits, it would leave the undesired last digit on the LCD. We wrote inside the loop to constantly "refresh" the temperature reading, but unfortunately its annoying me and isn't a desirable design.
-
The servo keeps "chirping". I can't seem to find anything on the boards that is similar to what I need. Can someone explain why it would be chirping and jerking every second or so? My guess is that the servo isn't in steady state or something, and the loop is having some sort of effect on it. Any help with code would be greatly appreciated!
/* Valve Control System for Monitoring & Scald-Proofing Tap Water Outputs
Developed by: Dennis M. Burke & Chris Mroz
Wayne State University
Detroit, MI
ET4999: Senior Project
Winter 2010
Copyright 2010 by Dennis M. Burke & Chris Mroz
All Rights Reserved
*/
/********************************************************************************
LCD Pinout: LED Pinout: Servo Motor:
Digital I/O Pin 2: D7 Digital I/O Pin 7: Red LED Digital I/O Pin 9
Digital I/O Pin 3: D6 Digital I/O Pin 8: Green LED
Digital I/O Pin 4: D5
Digital I/O Pin 5: D4
Digital I/O Pin 11: Enable Thermocouple:
Digital I/O Pin 12: RS Analog I/O Pin 0
*********************************************************************************/
#include <Servo.h>
#include <LiquidCrystal.h>
int red = 7, green = 8, index = 0, total = 0, average = 0, TC = 0, q = 0;
const int numReadings = 5;
int readings[numReadings];
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //see LCD Pinout
Servo myservo; // declares servo
void setup()
{
pinMode(TC, INPUT);
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
myservo.attach(9);
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
lcd.begin(20, 2);
lcd.print(" ET4999");
lcd.setCursor(0,1);
lcd.print(" Senior Project");
delay(1000);
for (int i = 0; i < 20; i++)
{
lcd.scrollDisplayLeft();
delay(50);
}
lcd.clear();
lcd.print(" Wayne State");
lcd.setCursor(0,1);
lcd.print(" University");
delay(1000);
for (int positionCounter = 0; positionCounter < 20; positionCounter++)
{
lcd.scrollDisplayRight();
delay(50);
}
lcd.clear();
lcd.print(" Scalding Water");
lcd.setCursor(0,1);
lcd.print(" Shutoff Device");
delay(3000);
lcd.clear();
lcd.print(" Developed by:");
delay(1000);
lcd.clear();
lcd.print(" Dennis Burke &");
lcd.setCursor(0,1);
lcd.print(" Chris Mroz");
delay(3000);
lcd.clear();
delay(500);
} //END VOID SETUP
void loop()
{
int raw = 0, celsius = 0, fahrenheit = 0;
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
if (q<5) //initializing sequence
{
lcd.print("Initializing");
for (int j = 0; j<=5; j++)
{
total= total - readings[index]; // subtract the last reading.
readings[index] = analogRead(TC); // read from the sensor.
total= total + readings[index]; // add the reading to the total.
index = index + 1; // advance to the next position in the array.
if (index >= numReadings) // if at the end of the array, then go back to the beginning
index = 0;
average = total / numReadings; // calculate the average analog reading (0 - 1024)
}
for(int z = 0; z <= 8; z++)
{
lcd.print(".");
digitalWrite(green, HIGH);
delay(250);
digitalWrite(green, LOW);
delay(250);
}
lcd.clear();
q = 5;
}
else
{
total= total - readings[index]; //see above notes...
readings[index] = analogRead(TC);
total= total + readings[index];
index = index + 1;
if (index >= numReadings)
index = 0;
average = total / numReadings;
celsius = ( 5.0 * average * 100.0) / 1024.0;
fahrenheit = (((celsius * 9) / 5) + 32);
if (fahrenheit >= 115)
{
myservo.write(180); // close valve and turn on "warning" LED
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
}
else
{
myservo.write(90); // open valve and turn on "okay" LED
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
}
lcd.print("Fahrenheit:");
lcd.setCursor(14,0);
lcd.print(fahrenheit);
lcd.setCursor(0,1);
lcd.print("Celsius:");
lcd.setCursor(14,1);
lcd.print(celsius);
delay(250);
lcd.clear();
} //END ELSE STATEMENT
} //END VOID LOOP