[SOLVED - OUTSOURCED] Defuse the Bomb! Coding and layout help needed

Tom, Dwight, Chris, & Jobi,

Thanks,

Tom, you ask WHY? The answer is easy, I'm stupid and don't really know what I'm doing. I know what I want it to do , but making it do what I want, is easier said, than done.

When I get home from my real job, I'm going to change the OUTPUTS to INPUTS as appropriate. It's those nuances that I am failing to grasp. You all have explained it well. I should have it this time. I am actually reading the help references, etc. They do give insight to the complexity of the programming language. It's helping to clear up the whole HIGH LOW problem I was struggling with earlier.

I tried the INPUT_PULLUP deal, but it did not seem to help. But apparently I had it wrong as well. I even found some info on that in the Help section, but I failed to make it work. Even though someone earlier told me NOT to use the PULLUP pinMode.

Once again, thanks for your help and understanding. If I continue to have such issues, I may have to resort to hiring someone to finish this out, as we need to get this working sooner rather than later. And my learning curve is moving pretty slow.

Mark

Even though someone earlier told me NOT to use the PULLUP pinMode.

It's hard to imagine anyone being that obtuse.

If you are using the schematic that Tom posted, there is no reason to use INPUT_PULLUP because
there are already pullup resistors drawn.
I have seen no clear reason why one would not use INPUT_PULLUP under the right conditions.
It was just said not to use it without a clear reason.
In any case, there is no reason if you have the thing wired as shown.
If things are not working, please post the updated code.
Dwight

OMG!!!!! I finally got it!!!!!!!

After much pulling of hair and gnashing of teeth, I finally got it working the way I wanted it to. I tested it over and over and finally figured out what was holding me up.

Apparently you can't put more than one Boolean Operator in a statement. For my third wire I was telling it:

