C4 Prop

I have made a program and have some hardware set up except instead of a wireless receiver and transmitter I’m just using a button for testing purposes. Whenever I run the program it goes through each section ignoring the switch cases and just loops through the whole thing over and over. I pasted the code below, any help would be great.

P.S. I’m still new at this.

const int speakerPin = 11;  //Pin buzzer is connected to
const int reciever = 0;  //Pin for wireless reciever
const int LED = 13;  //Sets pin for LED that tells if bomb is armed
const int button = 1;  //Sets pin for arming button

#define READY 0  //Case for bomb being ready
#define ARMED 1  //Case for bomb being armed 
#define DETONATED 2  //Case for bomb being detonated

int bombState = READY;  //Bomb starts out ready
int i = 0;  //Int for the 3 beeps

void setup() {
  Serial.begin(9600);
  pinMode(speakerPin, OUTPUT);  //Sets output for buzzer
  pinMode(LED, OUTPUT);  //LED telling you if the bomb is armed
  pinMode(reciever, INPUT);  //Wireless reciever is an input
  pinMode(button, INPUT);  //Button is an input
}

void loop() {
  switch (bombState) {
    
    //The bomb is waiting to be armed
    case READY:
    if (digitalRead(button) == HIGH) {
      digitalWrite(LED, HIGH);  //Bomb is armed turn LED on
      bombState = ARMED;
    }
    break;

    //The button has been pressed and it now awaits the wireless signal
    case ARMED:
    if (digitalRead(button) == HIGH) {
      digitalWrite(LED, LOW);  //Bomb is no longer armed turn LED off
      bombState = READY;
    }
    if (digitalRead(reciever) == HIGH) {
      bombState = DETONATED;
    }
    break;

    //The bomb has recieved the signal and is detonated
    case DETONATED:
    for (i = 0; i <= 2; i++) {
      tone(speakerPin, 5000, 100);  //Beeps 3 times at 5000Hz
      delay(150);
    }
    delay(400);
    tone(speakerPin, 4500, 3000);  //One long beep at 4500Hz
    delay(3000);
    digitalWrite(LED, LOW);  //Bomb is no longer armed turn LED off
    bombState = READY;
    break;
  }
}

Pin 0 and 1 are you serial out and are connected to the USB. You can not use 0 and 1 for buttons and use Serial.begin at the same time. It is most likely the cause of some of your problems.

Change your receiver and button to pins 2 and 3 or any other open digital pin.

I am assuming this something harmless like a toy or fireworks device.

Mark

Thanks for the reply! Yeah it is completely harmless, just plays a loud sound when it goes off, I was gonna use it in air soft battles. The serial com wasn't initially in there actually and it still didn't work. Is there any other problems besides that? I added the serial com to see how far it was getting through the program by adding various printlns throughout the program and it just loops the whole thing with no regard to the switch case. I will try your suggestion though. :slight_smile:

I think your switch case statements have many problems. Instead of using words like Ready, Armed and Detonate. You would be better off using 1,2, and 3.

Here is an example code found on this site that may help. It takes a analogread then coverts it to a 1,2, 3 depending on the level. In your case you will need a 1,2,or 3 depending on the digitalread of a button using true or false.

 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/SwitchCase
 */

// these constants won't change:
const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 600;    // sensor maximum, discovered through experiment

void setup() {
  // initialize serial communication:
  Serial.begin(9600);  
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // do something different depending on the
  // range value:
  switch (range) {
  case 0:    // your hand is on the sensor
    Serial.println("dark");
    break;
  case 1:    // your hand is close to the sensor
    Serial.println("dim");
    break;
  case 2:    // your hand is a few inches from the sensor
    Serial.println("medium");
    break;
  case 3:    // your hand is nowhere near the sensor
    Serial.println("bright");
    break;
  }

}

But with the #define wouldn't it go through and change all the READYs, ARMEDs, and DETONATEDs to 0s, 1s and 2s when compiling the program?

Evil-Dalek,

You're right. You are better off with #define

