I have a bench top power supply that I'm using to send anywhere from 8V to 12V to a L7805CV that drops the voltage to <=5V before it reaches the arduino uno's Vin pin. Running from the negative and positive from the bench top power supply is a voltage divider, that also drops the 12V to ~4.9V.
The divider then leads to pin A0, to call the AnalogRead function and use the formula in the code below to print a reading.
The Problem: The first problem is the readings are all over the place at times. If I drop the voltage on the power supply to 8, it'll hover around the expected ~3.5. Other times, it'll spit out the 3.5 and on the next loop reads 4.7 consistently. It seems to read the voltages correctly after not being on for awhile and then starts acting up when I've uploaded the code a few different times with changes.
It isn't a cheap power supply either. I picked this one up from Circuit Specialists and have confirmed the voltages coming from it with a multimeter to be accurate. I have also tried putting 2 capacitors with the voltage dividers to smooth out the readings before it connects to the Arduino, but that did not do anything either.
The second issue is digital pin 8. This pin is connected to a TIP100 that is acting as a switch between the power supply and the negative on the LED strip. For whatever reason it is always connecting the gate, leaving the LEDs always on, regardless of the input from the hall effect sensor or LDR. I've been experimenting with both, so that's why I have both of those in the code.
Upon removing the connection between pin 8 and the TIP100, the LEDs go off, so the TIP100 is working, it's something with the code/arduino.
int ldrPin = A4; // This is the pin connected between teh resiststor and the LDR. This reads the value coming from the LDR.
int sensorValue = 0;
const int hallPin = 2;
int hallState = 0;
const float referenceVolts = 4.904348; //
const int batteryPin = A0; // This is the variable that stores teh value coming form the sensor.
int blinkDelay = 300;
bool doneFlag = false;
void setup() {
pinMode(8, OUTPUT); //This pin is what gets turned on or off based on the value from the LDR using the logic in the lines below. In this example, it is connected to the TIP100 that turns the LEDs on or off. This line defines pin 6 as the OUTPUT.
pinMode(ldrPin, INPUT); // This line defines pin A2 as the input, where we will take our receiving values from. Remember we set pin A2 above as int ldrPin = A2
pinMode(12, OUTPUT); // This pin is connected to the single LED that blinks, indicating low battery
pinMode(hallPin, INPUT);
// analogReference(DEFAULT);
Serial.begin(9600); //sets serial port for communication
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(ldrPin); // this sets the variable "sensorValue" as whatever the reading coming from pin A2 is.
Serial.println(sensorValue); //prints the values coming from the sensor on the screen in the IDE
hallState = digitalRead(hallPin);
Serial.println("HallState");
Serial.println(hallState);
int val = analogRead(batteryPin);
float volts = (val / 1023.0) * referenceVolts;
Serial.println(volts);
if(hallState = 1);
digitalWrite(6, LOW);
if(hallState = 0) //if that value is above 100, then do....
digitalWrite(6, HIGH); //turn relay ON, by allowing power to flow from pin 6
if (hallState = 0) {
if(doneFlag == false) {
if(volts < 3.85) {
digitalWrite(12, HIGH);
delay(blinkDelay);
digitalWrite(12, LOW);
delay(blinkDelay);
digitalWrite(12, HIGH);
delay(blinkDelay);
digitalWrite(12, LOW);
doneFlag = true;
}
}
}
else digitalWrite(6, LOW); //turn relay OFF, by cutting power from pin 6
digitalWrite(12, LOW);
delay(1000); // wait 100ms then repeat.