I wrote the following code for a climate control system but I'm having an issue. Arduino uno. Hc05 I know my phone is connecting to the arduino because it shows up on the bt link on the app, the lights blink and then change rhythm when you select it. It's cold here and I'd love to have a heater in my truck. Please help. I didn't comment every single line but should be enough to follow. Thank you.
#define pin1 2 //motor #1 +
#define pin2 3 //motor #1 -
#define pw1 9 //motor 2 pwm
#define pin3 4 //motor #3 +
#define pin4 5 //motor #3 -
int analogPin1 = A0; //read location for bypass valve pot
int analogPin2 = A1; //read location for flapper valve pot
int analogPin3 = A2; //read location for temp sensor
long previousMillis = 0;
long interval = 2000;
void setup() {
// put your setup code here, to run once:
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pw1, OUTPUT);
Serial.begin(9600);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
analogWrite(pw1, 50);
}
void loop() {
// put your main code here, to run repeatedly:
float potvoltage1; //reference signal from flapper motor pot
float potvoltage2; //reference signal from bypass valve pot
int testvoltage = 1023;
float refvoltage; //comparison value for reference signals...provided by arduino
int tempF; //reference sinal from temperature sensor
int temprequest; //requested change from android app
int fanSpeed; //calculated value based on val
int value; //another calculated value based on val, for a different purpose
int val;
if (Serial.available() >= 2 )
{
unsigned int a = Serial.read();
unsigned int b = Serial.read();
unsigned int val = (b * 256) + a;
Serial.print(val); //Added this line to test the incoming data...it is for test purposes only
if (val > 200 && val < 300) //requested change in fan speed
{
fanSpeed = (val * ( 250 / 20));
analogWrite(pw1, fanSpeed);
}
else if (val > 300 && val < 400) //requested new flapper zone
{
value == ((val - 300) * 261);
potvoltage2 = analogRead(analogPin2);
if (value < potvoltage2)
{
while (value < potvoltage2)
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
potvoltage2 = analogRead(analogPin2);
}
if (value > potvoltage2)
{
while (value > potvoltage2)
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH);
potvoltage2 = analogRead(analogPin2);
}
}
else if (val > 100 && val < 200) // change in temperature setting
potvoltage1 = analogRead(analogPin1);
temprequest = (val - 100);
{
if (val == 164 )
{
while (potvoltage1 != 41 ); //where 41 = bypass valve closed pot signal voltage
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
potvoltage1 = analogRead(analogPin1);
}
else if (val == 186)
{
while (potvoltage1 != 977 ); //where potvoltage is the signal voltage from the bypass valve and 977 = bypass valve open pot signal voltage
digitalWrite(pin2, HIGH);
digitalWrite(pin1, LOW);
potvoltage1 = analogRead(analogPin1);
}
else if (val > 64 && val < 86 )//begin loop to move valve aperture
{
tempF = analogRead (analogPin2);
while (tempF < temprequest) //temperature requested is hotter than current temp
{
while (refvoltage > potvoltage1)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis < interval)
{
previousMillis = currentMillis;
refvoltage == (refvoltage + 40); //where refvoltage is the reference voltage for the next segment (1/24th)
digitalWrite(pin2, HIGH);
digitalWrite(pin1, LOW);
}
}
}
while (tempF > temprequest) // temperature requested is cooler than current temp
{
while (refvoltage < potvoltage1)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis < interval)
{
previousMillis = currentMillis;
refvoltage == (refvoltage - 40); //moving down the scale .2V at a time
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
}
}
}
}
}
}
}