Hi so we're trying to do a project but cant get the coding right and don't know where its going wrong so any help would be appreciated, the project brief is as follows:
Project: Safety control switch for a saw. (Saw replicated by motor)
Brief:
Once a button is pressed to turn on the saw it will give a countdown from 5secs to 0(zero) when the motor is activated. The motor however will not start after the counter reaches unless the operator pushes two buttons that demonstrate his/her hands are clear. If not done during the five second window the system should reset. During operation if either safety switch is released the motor should immediately stop and the system should reset.
So basically there will be 3 switches (push buttons) One to start and two that have to be kept pressed simultaneously.
You have code that is giving you trouble? If you post the code we can help get it working. Please read the "how to use this forum-please read" stickies to see how to format and post code properly.
TommyHitman:
So basically there will be 3 switches (push buttons) One to start and two that have to be kept pressed simultaneously.
I don't see the point of the first switch. Why not start the saw when the other two are pressed and held pressed for (say) 5 seconds. If either switch is released within the 5 seconds the whole timing sequence would need to restart.
I presume you have a BIG RED SWITCH to stop the saw in an emergency and are not relying on the Arduino for that?
Like in #3 is indeed how it's done in the real world.
Two switches, mounted so far apart that they can not be pressed with one hand (0.5-1 meter typically) and a big emergency off button that kills the power (for a saw: preferably also applies a very strong brake - e.g. spring loaded electromagnet that's released when power goes down). I've also worked with systems that had a kick wire at the bottom as emergency off. Motor will start as soon as both switches are pressed. No extra start switch, no time out. Normally only a short press is enough to start the machine, not continuous press for continued operation. That's not needed.
On reading this thread through, I can't help but think that the OP was asking about a homework assignment, and everyone else is treating it like a real-world scenario.
@TommyHitman, are you working on an assignment with fixed requirements?
If so, then we need to focus on your spec and help you with the areas you are having trouble.
If not, then all the good advice given so far applies.
In a real world situation, I wouldn't even attempt something like this. Way too much riding on a beginner programmer's abilities. If the system were to fail and someone is injured or worse, you are responsible. If you can live with that, by all means, go for it.
There has to be commercial products that do exactly what you want. That's what I would be looking for. Or, at the very least, paying a pro to design the system and write the sketch.
Even for a homework assignment, making it resemble the real world scenario as closely as possible is more than just a good idea.
Of course the saw is replaced by a simple motor, so it's hard for things to go catastrophically wrong. That's how I would start when designing a real safety system - by replacing the dangerous part with something that's not - and then do all the buttons the proper way.
Doing it the "wrong" way in the classroom (adding the startup delay which is irritating & may make the user think it's not working, no emergency off) teaches the wrong thing. It's much harder to unlearn something than it is to learn it correctly the first time around.
In relation to some of the replies it is indeed a homework assignment iv posted the code that we have so far below if anyone knows how to write it correctly or what mistakes were making itd be great if ye can help.
int button1Pin = 2;
int button2Pin = 4;
int currentButton1=LOW;
int currentButton2=LOW;
int motorPin = 13;
int i = 0 ;
boolean buttonPressed = false;
Buttons are customarily wired between the pin and GND, then using the internal pull-up resistor the input is pulled HIGH, so when a button is pushed, it's reading LOW. You'll use pinMode(pin, INPUT_PULLUP) in that case.
you may look for both buttons at the same time. Leaving it as active high.
if (digitalRead(button1Pin) == HIGH && digitalRead(button2Pin) == HIGH) {
buttonPressed = true;
}
else {
buttonPressed = false;
}
Don't forget to reset this bool. Your code only sets it to true, it's never reset. So the moment the buttons are pressed momentarily, it's set forever.
For the timing: use millis().
The moment both buttons are pressed, record the time (use an unsigned long variable for this). Then continue checking whether the buttons are pressed and whether the delay time has passed. The delay you check as follows:
if (conditionMet) {
startTime = millis();
}
if (millis() - startTime > interval {
do stuff - such as switching on your motor.
}
Of course you have to make sure startTime is only set the first time the buttons are detected to be pressed.