Ok this is part of my main sketch. millis() works well. the jist of the code is when I turn on switches on my remote 5 and 6. everything works like its suppose to unless voltage divider is reading between 24 and 25 volts then mowerpin will shut off and on to tell me the battery is getting low. when channel 5 reads to voltage is under 24 volts the diconnectpin will shut off Question is? Is there a way to adjust the delay times? its set for 1000 what if I wanted to have 2 secs off 10 sec on? Also when the millis() is working my test led on disconnectpin was fantly flashing when the ch5 switch is off. Any thoughts as to why it would do that. I tried to trouble shoot and fried my 328 when i turn on the internal pull up (not wise to do if you already have a external pull down doh).
Thanks
const int voltPin = 0; //Analog volt read pin
float denominator; //Variables for voltage divider
int resistor1 = 10000; //Check resistors with ohm meter and adjust according
int resistor2 = 1870;
int disconnectpin = 4; // Main disconnect
int mowerpin = 7; // Mower motor
int mowerstate= LOW;
long previousMillis = 0;// will store last time mowerpin was updated
long interval = 1000; // interval at which to delay (milliseconds)
int servo5 = 11; // r/c channel 5
int servo6 = 12; // r/c channel 6
int(ch5);
int(ch6);
void setup(){
Serial.begin(9600);
//Convert resistor values to division value
// R2 / (R1 + R2)
denominator = (float)resistor2 / (resistor1 + resistor2);
pinMode(servo5, INPUT); //Pin 5 as input
pinMode(servo6, INPUT); //Pin 6 as input
pinMode(disconnectpin, OUTPUT); //Pin 4 as output
pinMode(mowerpin, OUTPUT); //Pin 7 as output
analogWrite(disconnectpin, 0);
analogWrite(mowerpin, 0);
}
void loop() {
float voltage;
//Obtain RAW voltage data
voltage = analogRead(voltPin);
//Convert to actual voltage (0 - 5 Vdc)
voltage = (voltage / 1024) * 5.0;
//Convert to voltage before divider
// Divide by divider = multiply
// Divide by 1/5 = multiply by 5
voltage = voltage / denominator;
ch5 = pulseIn(servo5, HIGH, 20000); // Read the pulse width of each channel
ch6 = pulseIn(servo6, HIGH, 20000);
// Channel 6 mower switch. If voltage is between 24 and 25 volts mowerpin will turn off and on.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval && voltage >= 24 && voltage <= 25) {
// save the last time checked state
previousMillis = currentMillis;
if (mowerstate == LOW)
(mowerstate = HIGH);
if
(mowerstate = LOW);{
digitalWrite(mowerpin, mowerstate);
}
}
else if (ch6 >= 1900 && ch6 <= 2200){
digitalWrite(mowerpin, HIGH); //Also if voltage is above 24 volts
}
else {
digitalWrite(mowerpin, LOW); //If channel 5 is below 1900 or voltage is below 24 volts. disconnectpin will be on
}
// channel 5 disconnect switch with shut off if voltage falls below 24 volts
if(ch5 >= 1900 && ch5 <= 2200 && voltage > 24){ //If channel 5 is between 1900 and 2200 disconnectpin will be off
digitalWrite(disconnectpin, HIGH); //Also if voltage is above 24 volts
}
else {
digitalWrite(disconnectpin, LOW); //If channel 5 is below 1900 or voltage is below 24 volts. disconnectpin will be on
}
}