Skip to content

Instantly share code, notes, and snippets.

@settra
Created June 20, 2014 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save settra/5eabcb597c66412b3a88 to your computer and use it in GitHub Desktop.
Save settra/5eabcb597c66412b3a88 to your computer and use it in GitHub Desktop.
#include <Servo.h>
Servo FLOW1servo;
Servo RECservo;
Servo FLOW2servo;
Servo TEMPservo;
int LW = 0 ;
int RW = 0 ;
int AC= 0;
int REC=0;
int REC_OLD=1;
int TEMP=25;
int TEMP_OLD=25;
int AIRFLOW=0;
int AIRFLOW_OLD=0;
int BLOWER=0;
float EVAPORATOR_TEMP=0;
unsigned long time;
unsigned long AC_time;
boolean paused = false;
void setup() {
Serial.begin(9600); // same as in your java script
Serial.setTimeout(50) ;
RECservo.attach(7);
FLOW1servo.attach(8);
FLOW2servo.attach(A1);
TEMPservo.attach(A0);
pinMode(A5,INPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(9,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
}
int count=0;
float temp_sum =0.0;
void loop() {
//mesure evaporator temp, and then use the averege
float voltage = (analogRead(A5)* 5.0)/1024.0;
temp_sum = temp_sum + (voltage)*100.0 -50.0 ;
count++;
if (count>=100){
EVAPORATOR_TEMP = temp_sum/100.0 ;
temp_sum=0.0 ;
count=0;
}
if (EVAPORATOR_TEMP<=10.0){
digitalWrite(9,LOW);
AC_time=millis(); /// used to "delay" the re-opening of the ac compresor
}
if (Serial.available()>=14) { // "14" should be chnaged, to EXACTLY how many bytes your java app sends.
LW=Serial.parseInt();
RW=Serial.parseInt();
BLOWER=Serial.parseInt();
AC=Serial.parseInt();
REC=Serial.parseInt();
AIRFLOW=Serial.parseInt();
TEMP=Serial.parseInt();
//print them back for debug proposes
Serial.print(EVAPORATOR_TEMP);
Serial.print(",");
Serial.print(LW);
Serial.print(",");
Serial.print(RW);
Serial.print(",");
Serial.print(BLOWER);
Serial.print(",");
Serial.print(AC);
Serial.print(",");
Serial.print(REC);
Serial.print(",");
Serial.print(AIRFLOW);
Serial.print(",");
Serial.println(TEMP);
}
// power window controll//
//RIGHT
if (RW==1){
digitalWrite(3,LOW);
delay(50);
digitalWrite(2,HIGH);
}
else if(RW==2){
digitalWrite(2,LOW);
delay(50);
digitalWrite(3,HIGH);
}
else if (RW==0){
digitalWrite(2,LOW);
digitalWrite(3,LOW);
}
//LEFT
if (LW==1){
digitalWrite(5,LOW);
delay(50);
digitalWrite(4,HIGH);
}
else if(LW==2){
digitalWrite(4,LOW);
delay(50);
digitalWrite(5,HIGH);
}
else if (LW==0){
digitalWrite(4,LOW);
digitalWrite(5,LOW);
}
//AIRCONDITION//
if (BLOWER>0) {
if (BLOWER==1){ //PWM THE BLOWER FAN
analogWrite(11,30);
}
else if (BLOWER==2) {
analogWrite(11,100);
}
else if (BLOWER==3) {
analogWrite(11,180);
}
else if (BLOWER==4) {
analogWrite(11,255);
}
if ( (millis()-AC_time)>=60000) {
digitalWrite(9,AC); //TURN COMPRESOS ON/OF. ONLY if there has been 2 minutes, since it force closed cause of low temp.
}
if( (TEMP!=TEMP_OLD)||(AIRFLOW!=AIRFLOW_OLD)||(REC!=REC_OLD) ) {
// gives power to servos.
digitalWrite(12,HIGH);
time=millis();
delay(10); //when using delays, they cant be anything near, to the time intervals between complete serial arrivals (my case is 200ms)
if (REC==0) {
RECservo.writeMicroseconds(2000);
}
else if (REC==1){
RECservo.writeMicroseconds(1000);
}
REC_OLD =REC;
}
if (AIRFLOW==0){
FLOW1servo.writeMicroseconds(1500);
FLOW2servo.writeMicroseconds(1500);
}
else if (AIRFLOW==1){
FLOW2servo.writeMicroseconds(2000);
FLOW1servo.writeMicroseconds(1000);
}
else if (AIRFLOW==2){
FLOW1servo.writeMicroseconds(2000);
FLOW2servo.writeMicroseconds(1500);
}
else if (AIRFLOW==3){
FLOW2servo.writeMicroseconds(2000);
FLOW1servo.writeMicroseconds(1500);
}
else if (AIRFLOW==4){
FLOW2servo.writeMicroseconds(1000);
FLOW1servo.writeMicroseconds(1000);
}
AIRFLOW_OLD=AIRFLOW;
int temp_degree= -50*TEMP +2800 ;
TEMPservo.writeMicroseconds(temp_degree);
TEMP_OLD = TEMP ;
}
if (BLOWER==0){
digitalWrite(9,LOW);
digitalWrite(11,LOW);
}
if ( (millis()-time)>= 2000) {
digitalWrite(12,LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment