In the following code, I will read A1 which is an analog input from a thermocouple, A2 another input from a displacement sensor and then I will control a relay connected to pin 6 to switch it on/off.
The problem is that when I read zero C temperature it shows 14 C but when I remove "pinMode(6, OUTPUT);" then the temp. reads correctly (zero C) but the relay does not work anymore??!!
Anybody know what is happening? What should I do?
int sensePin1 = A1;
int sensepin2=A2;
int sensorInput;
int sensorInput2;
double temp;
double tempvolt;
double dis;
double dis0;
int in1 =7;
int i=0;
unsigned long int milli_time; //variable to hold the time
void setup() {
Serial.begin(9600); //Start the Serial Port at 9600 baud (default)
Serial.println("CLEARDATA");
Serial.println("LABEL,Position,Time (Milli Sec.),i,Temp");
}
void loop() {
sensorInput = analogRead(A1); //read the analog sensor and store it
sensorInput2 = analogRead(A2); //read the analog sensor and store it
tempvolt=sensorInput;
temp = 0.5452*tempvolt - 1.1888;
sensorInput2 = analogRead(A2);
dis0=sensorInput2;
dis=0.0671*dis0+33.389;
pinMode(6, OUTPUT);
if (temp <10) {
digitalWrite(6,LOW);
}
else if (temp>20){
digitalWrite(6,HIGH);
delay(600);
i=i+1;
}
milli_time = millis();
Serial.print("DATA,TIME");
Serial.print(",");
Serial.print(dis);
Serial.print(",");
Serial.print(i);
Serial.print(",");
Serial.println(temp);
Serial.print(",");
delay(600);
}
First thing you need to do is read the guides at the start of the forum as to how to use the forum ad specifically how to post code. It makes it that much easier for the people who would like to help you, otherwise in frustration they might just turn away.
Hi, in this code I am controlling a switch base on temperature which is read by A1.
When I use " pinMode(6, OUTPUT);" to turn on/off the switch the temperature is not correct( it reads 14 C instead of 0 C for instance). But when I remove this line, the temperature reading is fine but the switch does not work anymore. Does anybody know the solution?
Thanks in advance,
int sensePin1 = A1;
int sensepin2=A2;
int sensorInput;
int sensorInput2;
double temp;
double tempvolt;
double dis;
double dis0;
int in1 =7;
int i=0;
unsigned long int milli_time; //variable to hold the time
void setup() {
Serial.begin(9600); //Start the Serial Port at 9600 baud (default)
Serial.println("CLEARDATA");
Serial.println("LABEL,Position,Time (Milli Sec.),i,Temp");
}
void loop() {
sensorInput = analogRead(A1); //read the analog sensor and store it
sensorInput2 = analogRead(A2); //read the analog sensor and store it
tempvolt=sensorInput;
temp = 0.5452*tempvolt - 1.1888;
sensorInput2 = analogRead(A2);
dis0=sensorInput2;
dis=0.0671*dis0+33.389;
pinMode(6, OUTPUT);
if (temp <10) {
digitalWrite(6,LOW);
}
else if (temp>20){
digitalWrite(6,HIGH);
delay(600);
i=i+1;
}
milli_time = millis();
Serial.print("DATA,TIME");
Serial.print(",");
Serial.print(dis);
Serial.print(",");
Serial.print(i);
Serial.print(",");
Serial.println(temp);
Serial.print(",");
delay(600);
}
Why set pinMode() every time round loop()? It's only needs to be done once in setup().
And reading two analog pins rapidly can cause problems because the ADC doesn't have time to settle. Putting a delay(1) or maybe delay(2) between them usually solves that.
Where does the relay coil power come from? If from the 5V pin, it may be causing a small voltage dip that could throw your AREF voltage off. Please post a wiring diagram.
I moved the "pinMode" to setup(), it did not help.
I used the GND beside the 5-volt pin for the relay, later I used the GND pin next to digital pins and it seems the thermocouple and relay are both working properly.
I thought all of the GND pins on the board are connected, is not it true?