Starting New project but dont know where to start in terms of coding

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.

Can anyone help please????? ??? ??? ???

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.

the IDE has some sample programs.

one is blink without delay - that will do your timing.

if the start is pressed,
then the timer starts
if button A and B are both pressed
run the saw

this is a very simple program and I would hope you can modify the sketchs to make it work.

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?

...R

It's a common problem when don't know where to start. I would recommend a visual programming approach, use XOD as an option.

Here is how your program looks in XOD:

You just need to define Arduino ports for Start button, Left safety, Right safety buttons and for Saw switch/relay.

The program is attached

saw-relay.zip (921 Bytes)

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.

wvmarle:
No extra start switch, no time out. Normally only a short press is enough to start the machine,

I agree. I suspect a delayed start would just be a distraction leading the user to think there was a fault.

...R

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.

Just my opinion.

In a real world situation

For sure, but the OP's post is almost certainly a classroom assignment.

The pros have to start somewhere.

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;

void setup() {
pinMode(motorPin, OUTPUT);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
Serial.begin(9600);
}

void loop() {
Serial.print("seconds: ");
currentButton1=digitalRead(button1Pin);
currentButton2=digitalRead(button2Pin);

if(currentButton1==HIGH){
buttonPressed = true;
}

if(buttonPressed && i < 500 && currentButton2==HIGH){
digitalWrite(motorPin, HIGH);
Serial.println(currentButton2);
}else if (i>=5){
i = 0;
}
i = i++ ;
delay(1000);
Serial.println(i);

}

TommyHitman:
how to write it correctly or what mistakes were making itd be great if ye can help.

Have you tried and does it do what you want? Testing should be your first step.

If it does not do what you want then tell us what it actually does and what you want it to do that is different.

...R

A few comments on your loop() function.

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.

  1. 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.

  1. 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.