Please forgive me this is my first project and also my first post on the forum.
I am trying to make a cctv selector board. I have 3 camera inputs going into a 4051 multiplexer, I have 3 buttons to select each input and i also have a button to set it to cycle for 5 seconds.
The cycle part of the code seems to work however the selector buttons do not.
Please would anyone be kind enough to have a look at my code and tell me where I am going wrong
Thanks
// CCTV SEQUENCER
// setup the pins
int cctvdelay(5000);// delay between changes
int s0 = 2 ;
int s1 = 3 ;
int s2 = 4 ;
int button1 = 50 ;
int button2 = 51 ;
int button3 = 52 ;
int cyclebutton = 53 ;
// run once when the sketch starts
void setup ()
{
// sets the digital pin as an output
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(cyclebutton, INPUT);
}
// startup loop
void loop ()
{
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW); //cctv input 1 is now being displayed
if (digitalRead(button1) == HIGH) {
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW); //cctv input 1 is now being displayed
}
if (digitalRead(button2) == HIGH) {
digitalWrite(s0, HIGH);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW); //cctv input 2 is now being displayed
}
if (digitalRead(button3) == HIGH) {
digitalWrite(s0, LOW);
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW); //cctv input 3 is now being displayed
}
if (digitalRead(cyclebutton) == HIGH) {
cyclecctv ();
}
}
// 5 second cycle
void cyclecctv ()
{
// start 5 second cycle
{
while (digitalRead(cyclebutton) == HIGH) {
loop ();}
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW); //cctv input 1 is now being displayed
delay(5000); // waits for 5000ms
digitalWrite(s0, HIGH);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW); //cctv input 2 is now being displayed
delay(5000); // waits for 5000ms
digitalWrite(s0, LOW);
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW); //cctv input 3 is now being displayed
delay(5000); // waits for 5000ms
}
}
[code]
Thanks for the reply, yes the buttons do have pull down resistors and they do go high when i press them as I have checked them with a voltmeter.
I am sure it is a code issue rather that a hardware one.
Thanks AWOL, I am not too sure what you mean by debug prints, just had a look in the reference section of the Arduino website however couldn't find anything. Could you steer me in the right direction please?
At the beginning of loop you select camera port1, do this default setup in the setup() otherwise you will always revert to camera 1 when not pressing a button and the loop ieterates.
void loop ()
{
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW); //cctv input 1 is now being displayed
if (digitalRead(button1) == HIGH) {
in the above quote:
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW); //cctv input 1 is now being displayed
is the part that needs to happen in setup(), it will hold you on cam1 until you leave the loop() to go to cyclecctv().
Thanks guys thats great advice, I understand now that the initial input select should be done in setup(). Also the tip about the buttons being high and pulling them to ground is a gret one!
dxw00d:
What is this (in void cyclecctv ()) meant to do?
while (digitalRead(cyclebutton) == HIGH) {
loop ();}
This leads me to my next issue..........
What i was trying to achieve was when the 4th button (cyclebutton) is pressed the program goes to cyclecctv() and then that part of the program loops until another press of the 4th button is detected and then the program returns to the loop(). Have I gone the wrong way about doing this??
I am currenly trying to read up on while-loops, is this the right way to go about this or should I use something else?
Your 3 individual channel buttons ORed together would facilitate the exit back to the main loop. Use button 4 for getting into the loop, and selection of any single channel results in an exit back to the main loop.
i realise this was the wrong way to go about this now, I have rewritten that part of the program now........
// 5 second cycle program
void cyclecctv ()
{
while (digitalRead(cyclebutton) == LOW) {
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW); //cctv input 1 is now being displayed
delay(5000); // waits for 5000ms
digitalWrite(s0, HIGH);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW); //cctv input 2 is now being displayed
delay(5000); // waits for 5000ms
digitalWrite(s0, LOW);
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW); //cctv input 3 is now being displayed
delay(5000); // waits for 5000ms
}
// if the cyclebutton is pressed the program will secect input 1 and return to the main loop
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW); //cctv input 1 is now being displayed
}
So now if you repress the cyclebutton the program will return to the loop because it no longer adheres to the 'while' conditions?.......I hope!