Hello Everyone ...,
I want to create a logic in my code such as If my Battery Voltages is Less than the Threshold Values (11.6 volts) then Circuit should stop working; I have already created the voltage divider circuit and it works like a charm; I don't want to use any relays.
Code Below is as an example; I only want to create a logic in my code like that
If (load connected) {
if(voltage < 11.6) {
disconnect load;
}
} else {
if (voltage > 11.6 ) {
connect load;
}
}
Below is the code that I'm currently working on and i want to add If my 12 volts Battery Voltages is Less than the Threshold Values then Circuit should stop working.
I'm using two motors and On a serial monitor screen, i'm controlling its speed like that when I enter "<A+010>" means motorA runs at slow speed or "<A+100>" full speed or <A+000>" stop, similar goes to motor B as well <B+000> motor B stop <B+010> slow speed <B+100> full speed.,
to run both motors like this <C+000> <C+010> <C+100>. on a serial monitor and it shows voltages on a serial monitor screen as well.
#include <Servo.h>
const int m_1 = 5;
const int m_2 = 6;
const int LED = 13;
const int Battery = A0;
float Voltage = 0.0; //input battery voltage
float Voltage1 = 0.0; //output voltage for analog pin A0
float R1 = 1590; // R1 =1.590
float R2 = 1000; // R2 =1000
int readValue = 0;
String strID = "";
int BaudRate = 4800;
Servo ESC_1; // create servo object to control the ESC 1
Servo ESC_2; // create servo object to control the ESC 2
#define FWD 0
#define BWD 1
unsigned int FORWARD = 2000;
unsigned int NEUTRAL = 1500;
unsigned int REVERSE = 1000;
unsigned int APwm = NEUTRAL;
unsigned int BPwm = NEUTRAL;
boolean ADirection = FWD;
boolean BDirection = FWD;
unsigned int AVelocity = 0; // CURRENT VELOCITY OF M1 in 0-100%
unsigned int BVelocity = 0; // CURRENT VELOCITY OF M2 in 0-100%
String inputString = "";
int TaskNumber = 0;
unsigned int GetValue(void)// fetch three digit number value from received string and convert to INTEGER
{
unsigned int Val = 0;
Val = (inputString[3] - 48) * 100;
Val = Val + (inputString[4] - 48) * 10;
Val = Val + (inputString[5] - 48);
return (Val);
}
void setup()
{
Serial.begin(BaudRate); //RX0 for Radiolink
ESC_1.attach(m_1);// (pin, min pulse width, max pulse width in microseconds)
ESC_2.attach(m_2);// (pin, min pulse width, max pulse width in microseconds)
pinMode(LED, OUTPUT);
pinMode(Battery, INPUT);
Serial.println("System Started");
ESC_1.writeMicroseconds(APwm);
ESC_2.writeMicroseconds(BPwm);
}
boolean AFwdFlag = 0; //makes sure that the serial only prints once the state
boolean BFwdFlag = 0; //makes sure that the serial only prints once the state
void loop()
{
readValue = analogRead(Battery);
Voltage1 = (readValue * 5.0) / 1023;
Serial.print("$ , Voltages at Analog Pin A0 = ");
Serial.print(Voltage1);
Serial.print(" Volts");
Voltage = Voltage1 / (R2/(R1+R2));
Serial.print("\n,Battery Voltages = \r");
Serial.print(Voltage);
Serial.print("\r Volts");
if (Voltage > 11.60) //
{
Serial.print(", GOOD BATTERY,\n");
}
else if (Voltage < 10.00)
{
Serial.print(", LOW BATTERY,\n");
}
//serialEvent();
if(TaskNumber){
if(TaskNumber==1) Serial.println("OK");
if(TaskNumber==2 || TaskNumber==4) {ESC_1.writeMicroseconds(NEUTRAL); }
if(TaskNumber==3 || TaskNumber==4) {ESC_2.writeMicroseconds(NEUTRAL);}
if(TaskNumber==5 || TaskNumber==7) {
if(ADirection==FWD) { APwm= NEUTRAL + (((FORWARD-NEUTRAL)/100)*AVelocity); ESC_1.writeMicroseconds(APwm); AFwdFlag=1; }
else {
if(AFwdFlag==1)
{
ESC_1.writeMicroseconds(NEUTRAL);delay(100);
ESC_1.writeMicroseconds(REVERSE);delay(100);
ESC_1.writeMicroseconds(NEUTRAL);delay(100);
AFwdFlag=0;
}
APwm= NEUTRAL - (((NEUTRAL-REVERSE)/100)*AVelocity); ESC_1.writeMicroseconds(APwm);
}
}
if(TaskNumber==6 || TaskNumber==7) {
if(BDirection==FWD) { BPwm= NEUTRAL + (((FORWARD-NEUTRAL)/100)*BVelocity); ESC_2.writeMicroseconds(BPwm); BFwdFlag=1; }
else {
if(AFwdFlag==1)
{
ESC_2.writeMicroseconds(NEUTRAL);delay(100);
ESC_2.writeMicroseconds(REVERSE);delay(100);
ESC_2.writeMicroseconds(NEUTRAL);delay(100);
BFwdFlag=0;
}
BPwm= NEUTRAL - (((NEUTRAL-REVERSE)/100)*BVelocity); ESC_2.writeMicroseconds(BPwm);
}
}
TaskNumber=0;
}
// <AN> <A+xxx> <A-xxx>
// <BN> <B+xxx> <B-xxx>
// <CN> <C+xxx> <C-xxx>
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\r' || inChar == 10)
{
if(inputString[0]=='<')
{
if(inputString[1]=='A' && inputString[2]=='T' && inputString[3]=='>' ) TaskNumber=1; // AT
if(inputString[2]=='N' && inputString[3]=='>' )
{
if ( inputString[1] == 'A') TaskNumber = 2; // Set Motor A to Neutral
if ( inputString[1] == 'B') TaskNumber = 3; // Set Motor B to Neutral
if ( inputString[1] == 'C') TaskNumber = 4; // Set Both Motors to Neutral
}
if(inputString[6]=='>' )
{
if ( inputString[1] == 'A') {
if(inputString[2]=='+') ADirection=FWD; else ADirection=BWD;
AVelocity=GetValue();
TaskNumber=5;
}
if ( inputString[1] == 'B') {
if(inputString[2]=='+') BDirection=FWD; else BDirection=BWD;
BVelocity=GetValue();
TaskNumber=6;
}
if ( inputString[1] == 'C'){
if(inputString[2]=='+') { ADirection=FWD; BDirection=FWD;}
else {ADirection=BWD; BDirection=BWD;}
AVelocity=GetValue();
BVelocity=GetValue();
TaskNumber=7;
}
}
}
inputString = "";
}
}
}
I also tested the LED code such as if voltages >11.6 volts LED on and if voltages<11.6 volts then LED off ... please don't give me an example like that.
Please help me ...