Overclock.net - An Overclocking Community - View Single Post - Arduino code help, way out of my depth

View Single Post
post #1 of (permalink) Old 02-28-2018, 09:54 PM - Thread Starter
5291Crash
New to Overclock.net
 
5291Crash's Avatar
 
Join Date: Dec 2004
Location: Landsowne, Ontario, Canada
Posts: 563
Rep: 26 (Unique: 26)
Arduino code help, way out of my depth

Hey all

I am trying to make a simple controller on a Nano board, that watches A0, A1, and A2 for voltage reference from a voltage divider circuit, and puts the to the output

The idea being that the controller will look at the inputs in order A0-A2 and always give preference to the first one that meets criteria.

i am using 12vdc , setting cut off points at 11.8-14.5v

Inputs
A0 - Wall wart power
A1 - External Batt
A2 - Internal batt

Outputs are
D11 - LED and mosfet for power relay
D12 - LED and mosfet for power relay
D13 - LED and mosfet for power relay

This is where i hit a coding wall from pure lack of knowlege
In basic i need it to look at A0 and if its in range use it turning on d11, but if its not in range move to A1 doing the same. BUT it can only run one output at a time.


Kind of like this

if a0 <=11.8 but >=14.5 put d11 high, else d11 low

if d11 high skip to end else do next

if a1 <=11.8 but >=14.5 put d12 high, else d12 low

if d12 high skip to end, else do next

if a2 <=11.8 but >=14.5 put d13 high, else d13 low.

end

This is my current unfinished code i have modded from a single i/o setup that worked on my test bench.
Code:
/*
  DC Voltmeter
  An Arduino DVM based on voltage divider concept
  T.K.Hareendran
  This is reworked into a voltage controller monitoring 3 inputs to chose the right output.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int a0 = 0; // Main PSU input voltage of 12.5 or better
const int a1 = 1; // External Battery input voltage range no lower than 11.8v flooded
const int a2 = 2; // Internal Battery input voltage range no lower then 11.8v sla
const int led_d11 = 11; // Controls LED and power mosfet for PSU
const int led_d12 = 12; // Controls LED and power mosfet for External Battery
const int led_d13 = 13; // Controls LED and power mosfet for Internal Battery
float vout0 = 0.0;
float vout1 = 0.0;
float vout2 = 0.0;
float vin0 = 0.0;
float vin1 = 0.0;
float vin2 = 0.0;
float R1_0 = 4610.0; // R1 input A0(4.7K) -Measure for exact!
float R2_0 = 2163.0; // R2 for input of A0(2.2K) - Measure for exact!
float R1_1 = 4700.0; // resistance of R1 for input of A1(4.7K) -Measure for exact!
float R2_1 = 2200.0; // resistance of R2 for input of A1(2.2K) - Measure for exact!
float R1_2 = 4700.0; // resistance of R1 for input of A2(4.7K) -Measure for exact!
float R2_2 = 2200.0; // resistance of R2 for input of A2(2.2K) - Measure for exact!
int value = 0;
void setup() {
  pinMode(a0, INPUT), (a1, INPUT), (a2, INPUT);
  pinMode(led_d11, OUTPUT), (led_d12, OUTPUT), (led_d13, OUTPUT);
  lcd.begin(20, 4);
  lcd.print("Batt Mon V1.0");
}
void loop() {
  // read the value at analog input
  vout0 = (value * 3.98) / 1023.0; // see text 4.5 being reference voltage
  vout1 = (value * 3.98) / 1023.0; // see text 4.5 being reference voltage
  vout2 = (value * 3.98) / 1023.0; // see text 4.5 being reference voltage
  value = analogRead(a0);
  vin0 = vout0 / (R2_0 / (R1_0 + R2_0));
  if (vin0 < 0.09) {
    vin0 = 0.0; //statement to quash undesired reading !
  }
  lcd.setCursor(0, 1);
  lcd.print("PSU V= ");
  lcd.print(vin0);
  lcd.setCursor(0, 2);
  lcd.print("EXT BATT V= ");
  lcd.print(vin0);
  lcd.setCursor(0, 3);
  lcd.print("INT BATT V= ");
  lcd.print(vin0);
  if (vin0 < 2.5) {  // <------------- This is where i am trying to sort out (number values are from initial test bed i have now built a 3 output 0-13.8v test bed)
    digitalWrite(led_d13, HIGH);
  } else {
    digitalWrite(led_d13, LOW);
  }
  if (vin0 > 3.0) {
    digitalWrite(led_d13, HIGH);
  } else {
    digitalWrite(led_d13, LOW);
  }
  delay(500);
}
screen output i will tinker with later, i may not use it.
reference voltage is from a 4.5v with ±0.04% tolerance.
I want to use this as i can mod the code to fit whatever voltages i need. unlike something bought.

I'm not asking for finished code, but to be pointed in the right direction on getting it done.

Thanks all,
Joe

Ryzen
(12 items)
CPU
Ryzen 5 2600X
Motherboard
MSI x470 Gaming Plus
GPU
MSI Armour RX 570 8GB OC
RAM
Team Group TDGED416G3000HC16CDC01 2x8GB 3000Mhz C16
Hard Drive
WD Black 500 GB NVMe
Power Supply
Rosewill Capstone 750
Cooling
Cheap 240 AIO
Case
Rosewill Rise
Operating System
Win 10 Pro
Keyboard
Microsoft Sidewinder x4
Mouse
Logitech G300s
Audio
Audio Technica M50xBT
▲ hide details ▲
5291Crash is offline