Go Down

Topic: Button and Switch (Read 321 times) previous topic - next topic

tylernol

I am trying to create a switch for two security cameras in my house. I've successfully made a program that automatically switches between the cameras every second but I would like to add more to it. My goal is to have a toggle switch that changes between automatically changing the cameras when the switch is off and being able to manually select the camera with a button when the switch is on. This is the first question I've posted on the forum so please excuse any mistakes I might make in forum etiquette. Here is the code I have so far.

/*
  Timed Camera Switch
  Switches camera inputs every 3 seconds
*/

// Pins 0,1 and 2 are going to control the relays for the cameras.
// Pins 5 and 6 are the switch and button
// assign names to the pins being used:
int stateswitch = 5;
int button = 6;
int frontcam = 0;
int backcam = 2;

void setup() {               
  // initialize the digital pins as an outputs.
  pinMode(stateswitch, INPUT);
  pinMode(button, INPUT);
  pinMode(frontcam, OUTPUT);
  pinMode(backcam, OUTPUT); 
}

void loop() {
  buttonState = digitalRead(stateswitch);
  if (buttonState == HIGH) {
    //This is where I need to know what to put!
  }
  else {
    digitalWrite(frontcam, HIGH);   // turn frontcam on
    delay(1000);                    // wait for one seconds
    digitalWrite(frontcam, LOW);    // turn frontcam off
    delay(25);                      // delay between camera switch
    digitalWrite(backcam, HIGH);    //turn backcam on
    delay(1000);                    //wait for one seconds
    digitalWrite(backcam, LOW);     //turn backcam off
    delay(25);                      // delay between camera switch
  }
}

HazardsMind

Well you can use a simple if pressed once, show this feed. If pressed again switch to other feed. It is extremely simple to do what you want to do. Also don't switch the feeds into the arduino, instead use a SPDT relay.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

tylernol

I am actually using several reed relays. One per input. I'll attach a picture of the circuit that I made. Could you give me an example?

HazardsMind

#3
Jan 29, 2013, 12:29 pm Last Edit: Jan 29, 2013, 02:02 pm by HazardsMind Reason: 1
Code: [Select]

byte Button_Pin =2;
int Button_Feed;


void setup() {
.
.
}

void loop() {
Button_Feed = digitalRead(Button_Pin);

if(Button_Feed == HIGH) {
// do this
}
else // do this

}


Now being that you want to switch multiple feeds from a single button, you need to get a little creative.

Code: [Select]

byte Button_Pin =2;
int Button_Feed;
int Press_Counter = 0;
int Number_of_feeds = 4;


void setup() {
.
.
}

void loop() {
  Button_Feed = digitalRead(Button_Pin);
  // Add debounce code here
  if(Button_Feed == HIGH) { // include a latch code to allow one feed switch per press

    if(Press_Counter != Number_of_feeds) {
        Press_Counter++;
        //switch relays here
        }
     }
  //else //get rid of ELSE statement, its not needed anymore
}
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Go Up