I have been trying to make a control unit for my project(which is a diesel heater).
I would like to know if there is something wrong with this code because my switches sometimes react and sometimes don´t.
I am a newbie to programming so i can use any advice.
thanks in advance for any help
int relay_=8;
int relay_igni=7;
int relay_fan= 6;
int relay_pump = 5;
int switch_start = 2;
int switch_stop = 3;
int switch_half_speed = 4;
int pump_speed_full = 150;
int pump_speed_half = 300;
long fan_cooling = 120000;
boolean runing = false;
boolean half_speed = false;
int switch_state_start = 0;
int switch_state_stop = 0;
int switch_state_half_speed = 0;
void setup() {
pinMode(relay_pump,OUTPUT);
pinMode(relay_fan,OUTPUT);
pinMode(relay_igni,OUTPUT);
pinMode(switch_start,INPUT);
pinMode(switch_stop,INPUT);
}
void loop() {
switch_state_start=digitalRead(switch_start);
switch_state_stop=digitalRead(switch_stop);
switch_state_half_speed=digitalRead(switch_half_speed);
if(switch_state_start == HIGH){
delay(100);
runing = !runing;
} else if(switch_state_stop == HIGH){
delay(100);
runing = !runing;
digitalWrite(relay_fan,HIGH);
delay(fan_cooling);
digitalWrite(relay_fan,LOW);
}
else if(runing == true){
digitalWrite(relay_fan,HIGH);
digitalWrite(relay_pump,HIGH);
delay(pump_speed_full);
digitalWrite(relay_pump,LOW);
delay(pump_speed_full);
}
}
it appears that you recognize a button press when the button is HIGH instead of a change of state and the button being pressed. w/o recognizing the change in state, "runing" will be repeatedly toggled as long as the button is pressed. is this what you want?
it's common practice to wire a button between the input pin and ground so that a button press pulls the pin LOW. this is why larryd suggested configuring the pin as INPUT_PULLUP
in the start case, why not set runing to false instead of toggling runing? what if the start button is hit again? same for stop
and in both cases, why not do whatever processing is necessary to turn things on (start) or off (stop). there's a case for runing being true but not false.
don't understand the processing for runing true. this will be repeated invoked setting relay_pump HIGH and then LOW. shouldn't this only be performed once when runing is set true.
the condition testing for start could also test if runing is also false so those conditions are only met on the first press of the start but when not runing. similar for the stop button
i don't see any processing for the half_speed case
nt relay_=8;
int relay_igni=7;
int relay_fan= 6;
int relay_pump = 5;
int switch_start = 2;
int switch_stop = 3;
int switch_half_speed = 4;
int pump_speed_full = 150;
int pump_speed_half = 300;
Add a naming convention, like:
ALL_CAPS for constants (use "PIN" in the names that represent pin numbers)
InitialCaps for global variables
initialLowerCase for local variables
Add "State Change Detection" and "Debounce" for all mechanical contacts.
const byte START_SWITCH_PIN = 2;
const byte STOP_SWITCH_PIN = 3;
const byte HALF_SPEED_SWITCH_PIN = 4;
const byte PUMP_RELAY_PIN = 5;
const byte FAN_RELAY_PIN = 6;
const byte IGNITION_RELAY_PIN = 7;
const unsigned long PUMP_SPEED_FULL = 150;
const unsigned long PUMP_SPEED_HALF = 300;
const unsigned long FAN_COOLING_INTERVAL = 120000;
boolean Running = false;
boolean HalfSpeed = false;
void setup()
{
pinMode(PUMP_RELAY_PIN, OUTPUT);
pinMode(FAN_RELAY_PIN, OUTPUT);
pinMode(IGNITION_RELAY_PIN, OUTPUT);
pinMode(START_SWITCH_PIN, INPUT_PULLUP); // Active LOW
pinMode(STOP_SWITCH_PIN, INPUT_PULLUP); // Active LOW
pinMode(HALF_SPEED_SWITCH_PIN, INPUT_PULLUP); // Active LOW
}
void loop()
{
unsigned long currentMillis = millis();
static unsigned long lastButtonChangeMillis = 0;
const unsigned long DEBOUNCE_INTERVAL = 30;
boolean startIsPressed = digitalRead(START_SWITCH_PIN) == LOW; // Active LOW
static boolean startWasPressed = false;
boolean stopIsPressed = digitalRead(STOP_SWITCH_PIN) == LOW; // Active LOW
static boolean stopWasPressed = false;
boolean halfSpeedIsPressed = digitalRead(HALF_SPEED_SWITCH_PIN) == LOW; // Active LOW
static boolean halfSpeedWasPressed = false;
if (startIsPressed != startWasPressed &&
currentMillis - lastButtonChangeMillis > DEBOUNCE_INTERVAL)
{
// State Change Detected (debounced)
lastButtonChangeMillis = currentMillis;
startWasPressed = startIsPressed;
if (startWasPressed && !Running)
{
// Start button was just pressed while not Running
Running = true;
digitalWrite(FAN_RELAY_PIN, HIGH);
digitalWrite(PUMP_RELAY_PIN, HIGH);
delay(PUMP_SPEED_FULL);
digitalWrite(PUMP_RELAY_PIN, LOW);
delay(PUMP_SPEED_FULL);
}
}
if (stopIsPressed != stopWasPressed &&
currentMillis - lastButtonChangeMillis > DEBOUNCE_INTERVAL)
{
// State Change Detected (debounced)
lastButtonChangeMillis = currentMillis;
stopWasPressed = stopIsPressed;
if (stopWasPressed && Running)
{
// Stop button was just pressed while Running
Running = false;
digitalWrite(FAN_RELAY_PIN, HIGH);
delay(FAN_COOLING_INTERVAL);
digitalWrite(FAN_RELAY_PIN, LOW);
}
}
if (halfSpeedIsPressed != halfSpeedWasPressed &&
currentMillis - lastButtonChangeMillis > DEBOUNCE_INTERVAL)
{
// State Change Detected (debounced)
lastButtonChangeMillis = currentMillis;
halfSpeedWasPressed = halfSpeedIsPressed;
if (halfSpeedWasPressed)
{
// halfSpeed was just pressed
HalfSpeed = !HalfSpeed; // Toggle state each time button is pressed
}
}
}