im trying to use serial monitor to control arduino. it only works on the first try. I noticed that the string variable in 'msg' was not deleted, i've tried to delete the string within the variable 'msg' using the function msg = ""; for it to be ready for the next command, but it doesnt seem to work.
//library(time.h & TimeAlarms.h) installed from: http://playground.arduino.cc/Code/Time
#include <Time.h>
#include <TimeAlarms.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
int Relay1 = 3;
int Relay2 = 4;
char state = 0;
int ledpin = 5;
String msg = "";
// 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);
void setup()
{ pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(ledpin, OUTPUT);
Serial.begin(9600);
sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
// create the alarms
Alarm.alarmRepeat(12,00,00, AC1on);
Alarm.alarmRepeat(12,05,00, AC2off);
Alarm.alarmRepeat(00,00,00, AC2on);
Alarm.alarmRepeat(00,05,00, AC1off);
}
void loop(){
if (Serial.available()>0){
char pdata= Serial.read();
msg += pdata;
if (msg == "lights on"){
digitalWrite(ledpin,HIGH);
msg = "";}
else if (msg == "Lights off"){
digitalWrite(ledpin,LOW);
msg = "";}
else if (msg == "info"){
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.print("current Temperature is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.println();
Serial.print("The current time is: ");
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
Serial.print("Air conditioning status: ");
delay(1000);
msg="";
Serial.println(msg);
}
else
{msg=="";}
Serial.println(msg);
}}
// functions to be called when an alarm triggers:
void AC1on(){
Serial.println("AC1 on"); //print "AC1 on" on serial monitor
digitalWrite(Relay1, LOW); //trigger AC1 relay
}
void AC2off(){
Serial.println("AC2 off"); //print "AC1 on" on serial monitor
digitalWrite(Relay2, LOW); //trigger AC1 relay
}
void AC2on(){
Serial.println("AC2 on"); //print "AC1 on" on serial monitor
digitalWrite(Relay2, HIGH); //trigger AC1 relay
}
void AC1off(){
Serial.println("AC1 off"); //print "AC2 on" on serial monitor
digitalWrite(Relay1, HIGH); //trigger AC2 relay on
}
void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
when i tried to run it and type "info" in the serial monitor, this is the output
i
in
inf
current Temperature is: 27.50
The current time is: 8:29:06
Air conditioning status:
which is fine but when im going to type it over, im getting this instead,
i
in
inf
info
info