if (v2 <= 5.39) { digitalWrite (4,HIGH);
}
else if (v2 >= 5.45) { digitalWrite (4,LOW);
}
I connect a Relay to pin4
I want my code stop at this point and not continue until I press a push button
so if my relay turned ofF it will be forever OFF until i press a button
Here's an example with cheap button (a jumper that you ground on the USB connector) that pushed, lights led13 after 1 second and pushed again, stops the led blinking.
Run it, mess with it. The serial output is 115200 baud, make sure your monitor is the same.
// All you need to do this example is an Arduino and a button on pin 2.
// Push the button once, led lights after delay. Again, led turns off.
// Button is debounced and wired directly to ground. The button can be a
// jumper from pin 2 grounded on USB connector. Tested with jumper.
// By GoForSmoke for free public use. Using Arduino 1.0.5-r2
const byte ledPin = 13; // this is the onboard led pin
const byte buttonPin = 2;
byte setLedPin = 0;
const unsigned long ledDelay = 1000UL; // blink interval
unsigned long ledDelayStart = 0U; // 16 bit blink (on or off) time ms
// button variables
byte buttonRead; // wired for pullup -- if down then LOW, if up then HIGH
byte lastButtonRead = HIGH; // so debounce knows previous read
byte checkDebounce = 0; // only checks decounce after every button pin change
byte lastButtonState = 0; // last stable button state
byte buttonState = 0; // stable button state
// 0 = button is up after debounce
// 1 = button is down after debounce
// button debounce timing variables
const unsigned long debounceDelayMs = 100UL;
unsigned long debounceStartMs;
unsigned long msNow;
byte processState = 0; // 1st press to start, 1st release, 2nd press to stop, 2nd release
void setup()
{
Serial.begin( 115200 );
pinMode( ledPin, OUTPUT ); // default is LOW
pinMode( buttonPin, INPUT_PULLUP ); // my button connects to ground, not 5V
// however that means that when the button is pressed the pin is LOW.
}
void loop() // make sure that loop() runs fast and stays in the "now".
{
// BUTTON CODE BLOCK, it handles debouncing.
// the task is to set the variable buttonState when a Stable Button State is reached.
// other sensor code could change the same variable if desired
// read the pin which may be changing fast and often as the button bounces
buttonRead = digitalRead( buttonPin ); // momentary state
msNow = millis();
if ( buttonRead != lastButtonRead )
{
debounceStartMs = msNow;
checkDebounce = 1;
}
else if ( checkDebounce )
{
if ( msNow - debounceStartMs >= debounceDelayMs ) // stable button state achieved
{
buttonState = !buttonRead; // mission accomplished, button is stable
// note that buttonState is opposite buttonRead
checkDebounce = 0; // stop debounce checking until pin change
}
}
lastButtonRead = buttonRead;
//
// End of the BUTTON CODE BLOCK
//==================================================================================
// CONTROL CODE BLOCK that uses buttonState and processState
if ( lastButtonState != buttonState )
{
lastButtonState = buttonState;
Serial.println( F( "============================================================" ));
Serial.print( F( "processState " ));
Serial.print( processState );
Serial.print( F( " buttonState " ));
Serial.println( buttonState );
Serial.print( F( " millis " ));
Serial.println( msNow );
Serial.println( F( "============================================================" ));
switch ( processState )
{
case 0: // waits for press without blocking
if ( buttonState == 1 ) // button is pressed
{
processState = 1;
setLedPin = 1; // start led blink task
ledDelayStart = millis(); // led blink control value set in the control block
}
break;
case 1: // waits for release without blocking
if ( buttonState == 0 ) // button is released
{
processState = 2;
}
break;
case 2: // waits for press without blocking
if ( buttonState == 1 ) // button is pressed
{
processState = 3;
setLedPin = 2; // led blink control value set in the control block
}
break;
case 3: // waits for release without blocking
if ( buttonState == 0 ) // button is released
{
processState = 0; // start over
}
break;
}
}
// End of the CONTROL CODE
//==================================================================================
// LED BLINK CODE BLOCK
if ( setLedPin == 1 )
{
if ( millis() - ledDelayStart >= ledDelay )
{
digitalWrite(ledPin, HIGH );
setLedPin = 0;
}
}
else if ( setLedPin == 2 )
{
digitalWrite(ledPin, LOW );
setLedPin = 0;
}
// End of the LED BLINK CODE
//==================================================================================
// Want to add serial commands and args input?
// this is a good spot. Serial is so slow it can usually go last.
}
ok PaulS I will use your way
Please bring a cub of coffee because your brain will hurt you after this, it's a complicated problem.
This is my circuit
When I run the circuit, voltmeter read "0" because my relay is OFF then the relay turn ON because it's under 5.39V, now because my relay is ON the voltmeter will read 5.75V, because my power source is 5.75V, and because my power coerce is higher than 5.40 so my relay will turn off again and enter in infinity loop every 8 second relay ON then OFF,
So for that i want to add a push button or anything to avoid this.
to be more clear this is my code
I am using this to charge my lithium battery from a power source, and it will turn the charger off when the battery is fully charged, i can't connect the voltmeter to my lithium battery, voltmeter must be connected to both "battery and my power source to read the Voltage correctly"
forget about the temp sensor and the charger, anyway i am using charger 5.75 V not AA battary like the pic
I am using 9V-1A for my Arduino, everything under-control, That's not my fully circuit, I made a simple pic for this problem to solve it Hysteresis no it's my first time, I will read about it
narzan:
I am using 9V-1A for my Arduino, everything under-control, That's not my fully circuit, I made a simple pic for this problem to solve it Hysteresis no it's my first time, I will read about it
A digital pin uses hysteresis. Going from LOW to HIGH, voltage must pass about 3V (on 5V chip) to be HIGH but once HIGH must drop to almost 1V to switch to LOW.
A thermostat uses hysteresis. The difference between "cold, turn on" and "hot turn off" is a few degrees to prevent the heater from flickering itself broken.
( < 5.39V ) and ( > 5.4V ) is close for many things at .2V hysteresis but glad it is working now!
You are feeding the 9V to Arduino external power socket? That uses a 7805 regulator. The heat it makes is the excess 4V getting dropped to ground. Look for DC DC (buck) converter to turn your 9V 1A into 5V 1.4A to feed directly to VIN. If you run on batteries, it will make them last longer.