I cannot seem to figure out why this is happening. I am running a Servo, Relay, LM35 for temp monitoring, and a photocell. My temp sensor fluctuates a lot while looping through my program. The goal is to have my Arduino Uno R3 Plus, control 2 Relays (for 120v 5A loads), when temperatures reach a threshold. The Servo motor is to run when the photocell reaches a certain brightness, and moves back as a certain darkness (not calibrated fully yet).
This is the output I get from my code. Notice the temp fluctuation. As a side note, even when disconnecting the Servo and Relay, I still get erratic behavior.
Output:
Temp: 18.55good morningvoidloopstart
Celcuis: 18.55
Temp: 18.55good morningvoidloopstart
Celcuis: 20.51
Temp: 43.51good morningvoidloopstart
Celcuis: 16.11
Temp: 16.11good morningvoidloopstart
Celcuis: 21.48
Temp: 21.48good morningvoidloopstart
Celcuis: 16.11
Temp: 16.11good morningvoidloopstart
Celcuis: 25.88
good morningvoidloopstart
Celcuis: 24.90
good morning
/*
* Tutorial 2a: Sensing Light
*
* Measure brightness using a photocell over serial.
*
*
* To see this sketch in action, put the board and sensor in a well-lit
* room, open the serial monitor, and and move your hand gradually
* down over the sensor.
*
* The circuit:
* - photoresistor from analog in 0 to +5V
* - 10K resistor from analog in 0 to ground
*
*
* created 1 Jul 2009
* modified 9 Apr 2012
* by Tom Igoe
* modified 13 August 2013
* by Blaise Jarrett
*
* This example code is in the public domain.
*
* Derivative work from:
* http://www.arduino.cc/en/Tutorial/SwitchCase
*
*/
#include <Servo.h>
#include <math.h>
Servo myservo;
const int sensorMin = 0;
const int sensorMax = 800;
int photocellPin = A5;
int LEDPin = 2;
int LEDPin2 = 4;
int pos = 0;
int lm35Pin = A0;
int RelayPin = 10;
void setup()
{
// set up serial at 9600 baud
Serial.begin(9600);
pinMode (LEDPin, OUTPUT);
pinMode (LEDPin2, OUTPUT);
myservo.attach(9);
myservo.writeMicroseconds(1500);
pinMode(10, OUTPUT);
}
void loop()
{
Serial.println("voidloopstart");
int photocellValue;
int tempValue;
int range;
float temperature;
tempValue = analogRead(lm35Pin);
temperature = float(tempValue) * 0.48828125;
Serial.print("Celcuis: ");
Serial.println(temperature);
if(temperature <= 24){
digitalWrite(RelayPin, HIGH);
Serial.print("Temp: " + String(temperature));
}
else {
digitalWrite(RelayPin, LOW);
// Serial.print("else");
}
// digitalWrite(RelayPin, LOW);
// do something different depending on the
// range value
delay(1000);
photocellValue = analogRead(photocellPin);
range = map(photocellValue, sensorMin, sensorMax, 0, 2);
switch (range)
{
// your hand is on the sensor
case 0:
Serial.print("good night");
digitalWrite(LEDPin, HIGH);
digitalWrite(LEDPin2, LOW);
myservo.write(0);
break;
case 1:
Serial.print("good morning");
digitalWrite(LEDPin, LOW);
digitalWrite(LEDPin2, HIGH);
myservo.write(90);
break;
// your hand is a few inches from the sensor
case 2:
Serial.println("bright");
digitalWrite(LEDPin, LOW);
digitalWrite(LEDPin2, HIGH);
break;
}
delay(20000);
}
PS, I am not a programmer, but learned Java way back 10+ years ago. Ive lost a lot of my abilities, but am using this as a way to build my coding again. Any input is appreciated.
Any ideas on what could be going on?

