float alarm system help

hi all I must say I'm loving the Arduino world I'm a newbie and I have managed to create what I was
looking for but could do with a little bit of help and advice.

I needed a alarm system for my boat where I have 2 float switches 1 in the foul tank for when it gets full and 1 for the clean water tank when its nearly empty each float switch sets of a alarm controlled by a dual relay module. I have managed to get the float switches working and each setting off in1 and in2 on the relay module but what I could do with help with is someone just looking over my sketch to see how it looks and if anything needs smoothing out.

I also would like some help in adding a push button delay I know a delay will kill the board all the time its activated but that does not worry me I have googled how to do it but some of it just isn't making sense to me like does it need to be in any sort of sequence on the loop or can it go anywhere? I have read about millis but not sure if this is what I need I would like the delay to be for an hour or 2 any help would be appreciated.

thanks in advance
Daniel

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;
const int relay = 10;
const int buttonPin1 = 3;
const int relay1 = 11;

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {

pinMode(relay, OUTPUT);
pinMode(relay1, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
}

void loop() {

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED on:
digitalWrite(relay, HIGH);
} else {
// turn LED off:
digitalWrite(relay, LOW);
} {
buttonState = digitalRead(buttonPin1);

if (buttonState == LOW) {

digitalWrite(relay1, HIGH);
} else {

digitalWrite(relay1, LOW);
}

}
}

Workingfloatalarm.ino (1.38 KB)

Some suggestions :

Use byte instead of int for pin numbers to save memory. Not important in this program but in a larger one it might be.

It is impossible to tell from just the program, but do you have any pull up resistors on the input pins to keep them in a known state ? If not then use INPUT_PULLUP in pinState() to activate the built in resistors.

Do the relays really turn on when their input pins go HIGH ? If so, that is unusual. I suspect that you have the logic of reading the inputs and setting the outputs the wrong way round, but maybe not.

Make the comments match the code. LEDs are mentioned in the comments but you are not using any

Unnecessary curly braces around reading and acting upon buttonPin1

For neatness rename the variables to buttonPin1 and buttonPin2, relay1 and relay2

You can put the delay() anywhere in loop() but bear in mind that unless you program it otherwise the relays will stay in whatever state they are in when the delay() starts. If you were to use millis() for timing then loop() would continue to be executed and you could have another button to bring the delay period to an end early if required.

hi UKHeliBob

thanks for taking your time to reply I will upload a few photos of what I have just made up with a kit I ordered a week ago now that's all ive had my Arduino for so please forgive the newbie question.

my original sketch started off as the example sketch for a button I then added the next bit because I have 2 switches and not 1. I don't know why or what ive done wrong but yes my sketch does seem to be backwards from what was originally there but when I first uploaded the sketch the relays were already activated before I even put them in a high state do you know why this could be? The onboard LED on pin 13 is also on is this correct?

I am using external resistors so that bit isn't needed I have a red cable coming off of my breadboard from 5v that I use to activate my relays just by touching it on to each cable acting as the float switch when I touch the pin input the relay activates

any help is really appreciated
thanks

it wont let me upload any pictures they say they are too large

It is normal for the relays used with the Arduino to be activated by taking their input LOW, hence my comment. As to the inputs, I would strongly suggest that you use INPUT_PULLUP in pinMode() and arrange for the pins to be taken LOW when activated. This means that there is less wiring than using external resistors so less chance of the circuit being wrong or failing.

UKHeliBob

if I could buy u a beer I would I love learning I rewrote my code again trying the method you said and it all now works the right way round without the external resistors I cant thank you enough.

im just getting stuck with my delay and if it is even possible?? say my foul tank float goes high and sets off the relay module to activate setting off my 12v buzzer is it then possible to silence the buzzer using a delay via a push button for say an hour or 2 because that's how long it will take me to get the tank empty and for the float switch to go back to low am I looking in the right direction as like I say I don't mind the whole board being disabled for an hour or am I looking at the wrong method?

thanks again
Daniel

here is my new code I just done a quick mock up instead of messing with my original but now I know it works I can change it this is without me using external resistors and turning off the internal LED

const int buttonPin = 2;
const int buttonPin1 = 3;
const int ledPin = 11;
const int ledPin1 = 10;// the number of the LED pin
const int intPin = 13;

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin1, OUTPUT);

pinMode(buttonPin1, INPUT_PULLUP);
pinMode(intPin, OUTPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
} {
buttonState = digitalRead(buttonPin1);

if (buttonState == HIGH) {

digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1, LOW);
}
} { digitalWrite(intPin, LOW);
} }

is it then possible to silence the buzzer using a delay via a push button for say an hour or 2

You don't want to do that.

Instead, save the value of millis() when you push the button to silence the buzzer. Then, each time through loop() check whether the current value of millis() minus the start value is greater than the required period. If not, then keep going round loop() doing whatever yo need to until the period ends then act on it.

Using millis() for timing in this way allows the program to keep running rather than being stalled for a period.

An even easier way to silence the buzzer would be to have it only sound when a boolean variable is set to true and the buzzer trigger conditions are true. When you push the "silence that bl**dy buzzer" button set the boolean to false and the buzzer will stop sounding.

thanks for the advice ill start reading up on these methods and let you know what I come up with

thanks

Daniel