Go Down

Topic: Could use some help with my 8 switch project...please (Read 1 time)previous topic - next topic

rynd96

Bit of a noob but I've had good luck, until now. I'm working on a setup where you have 8 switches and once they are in the proper position, one led goes off, one led comes on and a relay is triggered. Thought I was on the right track but I'm not having any luck. My guess is that it at the point where the switch conditions are. Any help would be greatly appreciated

/*
Pin 2 is switch 1
Pin 3 is switch 2
Pin 4 is switch 3
Pin 5 is switch 4
Pin 6 is switch 5
Pin 7 is switch 6
Pin 8 is switch 7
Pin 9 is switch 8
Pin 10 is red Led
Pin 11 is grn Led
Pin 12 is relay Com
Pin 13 is n/a

Pin 2,3,4,5,6,7,8,9 Input
*/

const bool led = 13;

// set pin numbers:
const int interruptPin0 = 2;     // the number of the pushbutton pin 0
const int interruptPin1 = 3;     // the number of the pushbutton pin 1
const int interruptPin2 = 4;     // the number of the pushbutton pin 2
const int interruptPin3 = 5;     // the number of the pushbutton pin 3
const int interruptPin4 = 6;     // the number of the pushbutton pin 5
const int interruptPin5 = 7;     // the number of the pushbutton pin 6
const int interruptPin6 = 8;     // the number of the pushbutton pin 7
const int interruptPin7 = 9;     // the number of the pushbutton pin 8
const int redLedPin =  10;       // the number of the RED LED pin
const int grnLedPin = 11;        // the number of the GRN LED pin
const int relayPin = 12;         // the number of the relay pin

// variables will change:
int buttonState0 = HIGH;         // variable for reading the pushbutton0 status
int buttonState1 = HIGH;         // variable for reading the pushbutton1 status
int buttonState2 = HIGH;         // variable for reading the pushbutton2 status
int buttonState3 = HIGH;         // variable for reading the pushbutton3 status
int buttonState4 = HIGH;         // variable for reading the pushbutton5 status
int buttonState5 = HIGH;         // variable for reading the pushbutton6 status
int buttonState6 = HIGH;         // variable for reading the pushbutton7 status
int buttonState7 = HIGH;         // variable for reading the pushbutton8 status
int puzzle = LOW;                // variable for state puzzle

// the setup funtion runs once when you press reset or power the board
void setup() {

//initialize digital pin 10, 11, 12 as outputs
pinMode(redLedPin, OUTPUT);
pinMode(grnLedPin, OUTPUT);
pinMode(relayPin, OUTPUT);

// initialize digital pin 2,3,4,5,6,7,8,9 as inputs with internal pullup resistors

pinMode(interruptPin0, INPUT_PULLUP);
pinMode(interruptPin1, INPUT_PULLUP);
pinMode(interruptPin2, INPUT_PULLUP);
pinMode(interruptPin3, INPUT_PULLUP);
pinMode(interruptPin4, INPUT_PULLUP);
pinMode(interruptPin5, INPUT_PULLUP);
pinMode(interruptPin6, INPUT_PULLUP);
pinMode(interruptPin7, INPUT_PULLUP);

// set the outputs
digitalWrite(redLedPin, HIGH);  // turn the red LED on
digitalWrite(grnLedPin, LOW);  // turn the green LED off
digitalWrite(relayPin, LOW);  // relay off

}

// the loop funtion runs over and over again forever
void loop() {
// read the state of the puzzle
puzzle = (digitalRead(buttonState0 == HIGH) && (buttonState1 == LOW) && (buttonState2 == LOW) && (buttonState3 == LOW) && (buttonState4 == LOW) && (buttonState5 == LOW) && (buttonState6 == LOW) && (buttonState7 == LOW));

// check if the puzzle is solved
// if it is, the puzzle state is low or zero volts
if (puzzle == LOW) {
// delay to avoid randomly flicking switches
delay (1500);
if (puzzle == LOW) {
// turn red led off, grn led on and trigger relay
digitalWrite(redLedPin, LOW);  // turn the red LED off
digitalWrite(grnLedPin, HIGH);  // turn the green LED on
digitalWrite(relayPin, HIGH);  // relay on

// wait time for reset
delay (10000);
}
}
else { // else reset the puzzle
// turn redLed on and relay off
digitalWrite(redLedPin, HIGH);  // turn the red LED on
digitalWrite(grnLedPin, LOW);  // turn the green LED off
digitalWrite(relayPin, LOW);  // relay off

}
}

