Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View Arduino_Interior_control
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
#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
Something went wrong with that request. Please try again.