The problem of your code is not what cyclegadget said (he's full of crap). When you press the button (can't you think of a more meaningful name?), your case ready is run and that turns the bomb into armed. But I bet you didn't follow through. As your button is still depressed, loop reruns and your bomb is in armed case, and the code in armed case turns the bomb back to ready. So if you keep your button pressed for even a fraction of a millisecond, your bomb state will go between read and armed rapidly since the code runs at very high speed with no delay. Then the bomb lands at either state in random when you let go of your finger.

You know what would really you? Tell us what you intend to do. You present a broken piece of code and ask help me fix it. Who knows what it is intended to do. I can turn it into a coffee maker but you may not like/want it.

cyclegadget, you should really think before you act. If you don't understand #define, read before answering a question. What the OP did with #define has nothing wrong or worse. Also OP is using digital I/O for buttons so your analog code is entirely irrelevant.

So essentially what I'm trying to do is make a C4 prop. It will start out ready and when you press the arming button it becomes armed and waits for a wireless signal to make it explode which pretty much means it will play three short beeps then one long one. If I were to use a button that stays depressed until you press it again and changed the second digital read to wait for the button to become low instead of waiting for high, would that work?

That way you will have to keep your hand pressed to keep the bomb armed. What you need is to be able to tell a transitional state (button down to button up), called button released, not just the current state of a button (up or down).

You can do this:

If button is released, state changes from armed to ready or the other way around.

There are several button libraries that can help you tell transitional states. Here is one I wrote:

Once you install the library, just try the following code:

#include <phi_buttons.h>

phi_buttons arm_ready_button(arm_ready_pin, LOW); // Please don't use digital pin 1 for buttons. Use a different one

void setup(){

Serial.begin(9600);

}

void loop(){

byte temp1=arm_ready_button.sense();

if (temp1==buttons_released)
{
  switch (bombState) {
    
    //The bomb is waiting to be armed
    case READY:
      digitalWrite(LED, HIGH);  //Bomb is armed turn LED on
      bombState = ARMED;
    break;

    //The button has been pressed and it now awaits the wireless signal
    case ARMED:
      digitalWrite(LED, LOW);  //Bomb is no longer armed turn LED off
      bombState = READY;
    if (digitalRead(reciever) == HIGH)
      bombState = DETONATED;
    break;

    //The bomb has recieved the signal and is detonated
    case DETONATED:
    for (i = 0; i <= 2; i++) {
      tone(speakerPin, 5000, 100);  //Beeps 3 times at 5000Hz
      delay(150);
    }
    delay(400);
    tone(speakerPin, 4500, 3000);  //One long beep at 4500Hz
    delay(3000);
    digitalWrite(LED, LOW);  //Bomb is no longer armed turn LED off
    bombState = READY;
    break;
  }

}
}

liudr:
Evil-Dalek,