if (digitalRead(thirdWire) == HIGH && digitalRead(secondWire) == LOW && digitalRead(firstWire) == LOW)  {

It was holding up the firstWire working properly even though I was not telling the first wire to do anything. I even tried telling the first to do nothing , but that did not help. I was able to figure it out by turning off every thing after, where it stopped working, and turning on one element at a time until it broke. Which was just before the last segment.
I then got the idea to break up the two && operators into their own segment and
VOILA!! it worked. I even added in the final action to make sure it all triggered.

    //reminder to self: HIGH is wire Cut, LOW is wire Intact
    //To set off effects when second wire is cut before the first wire is cut
if (digitalRead(secondWire) == HIGH && digitalRead(firstWire) == LOW) {
   digitalWrite(smoke, HIGH);
  digitalWrite(light, HIGH);
  digitalWrite(sound, HIGH);
}
   //To set off effects when third wire is cut before the first or second wire is cut
if (digitalRead(thirdWire) == HIGH && digitalRead(secondWire) == LOW)  {
  digitalWrite(smoke, HIGH);
  digitalWrite(light, HIGH);
  digitalWrite(sound, HIGH);
}

if (digitalRead(thirdWire) == HIGH && digitalRead(firstWire) == LOW){
  digitalWrite(smoke, HIGH);
  digitalWrite(light, HIGH);
  digitalWrite(sound, HIGH);
}
if (digitalRead(thirdWire) == HIGH && digitalRead(secondWire) == HIGH && digitalRead(firstWire) == HIGH) {
  digitalWrite(drawer, HIGH);
}

Thanks for all your help everyone, and Mostly your patience with this Old Newbie. Kind of nice being a Newbie at my age...

Now I just have to figure out the time clock, the Light activator, and the sound. I've got the drawer working, just need to add it into this code. Oh, and the button to start the whole thing. And a reed switch to start the smoke filling the chamber.

If I have questions about any of those other elements, should I start a new Topic or just continue this since it's all related?

Thanks Again!

Mark

I believe you can have more than one Boolean operator. Your
problem was you were not controlling the order of execution
with ()s to do things in the right order.
Your statement:

(digitalRead(thirdWire) == HIGH && digitalRead(secondWire) == LOW && digitalRead(firstWire) == LOW)

probably would have worked if written:

((digitalRead(thirdWire) == HIGH) && ((digitalRead(secondWire) == LOW) && (digitalRead(firstWire) == LOW)))

One does have to be careful about putting functions in the
Boolean equation. Say one of the center values was the return
of a function. Say also that that function set some global variable,
in the middle of its calculation.
Depending on how it was compiled, that function might not
execute and the variable never get set to the new value.
This is because the compiler does not use AND and OR code operations
to evaluate the statement, it instead does conditional branching.
This means that if there were sequential &&'s it might find the first
value it executes is a 0. It will branch out without looking at the rest.
Of course, to add to this, the order you enter the code is not
always the required to be the same order it is executed.
Don't ask me how I found all this out.
Dwight

Dwight,

Don't worry, I'm not going to ask you how you found all that out, Heck , I don't know what you even said. But seriously, I had several versions of that statement, with even more brackets. And I used the || (OR) operator as well.

I literally was about to give up as I could not understand WHY it was not working. I knew it had to do with little idiosyncrasies in writing the code, just as you say. I don't know how much of this programming is based on algebra, but it sure looks like it. And as I said earlier, I did not do well in algebra. English either, for that matter.

Again, thanks!

Hey guys, I’m Back with more. I hope you are still willing to help. Thanks to all, I have the original request up and working correctly. Here is my working code:

/*Wire Cutting sequence   
 Cut the right wires in the right order and you win. Cut the wrong wire and or in the wrong order,
  and you lose, and the special effect activates.
*/

const int  wrongWire = 13; // the number of the cut wire pin
const int firstWire = 12;  // the number of the first wire pin
const int secondWire = 11;  // the number of the second wire pin
const int thirdWire = 10;   // the number of the third wire pin
const int smoke = 7;   // the number of the smoke pin
const int light = 6;   // the number of the light pin
const int sound = 5;   // the number of the sound pin
const int drawer = 4;   // the number of the drawer pin
 //int firstWireState = digitalRead(firstWire);
 //int secondWireState = digitalRead(secondWire);
 //int thirdWireState = digitalRead(thirdWire);
 
void setup()
{
  pinMode(wrongWire, INPUT);
  pinMode(firstWire, INPUT);
  pinMode(secondWire, INPUT);
  pinMode(thirdWire, INPUT);
  pinMode(smoke, OUTPUT);
  pinMode(light, OUTPUT);
  pinMode(sound, OUTPUT);
  pinMode(drawer, OUTPUT);
 }


void loop(){
 //represents the wrong wire being cut at any time.
  if (digitalRead(wrongWire) == HIGH) {
    digitalWrite(smoke, HIGH);
    digitalWrite(sound, HIGH);
    digitalWrite(light, HIGH);
    delay(500);
    digitalWrite(light, LOW);
    digitalWrite(sound, LOW);
    delay(900000);  //15 minute delay
    //digitalWrite(smoke, digitalRead(wrongWire));
    //digitalWrite(light, digitalRead(wrongWire));
    //digitalWrite(sound, digitalRead(wrongWire));
}   

    //reminder to self: HIGH is wire Cut, LOW is wire Intact
    //To set off effects when second wire is cut before the first wire is cut
if (digitalRead(secondWire) == HIGH && digitalRead(firstWire) == LOW) {
   digitalWrite(smoke, HIGH);
   digitalWrite(sound, HIGH);
    digitalWrite(light, HIGH);
     delay(500);
    digitalWrite(light, LOW);
    digitalWrite(sound, LOW);
    delay(900000);  //15 minute delay to keep it from just repeating, and enough time to reset.
}
  //To set off effects when third wire is cut before the first or second wire is cut
if (digitalRead(thirdWire) == HIGH && digitalRead(secondWire) == LOW)  {
  digitalWrite(smoke, HIGH);
  digitalWrite(sound, HIGH);
  digitalWrite(light, HIGH);
    delay(500);
    digitalWrite(light, LOW);
    digitalWrite(sound, LOW);
    delay(900000);  //15 minute delay
}

if (digitalRead(thirdWire) == HIGH && digitalRead(firstWire) == LOW){
  digitalWrite(smoke, HIGH);
    digitalWrite(sound, HIGH);
    digitalWrite(light, HIGH);
    delay(500);
    digitalWrite(light, LOW);
    digitalWrite(sound, LOW);
    delay(900000);  //15 minute delay
}
if (digitalRead(thirdWire) == HIGH && digitalRead(secondWire) == HIGH && digitalRead(firstWire) == HIGH) {
  delay(1000);
  digitalWrite(drawer, HIGH);
  delay(1000);
  digitalWrite(drawer, LOW);
}

}

All hardware works, and I’m ready to it put on soldered boards. But I have one more element that needs to be incorporated. A Countdown Timer.

I started a new topic to get some help with the Adafruit 4-Digit 7-Segment Display w/I2C Backpack that I was having trouble getting to work, and Chris was kind enough to lend a hand. Here is the code he provided;

/***********************************************
* Count down using millis()
*
* Adafruit 7-segment, I2C, LED backpack display
*
* pin A5 -> C (on 7-segment)
* pin A4 -> D (on 7-segment)
*
************************************************/

#include <Wire.h>
#include "Adafruit_GFX.h"
#include "Adafruit_LEDBackpack.h"
Adafruit_7segment matrix = Adafruit_7segment();

unsigned long previousSecondMillis = 0UL;
long oneSecond = 1000UL; // milliseconds per second

#define startMinute 4  //  Modify these defines to
#define startSecond 59 // change the timer interval
int minutes = startMinute;
int seconds = startSecond;

void setup()
{
  matrix.begin(0x70);
}

void loop() {

  // --------- Run this every second ------------------
  if (millis() - previousSecondMillis >= oneSecond) {

    matrix.writeDigitNum(0, (minutes / 10));
    matrix.writeDigitNum(1, (minutes % 10));
    matrix.writeDigitNum(3, (seconds / 10));
    matrix.writeDigitNum(4, (seconds % 10));
    matrix.writeDisplay();

    if (seconds-- == 0) {
      if (minutes == 0) {
        minutes = startMinute;
        seconds = startSecond;
        delay(1000);
      } else {
        minutes -= 1;
        seconds = 59;
      }
    }

    previousSecondMillis += oneSecond;
  }
}

OK, I’m stuck. I was hopeful, but in, way over my head. I got the countdown timer working, that Chris wrote above. Ideally, I want to incorporate this code into the original "Wire Cutting sequence " code to run it all on the one Arduino UNO . Pins 9,8,3,2,1,0,A0,A1,A2,A3 are all available. (I have a Mega available if needed and/or another UNO, if these need to be broken up.)

Now what I need to accomplish is;

  1. To have it START with a momentary push button.

  2. Have it STOP when signal from (Drawer HIGH signal) is received.

  3. Once the STOP signal is received, have it PAUSE for 10 or 15 seconds and RESET the countdown time to 20 or 30 seconds and continue counting down.

  4. When it reaches the end either by NOT receiving the STOP signal in time or after the PAUSE & RESET, to send a signal to activate the special effects. (digitalWrite(smoke, light , sound, HIGH);

I’ve looked at, and tried to use, bits and pieces of other countdown timer codes to make it work, but they are all written very differently and I can’t find the magic formula to make it all work.

Any help that you are willing to share with this newbie, would be highly appreciated.

Thanks, Mark

Continued from the OPs other thread …

ridenhr:
Chris,

I tried to put in a Boolean variable after reading up on it but, I’m not have any success. I guess part of my problem is I don’t understand what line of your code makes the countdown timer start in the first place. I know once you hit reset it just goes. If i understood what command makes it start, then maybe I could tell it to start when the button is pushed.

This code decrements the count:

if (seconds-- == 0) {
  if (minutes == 0) {
    minutes = startMinute;
    seconds = startSecond;
      delay(1234); // this is the end of the count - all it does is pause 1.234 seconds.
    } else {
      minutes--;
      seconds = 59;
    }
  }

It starts because it is the only option in my loop() function. Use something like

if (isCountingDown) { decrement the count code }

As discussed, you can control the execution of the ‘decrement the count code’ with the state of the variable, isCountingDown. Try it, and please show your work. :wink:

dwightthinker:
I believe you can have more than one Boolean operator. Your
problem was you were not controlling the order of execution
with ()s to do things in the right order.
Your statement:

(digitalRead(thirdWire) == HIGH && digitalRead(secondWire) == LOW && digitalRead(firstWire) == LOW)

probably would have worked if written:[............]

There is no need to include extra braces. This statement is fine, as is. Sometimes the order of operations can become confusing but this is one thing the designers got right. Comparison goes before boolean logic. If not for that, most C code would look like an awful mess (well, more than it already does...).

It follows propositional logic in speech, such as:
If I am happy and you are happy or Joe is unhappy, then ....
if (I==happy && you==happy || Joe!=happy) ...
which makes it easier to understand.

As discussed, you can control the execution of the ‘decrement the count code’ with the state of the variable, isCountingDown. Try it, and please show your work. :wink:

Here is what I wrote, pressing the button does nothing. The timer starts as usual as soon as it is reset.

#include <Adafruit_LEDBackpack.h>

/***********************************************
* Count down using millis()
*
* Adafruit 7-segment, I2C, LED backpack display
*
* pin A5 -> C (on 7-segment)
* pin A4 -> D (on 7-segment)
*
************************************************/

#include <Wire.h>
#include "Adafruit_GFX.h"
#include "Adafruit_LEDBackpack.h"
Adafruit_7segment matrix = Adafruit_7segment();

unsigned long previousSecondMillis = 0UL;
long oneSecond = 1000UL; // milliseconds per second


//#define BUZZ    3  //pin 3  connect to Buzzer
#define startMinute 0  //  Modify these defines to
#define startSecond 30 // change the timer interval
int minutes = startMinute;
int seconds = startSecond;
boolean isCountingDown = false;
int button  = 9;  //pin 9 connect to Button

void setup()
{  
  matrix.begin(0x70);
  pinMode(button, INPUT);
  digitalWrite(button, HIGH);
}

void loop() 
{
    //Wait for button press to start Countdown
  if(digitalRead(button) == LOW){
   delay(100);  //delay to debounce switch

   isCountingDown = !isCountingDown;    //toggle running variable
  
 }

  // --------- Run this every second ------------------
  if (millis() - previousSecondMillis >= oneSecond) {

    matrix.writeDigitNum(0, (minutes / 10));
    matrix.writeDigitNum(1, (minutes % 10));
    matrix.writeDigitNum(3, (seconds / 10));
    matrix.writeDigitNum(4, (seconds % 10));
    matrix.writeDisplay();

    if (seconds-- == 0) {
       if (minutes == 0) {
        minutes = startMinute;
        seconds = startSecond;
        //digitalWrite (BUZZ, HIGH) ;// Buzzer On
        //delay(10000);
        while (1);
      } else {
        minutes -= 1;
        seconds = 59;
      }
    }

    previousSecondMillis += oneSecond;
  }
}

Your code toggles the value of the variable, but then never tests the variable to either run the decrement code or not. That's what the line I mentioned is for:

if (isCountingDown) { decrement the count code }

Where would you put this line?

ChrisTenone:
Your code toggles the value of the variable, but then never tests the variable to either run the decrement code or not. That's what the line I mentioned is for:

if (isCountingDown) { decrement the count code }

Where would you put this line?

Well Chris, that would be the question. I guess you would put it before;

if (seconds-- == 0) {
if (minutes == 0) {
minutes = startMinute;
seconds = startSecond;

But then the compiler says that "isCountingDown" is not defined.

and do you put it in EXACTLY as you wrote it?

Can I just PAY someone to write the code the way I need it? I am not making any progress LEARNING this stuff and I still have several commands to add in there.

Mark

ridenhr:
Well Chris, that would be the question. I guess you would put it before;

if (seconds-- == 0) {
if (minutes == 0) {
minutes = startMinute;
seconds = startSecond;

But then the compiler says that "isCountingDown" is not defined.

and do you put it in EXACTLY as you wrote it?

Yes, that is where to put the line (but be sure and put ALL the 'decrement-the-count-code' between curly braces, and yes, I wrote it exactly. Getting "not defined" errors when you know damn well that you defined something, often means that you have an extra ending brace somewhere in your code.

Recall that I identified the following code as being the code that decrements the count:

ChrisTenone:
This code decrements the count:

if (seconds-- == 0) {

if (minutes == 0) {
    minutes = startMinute;
    seconds = startSecond;
      delay(1234); // this is the end of the count - all it does is pause 1.234 seconds.
    } else {
      minutes--;
      seconds = 59;
    }
  }

So when I said:

Use something like

if (isCountingDown) { decrement the count code }

I meant substitute "the code that decrements the count" into the line. I have done that here with your code, and made sure all the braces match up:

    if (isCountingDown) {

      if (seconds-- == 0) {
        if (minutes == 0) {
          minutes = startMinute;
          seconds = startSecond;
          //digitalWrite (BUZZ, HIGH) ;// Buzzer On
          //delay(10000);
          while (1);
        } else {
          minutes -= 1;
          seconds = 59;
        }
      }
    }

(everything else is unchanged. You might be getting fooled by your debounce delay. Sometimes it'll work, sometimes it won't.)

Can I just PAY someone to write the code the way I need it? I am not making any progress LEARNING this stuff and I still have several commands to add in there.

Mark

Of course you can. Write a request for proposal, specify your design, then post on the "Gigs & Collaborations" sub-forum.

Chris,

Glad you spelled it out for me

if (isCountingDown) {

      if (seconds-- == 0) {
        if (minutes == 0) {
          minutes = startMinute;
          seconds = startSecond;
          //digitalWrite (BUZZ, HIGH) ;// Buzzer On
          //delay(10000);
          while (1);
        } else {
          minutes -= 1;
          seconds = 59;
        }
      }
    }

I was not getting how to insert the statement you suggested.

Before I post this to the "gigs & collaboration" sub-forum, do you want to write it for me? & if so, how much? DM me if you are interested. I would want to give you first right of refusal since you have helped me so much.

ridenhr:
Chris,

Glad you spelled it out for me

if (isCountingDown) {

if (seconds-- == 0) {
       if (minutes == 0) {
         minutes = startMinute;
         seconds = startSecond;
         //digitalWrite (BUZZ, HIGH) ;// Buzzer On
         //delay(10000);
         while (1);
       } else {
         minutes -= 1;
         seconds = 59;
       }
     }
   }




I was not getting how to insert the statement you suggested.

Before I post this to the "gigs & collaboration" sub-forum, do you want to write it for me? & if so, how much? DM me if you are interested. I would want to give you first right of refusal since you have helped me so much.

Ha ha! ... No. I've got my own projects keeping me busy. I hopped on this thread because I had written very similar code recently using exactly that hardware.
Just reading through this thread though, it appears you are more than half done.

So close, but yet so far......

I'm running out of time and patience.

Thanks, Chris, for all your help!

Quick update! I've found a solution to my project!!!

As much as I appreciate everyone trying to help me write the code on my own, I had to resort to more drastic measures. I should have done this a long time ago. I've been trying to write the code myself for over a month now and I've gotten far but not quite there yet.

Any way, I went onto the Upwork site and posted my project and within hours, I hired someone to finish out the code for me. Less than 18 hours later, (and only because there was a 7 hour time difference) I have a fully working sketch.

I highly recommend the Upwork site. It's easy to find someone willing to do the work and at a price I could live with.

Thanks for trying, I'll be posting any future projects, that I can't figure out, on Upwork from now own.

I don't know how to list this topic as solved, but it is.

Thanks again to everyone who tried to help me!!!

Mark

I highly recommend the Upwork site.

Bye

I've found a solution to my project

I've never seen "paid for" so badly misspelled.