ieee488

#1
Dec 04, 2017, 02:04 am
I am not sure I understand you want the circuit and this program to do.

rynd96

#2
Dec 04, 2017, 03:11 am
It's a box with 8 eight toggle switches, put all eight switches in the correct position and a relay triggers a solenoid which allows you to open a box with a gift inside. It's for my son for Christmas, he loves puzzles so I thought I'd be able to pull it off but I've hit a road block

larryd

#3
Dec 04, 2017, 03:35 amLast Edit: Dec 04, 2017, 03:48 am by larryd
You need to do digitalRead on all 8 input pins.

Quote
puzzle = (digitalRead(buttonState0 == HIGH) && (buttonState1 == LOW) && (buttonState2 == LOW) && (buttonState3 == LOW) && (buttonState4 == LOW) && (buttonState5 == LOW) && (buttonState6 == LOW) && (buttonState7 == LOW));
You need:

No technical PMs.
The last thing you did is where you should start looking.

rynd96

#4
Dec 04, 2017, 05:07 pm
Thanks for the help. I got the switch combination working properly so that's awesome.

I am having an issue with the "delay" though.

What I was hoping for was once the switches are in the right order, the program reads that, delays for 1.5 seconds, reads it again and triggers the "win" state. This would circumvent my son just randomly flipping switches to get the box open.

However once all the switches are in the proper position, it's taking anywhere from 2 to 12 seconds for the program to go into the "win" state. I've timed it multiple times and it's all over the place.

Any idea what could be causing this?

larryd

#5
Dec 04, 2017, 05:26 pmLast Edit: Dec 04, 2017, 05:26 pm by larryd
When you make changes to the code, we need to see them.

Always show us your 'current' compete sketch.
Use CTRL T to format the sketch.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

Also:
Show us a good schematic of your circuit.
Show us a good image of your wiring.

No technical PMs.
The last thing you did is where you should start looking.

rynd96

#6
Dec 04, 2017, 05:44 pm
Here's the current working code, save for the delay issue. Neither one of the delays seem to be working correctly. I have one delay near the beginning of the "if" statement to avoid solving the puzzle with random switch flicking prior to the win state and one later for the reset.

Haven't had time to do the diagram just yet as I wanted to see if it would work first but I will post it later today

Code: [Select]
`/*  Pin 2 is switch 1  Pin 3 is switch 2  Pin 4 is switch 3  Pin 5 is switch 4  Pin 6 is switch 5  Pin 7 is switch 6  Pin 8 is switch 7  Pin 9 is switch 8  Pin 10 is red Led  Pin 11 is grn Led  Pin 12 is relay Com  Pin 13 is n/a  Pin 2,3,4,5,6,7,8,9 Input*/const bool led = 13;// set pin numbers:const int interruptPin0 = 2;     // the number of the pushbutton pin 0const int interruptPin1 = 3;     // the number of the pushbutton pin 1const int interruptPin2 = 4;     // the number of the pushbutton pin 2const int interruptPin3 = 5;     // the number of the pushbutton pin 3const int interruptPin4 = 6;     // the number of the pushbutton pin 5const int interruptPin5 = 7;     // the number of the pushbutton pin 6const int interruptPin6 = 8;     // the number of the pushbutton pin 7const int interruptPin7 = 9;     // the number of the pushbutton pin 8const int redLedPin =  10;       // the number of the RED LED pinconst int grnLedPin = 11;        // the number of the GRN LED pinconst int relayPin = 12;         // the number of the relay pin// variables will change:int buttonState0 = HIGH;         // variable for reading the pushbutton0 statusint buttonState1 = HIGH;         // variable for reading the pushbutton1 statusint buttonState2 = HIGH;         // variable for reading the pushbutton2 statusint buttonState3 = HIGH;         // variable for reading the pushbutton3 statusint buttonState4 = HIGH;         // variable for reading the pushbutton5 statusint buttonState5 = HIGH;         // variable for reading the pushbutton6 statusint buttonState6 = HIGH;         // variable for reading the pushbutton7 statusint buttonState7 = HIGH;         // variable for reading the pushbutton8 statusint puzzle = LOW;                // variable for state puzzle// the setup funtion runs once when you press reset or power the boardvoid setup() {  //initialize digital pin 10, 11, 12 as outputs  pinMode(redLedPin, OUTPUT);  pinMode(grnLedPin, OUTPUT);  pinMode(relayPin, OUTPUT);  // initialize digital pin 2,3,4,5,6,7,8,9 as inputs with internal pullup resistors  pinMode(interruptPin0, INPUT_PULLUP);  pinMode(interruptPin1, INPUT_PULLUP);  pinMode(interruptPin2, INPUT_PULLUP);  pinMode(interruptPin3, INPUT_PULLUP);  pinMode(interruptPin4, INPUT_PULLUP);  pinMode(interruptPin5, INPUT_PULLUP);  pinMode(interruptPin6, INPUT_PULLUP);  pinMode(interruptPin7, INPUT_PULLUP);  // set the outputs  digitalWrite(redLedPin, HIGH);  // turn the red LED on  digitalWrite(grnLedPin, LOW);  // turn the green LED off  digitalWrite(relayPin, HIGH);  // relay on for NO}// the loop funtion runs over and over again forevervoid loop() {  // read the state of the puzzle  puzzle = (digitalRead(interruptPin0) == HIGH  && digitalRead(interruptPin1) == LOW && digitalRead(interruptPin2) == HIGH && digitalRead(interruptPin3) == LOW  && digitalRead(interruptPin4) == HIGH && digitalRead(interruptPin5) == LOW && digitalRead(interruptPin6) == LOW  && digitalRead(interruptPin7) == LOW);  // check if the puzzle is solved  // if it is, the puzzle state is low or zero volts  if (puzzle == LOW) {    // delay to avoid randomly flicking switches    delay (1500);    if (puzzle == LOW) {      // turn red led off, grn led on and trigger relay      digitalWrite(redLedPin, LOW);  // turn the red LED off      digitalWrite(grnLedPin, HIGH);  // turn the green LED on      digitalWrite(relayPin, HIGH);  // turns relay off for NC      // wait time for reset      delay (10000);    }  }  else { // else reset the puzzle    // turn redLed on and relay on    digitalWrite(redLedPin, HIGH);  // turn the red LED on    digitalWrite(grnLedPin, LOW);  // turn the green LED off    digitalWrite(relayPin, LOW);  // relay on  }}`