The problem of your code is not what cyclegadget said (he's full of crap).

Saying I was mistaken, incorrect, or perhaps confused would have been a better choice of words unless you mean to be offensive. Saying I am full of crap is about the same to me as saying I am a liar. I don't think I have earned that.

Evil-Dalek, If I have add difficulty to your project I apologize. I was trying to help and I didn't know being mistaken, incorrect, or perhaps confused, would make me regret trying to help.

Mark

cyclegadget,

I didn't mean to insult you but what you did should be stopped. If you don't know about #define then don't answer. If you do, you will provide the right answer. But you pretended to know better than the OP about #define and provided a wrong answer. I see that same as a lie. I teach as a profession. If I pretend to know an answer to a question and tell a student the wrong stuff, what would the student say about me? I am just preventing wrong answers on the forum. Besides if a student asked about digital buttons and you provided an analog button answer, what would the student respond? If I made a mistake, you can burn me too.

So It is no longer skipping around and is working as it should be, what it is doing right now is getting to void loop going through the sensing, switching to the ready case, going over the if statement and hitting the break and restarting at the beginning of the loop. This is perfect and exactly what it should do but when I press and release the button it is not switching to the armed case. So am I using your library incorrectly because that is the most likely error now. Also the button, pretty much one end is connected to 5v and the other end goes to sensor pin 3 and I tested it by connecting an LED into the circuit after the button to the ground and the LED only turns on when I press the button so the button does work.

P.S. Thank you for the help!!! :smiley: :smiley: :smiley:

#include <phi_buttons.h>

const int speakerPin = 11;  //Pin buzzer is connected to
const int reciever = 2;  //Pin for wireless reciever
const int LED = 13;  //Sets pin for LED that tells if bomb is armed
phi_buttons armingButton(3, LOW);  //Sets the arming button to be read

#define READY 0  //Case for bomb being ready
#define ARMED 1  //Case for bomb being armed 
#define DETONATED 2  //Case for bomb being detonated

int bombState = READY;  //Bomb starts out ready

void setup() {
  Serial.begin(9600);
  pinMode(speakerPin, OUTPUT);  //Sets output for buzzer
  pinMode(LED, OUTPUT);  //LED telling you if the bomb is armed
  pinMode(reciever, INPUT);  //Wireless reciever is an input
}

void loop() {
  byte button_status = armingButton.sense();
  Serial.println("Sensed");
  switch (bombState) {
    
    //The bomb is waiting to be armed
    case READY:
    Serial.println("Case Ready");
    if (button_status == buttons_released) {
      digitalWrite(LED, HIGH);  //Bomb is armed turn LED on
      Serial.println("ARMED");
      bombState = ARMED;
    }
    break;

    //The button has been pressed and it now awaits the wireless signal
    case ARMED:
    if (button_status == buttons_released) {
      digitalWrite(LED, LOW);  //Bomb is no longer armed turn LED off
      Serial.println("READY");
      bombState = READY;
    }
    if (digitalRead(reciever) == HIGH) {
      Serial.println("DETONATED");
      bombState = DETONATED;
    }
    break;

    //The bomb has recieved the signal and is detonated
    case DETONATED:
    for (int i = 0; i <= 2; i++) {
      Serial.println("Beep");
      tone(speakerPin, 5000, 100);  //Beeps 3 times at 5000Hz
      delay(150);
    }
    delay(400);
    Serial.println("Long Beep");
    tone(speakerPin, 4500, 3000);  //One long beep at 4500Hz
    delay(3000);
    digitalWrite(LED, LOW);  //Bomb is no longer armed turn LED off
    bombState = READY;
    break;
  }
}

You need to connect one end of the button to pin 3 and the other end to GND :slight_smile:
although pulling logic to HIGH when depressing the button works fine, you will need a pull-down resistor to pull the logic to LOW when you let go. That is one more part if you want say 6 buttons then 6 more resistors. On the other hand, if pressing the button pulls the logic to LOW, then you will need a pull-up resistor to pull logic to HIGH when you let go the button. This can be done very easily. You enable internal pull-up resistor on the input pin, which is done in the library.

Okay it is done and it works!!! :stuck_out_tongue_closed_eyes: :stuck_out_tongue_closed_eyes: :stuck_out_tongue_closed_eyes: Thank you so much everyone! Now I just need to figure out how to make a wireless detonator... -.- Do you think I could make one out of old RC cars using the controller as the detonator and finding the receiver in the car and connecting it to arduino? Or is there some better way?

Glad it worked! I have no doubt my library works :wink:

Now the wireless part, do you need a true wireless like RC car or can an infrared remote do the job. IR remotes are pretty cheap (borrow your own TV remote) and code is pretty easy to do. For RC car remote, someone else has to help you. I have one RC car but it's too good to take apart for arduino.

I thought about IR because I have an IR sensor but it needs to work in the woods were you wouldn’t be able to get a clear view of it so it has to be something like an RC controller. I have like five old RC cars and their controllers so I could probably find the parts I need in them. And yes your library works great!

So my idea is to get an RC car and take out the circuit and battery holder, from there I will get a relay and have the circuit connected where the DC motor was originally connected so when it gets a signal the relay will open and turn a digitalread on Arduino to high.

This should work. Make sure you probe the connections with your multimeter and confirm what the circuit does before connecting it to arduino. There should be RC cars for around $20 or less in supermarkets.