Hello there!
I am fairly new to Arduino and currently working on the third Starter project, which uses a temperature sensor as an input and activates LEDs depending on analog values. I am running the sketch on the Web Editor and printing some values recorded using Serial.print(), with a baud of 9600 (same in sketch and Serial Monitor platform).
The problem is, the values in each printing session are mixed with each other, and some characters are doubled, shown in the examples below:
Example 1: Sensor value: 1or value: 145, Volts: 0.71,45, Volts: 0.71, Temperature: 20 Temperature: 20.80
Example 2: Sensor value: 146, Volts: e: 146, Volts: 0.71, Temperatur0.71, Temperature: 21.29
Example 3: Sensor value: 144, Vol value: 144, Volts: 0.70, Tempets: 0.70, Temperature: 20.31
Instead of (example):
Sensor value: 145, Volts: 0.71, Temperature: 20.8
I tried different baud values but the characters do not make sense. The sketch is as follows:
const int TempSensor = A0; // analog input pin connected to temp sensor
const float baseLineTemp = 18.0; // 18 degreesvoid setup() {
// open up connection between Arduino and PC to send analog values
Serial.begin(9600); // 9600 bits per second// set output pins (default state: LOW)
for (int pinNumber = 2; pinNumber < 5; pinNumber++){
pinMode(pinNumber, OUTPUT);
}}
void loop() {
// read temperature analog value and print on monitor
int sensorVal = analogRead(TempSensor);Serial.print("Sensor value: ");
Serial.print(sensorVal);// convert analog reading to voltage and print
float Voltage = (sensorVal/1024.0) * 5.0;Serial.print(", Volts: ");
Serial.print(Voltage);// convert voltage to temperature and print
float Temperature = (Voltage - 0.5) * 100;Serial.print(", Temperature: ");
Serial.println(Temperature);// activate LEDs depending on temperature
if (Temperature < baseLineTemp){ // if temperature less than baseline
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}else if (baseLineTemp <= Temperature && Temperature < baseLineTemp + 2){ // if temperature between baseline and baseline +2
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}else if (Temperature >= baseLineTemp + 2){ // if temperature more than baseline +2
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
}delay(1); // a small delay of 1 ms is advised for ADC
}
Any idea what might be the case?
Thank you!