PaulRB

#7
Dec 04, 2017, 06:03 pm
I can't explain your random delay issue, but I spotted this:
Code: [Select]
`  if (puzzle == LOW) {    // delay to avoid randomly flicking switches    delay (1500);    if (puzzle == LOW) {`
If "puzzle" has the value LOW before the delay(1500), it will still be low after the delay. I think you need to re-evaluate "puzzle" after the delay to see if all switches are still in the correct position.

larryd

#8
Dec 04, 2017, 06:10 pm
It's not fair to have to guess 0-255 to open a gift

No technical PMs.
The last thing you did is where you should start looking.

rynd96

#9
Dec 04, 2017, 07:12 pm
I can't explain your random delay issue, but I spotted this:
Code: [Select]
`  if (puzzle == LOW) {    // delay to avoid randomly flicking switches    delay (1500);    if (puzzle == LOW) {`
If "puzzle" has the value LOW before the delay(1500), it will still be low after the delay. I think you need to re-evaluate "puzzle" after the delay to see if all switches are still in the correct position.
My thought was, the delay of 1.5 seconds should prevent anyone from randomly guessing the correct switch position by just flicking switches on and off. Then it reads the state again and if still LOW, triggers the "win" state. Am I off base?

rynd96

#10
Dec 04, 2017, 07:21 pm
It's not fair to have to guess 0-255 to open a gift

Forgive my noob-ness but I don't know what your referring to Larry?

PaulRB

#11
Dec 04, 2017, 07:48 pmLast Edit: Dec 04, 2017, 07:50 pm by PaulRB
Am I off base?
Yes. I understand your idea, but that's not what your code is doing. Just look at the lines I highlighted. They get executed one immediately after the other. Every time. Nothing happens in-between those lines.

larryd

#12
Dec 04, 2017, 09:56 pmLast Edit: Dec 04, 2017, 09:58 pm by larryd
Forgive my noob-ness but I don't know what your referring to Larry?
Quote
It's a box with 8 eight toggle switches, put all eight switches in the correct position and a relay triggers a solenoid which allows you to open a box with a gift inside. It's for my son . . .
With 8 switches he has a possibility of 256 different combinations to try before he gets his reward.

.
No technical PMs.
The last thing you did is where you should start looking.

rynd96

#13
Dec 04, 2017, 11:27 pm
With 8 switches he has a possibility of 256 different combinations to try before he gets his reward.

.
Lol, I was going to make up a puzzle for that but...

Go Up