help needed with code using 4051 multiplexer

Hi all,

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]

[/code]

Do the buttons have pulldown resistors?

Hi,

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.

regards

Chris

I am sure it is a code issue rather that a hardware one.

Some simple debug prints would help nudge you int the right direction.

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?

What is this (in void cyclecctv ()) meant to do?

    while (digitalRead(cyclebutton) == HIGH) {
      loop ();}

(deleted)

No, I think dxw00d got it, but debug prints would nail it.

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().

If you set the button pins high and pull them low to ground with the buttons, you won't need the pull up resistors.

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!

Thank you all, now off to rewrite my code!

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?

Thanks again for your help!

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.

Have I gone the wrong way about doing this??

About as wrong a way as you could possibly do.
You should never call "loop()" directly.

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!

You should really get rid of the calls to "delay", otherwise you may miss key presses.