Go Down

Topic: Button question (Read 148 times) previous topic - next topic

stonemason

Aug 01, 2020, 04:35 pm Last Edit: Aug 01, 2020, 04:38 pm by stonemason
Hi All

My first post to this forum. I'm an absolute beginner coding Arduino sketches so would like some advice please. I'm trying to trigger a camera shutter using a sound detector and have managed to create code and a breadboard layout which does just that. My problem is that the sensor is continually listening for noise and each time it hears any it triggers the camera, which is not helpful when I'm trying to set up a shot. What I would like to do is to press a momentary switch which arms the system to listen for noise, fires the camera shutter when it hears the noise then sends the system into inactive mode until the button is pressed again. I have absolutely no idea how to achieve this despite reading countless articles on using buttons, none of which worked on my system. Any help would be much appreciated.
My code is as follows

Code: [Select]


int flash = 3; //flash is on pin 3
int soundSensor = 4; //sound sensor is on pin 4
int LED = 5; //LED is on pin 5 and is used for testing if the code works

void setup() {
    pinMode(flash, OUTPUT); //sets the flash as an output
    pinMode(soundSensor, INPUT); //sets the sound sensor as an input
    pinMode(LED, OUTPUT); //sets the LED as an output
}

void loop() {
    int sensorInfo = digitalRead(soundSensor);
    //a variable created for the data taken in by the sound sensor

    digitalWrite(flash, LOW); //start with the flash off
    digitalWrite(soundSensor, HIGH); //Sound sensor ready to detect
    digitalWrite(LED, LOW); //start with the LED off

    if (sensorInfo == LOW) {
        delay (25); //wait for the flash to settle down
        digitalWrite(LED, HIGH); //fires flash
        digitalWrite(flash, HIGH); //turns LED on
        delay (70); //keep the flash on for 70th of a second
        digitalWrite (LED, LOW); //turns LED off
        digitalWrite (flash, LOW); // turns flash off
        delay (2000); //wait 2 seconds for the system to reset
    }
}

Robin2

You need a variable, let's call it sensorMayOperate, and all the code you have now in loop() should be inside an IF statement

Code: [Select]
if (sensorMayOperate == true) {
  // existing code
}


Then (I think it should be at the bottom of your if (sensorInfo == LOW) you need to insert the line
Code: [Select]
sensorMayOperate = false;
which will prevent another event.

Then all you need is a little bit of code that sets sensorMayOperate to true when a button is pressed.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

groundFungus

Here is example code to show how to toggle the state of a variable (armed) with a push button switch.  Only listen for noise when armed is true.

Code: [Select]
// by C Goulding aka groundFungus


const byte  buttonPin = 4;    // the pin that the pushbutton is attached to
const byte ledPin = 13;       // the pin that the LED is attached to

boolean buttonState = 0;         // current state of the button
boolean lastButtonState = 0;     // previous state of the button

boolean armed = false;

void setup()
{
   // initialize the button pin as a input with internal pullup enabled
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(9600);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check switch 20 times per second
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);
      // compare the new buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            armed = !armed;  // toggle armed state
            digitalWrite(ledPin, !digitalRead(ledPin));  // toggle led state

            Serial.print("armed condition  ");
            Serial.println(armed);
         }
         // save the current state as the last state,
         //for next time through the loop
         lastButtonState = buttonState;
      }

   }
}

You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

stonemason

Thanks for the swift replies. groundFungus I've tried incorporating your code with mine but without much success . I'm sure this is my lack of knowledge and would appreciate it if you could give me a bit more guidance on how to do that. The LED is only there as a circuit checker and indicates that the sound sensor is working and is a visual indicator that the camera has fired.

Many thanks

slipstick

Why don't you post your changed code and then we can help you get it working?

Steve

stonemason

This is the incorporated sketch. The sound trigger still detects sound and fires the flash (camera) but the button has no effect.

Thanks for the help.

Code: [Select]


const byte  buttonPin = 9;    // the pin that the pushbutton is attached to
int flash = 3; //flash is on pin 3
int soundSensor = 4; //sound sensor is on pin 4
int LED = 5; //LED is on pin 5 and is used for testing if the code works

boolean buttonState = 0;         // current state of the button
boolean lastButtonState = 0;     // previous state of the button

boolean armed = false;

void setup()
{
  pinMode(flash, OUTPUT); //sets the flash as an output
  pinMode(soundSensor, INPUT); //sets the sound sensor as an input
  pinMode(LED, OUTPUT); //sets the LED as an output
  // initialize the button pin as a input with internal pullup enabled
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  static unsigned long timer = 0;
  unsigned long interval = 50;  // check switch 20 times per second
  if (millis() - timer >= interval)
  {
    timer = millis();
    // read the pushbutton input pin:
    buttonState = digitalRead(buttonPin);
    // compare the new buttonState to its previous state
    if (buttonState != lastButtonState)
    {
      if (buttonState == LOW)
      {
        armed = !armed;  // toggle armed state
        Serial.print("armed condition  ");
        Serial.println(armed);
        int sensorInfo = digitalRead(soundSensor);
        //a variable created for the data taken in by the sound sensor

        digitalWrite(flash, LOW); //start with the flash off
        digitalWrite(soundSensor, HIGH); //Sound sensor ready to detect
        digitalWrite(LED, LOW); //start with the LED off

        if (sensorInfo == LOW) {
          delay (25); //wait for the flash to settle down
          digitalWrite(LED, HIGH); //fires flash
          digitalWrite(flash, HIGH); //turns LED on
          delay (70); //keep the flash on for 70th of a second
          digitalWrite (LED, LOW); //turns LED off
          digitalWrite (flash, LOW); // turns flash off
          delay (2000); //wait 2 seconds for the system to reset
        }
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }

}

dougp

You have everything under one if() statement.  Rearrange things along the lines of:

if switch pressed and debounced
{
  set armed=true;
}

Under a separate section of code:

if(armed == true)
{
    if sensor indicates{
   {
      trigger flash
      reset armed = false;
   }
}
I don't trust atoms.  They make up everything.

No private consultations undertaken!

groundFungus

#7
Aug 02, 2020, 06:02 pm Last Edit: Aug 02, 2020, 06:03 pm by groundFungus
How to wire the switch.
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

Go Up