One of the homework tasks was to create a condition that required an analogue input to be within a certain range. I went with the example given, having a light respond to a temperature sensor.
The starting point was the 'Temperature Alarm' instructions that came with the Arduino kit.
After I got this to work I added two LEDs to the system and gave them instructions on what to do with different temperature thresholds.
Here is the code...
// Conditional Thermostat Test with Temperature Alarm
float sinVal;
int toneVal;
unsigned long tepTimer;
int greenLED = 9;
int redLED = 10;
void setup() {
pinMode (8, OUTPUT); //configure buzzer pin
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
Serial.begin(9600); // configure baud rate to 9600 bps
}
void loop() {
int val; //save the value of LM35 temperature sensor
double data; //save the converted value of temperature
val = analogRead(0); //Connect LM35 to analogue pin and read value from it
data = (double) val * (5 / 10.24); //converts the voltage value to temperature value
if (millis() - tepTimer > 50) //Every 500 ms, serial port oputputs temperature value
tepTimer = millis();
Serial.print("temperature: "); // Serial port outputs temperature
Serial.print(data); //Serial port outpouts temperature value
Serial.println("C"); // Serial port output temperature unit
if (data < 23) { // if temperature is less than the threshold, the greenLED lights up
digitalWrite(greenLED, HIGH); // turns on the greenLED
}
else { // If the temperature is lower than the threshold, turn off the greenLED
digitalWrite(greenLED, LOW); // turns off the green LED
{
if (data > 23) { // if temperature is higher than the threshold, the buzzer starts to make sound
for (int x = 0; x < 180; x++) { //Convert sin function to radian
sinVal = (sin(x * (3.1412 / 180))); //Use sin function to generate frequency of sound
toneVal = 2000 + (int(sinVal * 1000)); // configure the buzzer pin 8
tone(8, toneVal);
delay(2);
digitalWrite(redLED, HIGH); // and the red LED lights up
}
}else { // If the temperature is lower than the threshold, turn off the buzzer and the redLED
noTone(8); // turn off the buzzer
digitalWrite(redLED, LOW); // turns off the red LED
}
}
}
}
The starting point was the 'Temperature Alarm' instructions that came with the Arduino kit.
After I got this to work I added two LEDs to the system and gave them instructions on what to do with different temperature thresholds.
Here is the code...
// Conditional Thermostat Test with Temperature Alarm
float sinVal;
int toneVal;
unsigned long tepTimer;
int greenLED = 9;
int redLED = 10;
void setup() {
pinMode (8, OUTPUT); //configure buzzer pin
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
Serial.begin(9600); // configure baud rate to 9600 bps
}
void loop() {
int val; //save the value of LM35 temperature sensor
double data; //save the converted value of temperature
val = analogRead(0); //Connect LM35 to analogue pin and read value from it
data = (double) val * (5 / 10.24); //converts the voltage value to temperature value
if (millis() - tepTimer > 50) //Every 500 ms, serial port oputputs temperature value
tepTimer = millis();
Serial.print("temperature: "); // Serial port outputs temperature
Serial.print(data); //Serial port outpouts temperature value
Serial.println("C"); // Serial port output temperature unit
if (data < 23) { // if temperature is less than the threshold, the greenLED lights up
digitalWrite(greenLED, HIGH); // turns on the greenLED
}
else { // If the temperature is lower than the threshold, turn off the greenLED
digitalWrite(greenLED, LOW); // turns off the green LED
{
if (data > 23) { // if temperature is higher than the threshold, the buzzer starts to make sound
for (int x = 0; x < 180; x++) { //Convert sin function to radian
sinVal = (sin(x * (3.1412 / 180))); //Use sin function to generate frequency of sound
toneVal = 2000 + (int(sinVal * 1000)); // configure the buzzer pin 8
tone(8, toneVal);
delay(2);
digitalWrite(redLED, HIGH); // and the red LED lights up
}
}else { // If the temperature is lower than the threshold, turn off the buzzer and the redLED
noTone(8); // turn off the buzzer
digitalWrite(redLED, LOW); // turns off the red LED
}
}
}
}
Comments
Post a Comment