Side Project

I had recently taken up a project and was wondering if I could get some help to program part of it, as I have experience in programming, but not with arduino. The idea is to make a safety lock for machine shop tools.

For this project I'm going to use a drill press, so here's the hardware setup:
-Arduino UNO board, color sensor, and button will all be connected and placed on the drill press (these will most likely be all put into a box to conceal it all)
-A 5v wire will be connected from the arduino (I think) to the relay
-The 120v wire stemming from the drill press will have the AC Plug cut off, and the severed end will be connected to the relay
-Another 120v will be plugged into the wall and the other end is connected to the relay.
(Essentially the relay will be in between the two 120v wires to power the machine tool, and the 5v from the arduino will be what turns the relay on and off, therefore it being powered controls whether or not the 120v will get power between the outlet and drill press)

vvvvv Here's the pseudocode vvvvv

-Waits until a button is pressed
-Color sensor scans for the retro reflective tape on safety glasses)
-If it detects the tape, begin a 5 minute timer (other functions need to be able to run- while this timer is active)
-Sends power to a 5v wire, to make the relay close the circuit, so power will flow from the 120v wire (from the machine) to the 120v wire plugged into the wall.
-After the 5 minutes is over, the relay needs to open, so the device can't be powered again until the button is pressed again and the the sensor scans again.

Currently, we have the color sensor, arduino UNO board, retroreflective tape, safety glasses, machine tool to test with, and the necessary power cords.

I'd appreciate any help for how to write the code, as well as any advice for how to improve the hardware setup. I have looked at many tutorials, but none of them have seemed to help to the extent that I need.

IMG_0768.jpg

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Please READ THIS POST to help you get the best out of the forum.

Bob.

sounds like a school project.

What's the actual context?

Post your attempt at the program. Describe what the program is actually doing and tell us how that differs from what you want the program to do.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

lbrooks9214:
-Color sensor scans for the retro reflective tape on safety glasses)

Will that work sufficiently reliably to be practical? I doubt it.

And if the purpose is to prevent the tool being used without safety glasses being worn I suspect the "clever" user will find a workaround within 3 minutes.

...R

You can use the Mills function, that will allow you to operate your other items while timing. There are a lot of posts on how to use this function. This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil

Hey, that is a neat idea! I can see some problems that would have to be overcome, but none the less, kudos.

Any system is not entirely fool proof.

How about a pressure sensor on the glasses. There would be a NRF24L01 unit in the glasses that constantly sends a signal to the Relay Arduino. When the pressure sensor is activated, the signal sent to the Arduino says 'The operator has his glasses on, he/she can use the drill press'. That will over come the timer problem, and the danger with the user removing glasses once they have gained access to the machine.

Nice project.

Steve

J-M-L:
sounds like a school project.

What if it is? Or isn't?

12Stepper:
What if it is? Or isn't?

This changes the way I contribute and my expectations on OP’s investment in her/his assignment. just too many kids want a solution handed over to them without investing any brain cell.

lbrooks9214:
-A 5v wire will be connected from the arduino (I think) to the relay
-The 120v wire stemming from the drill press will have the AC Plug cut off, and the severed end will be connected to the relay
-Another 120v will be plugged into the wall and the other end is connected to the relay.
(Essentially the relay will be in between the two 120v wires to power the machine tool, and the 5v from the arduino will be what turns the relay on and off, therefore it being powered controls whether or not the 120v will get power between the outlet and drill press)

I would be very very very inclined to get one of of these or similar.

edit: After checking the ratings, of course.

Okay I apologize for any confusion or aggravation my question or wording may have caused. Allow me to elaborate on this. Yes, this project would be for school, for a product based expo I will be participating in at my school sometime in 2020. I have taken both engineering and computer science classes, which is where my experience is, however, the curriculum does not align with one another, so I do not have much experience on writing code that will be uploaded into a machine (Writing digital code to manipulate a physical object, in this case the shop tool). However, as some have questioned, my intention is not to ask for handouts. I would just like to know any methods you think might be helpful to make this idea work. I have dabbled the past week in the millis method for time delays, however I do not fully understand how to implement it. Additionally, I am not completely sure how to have the color sensor read retro reflective tape, as I'm not sure what color it would identify it as. These are just a few concerns I have so far. I plan to really dig into this assignment, as for the past few months I've been through different materials. I originally had planned on using RFID but due to pricing we switched to an IR sensor. However, it didn't work out because when we connected the IR and arduino UNO board, the IR heated up a lot in a matter of minutes. So now, I will be using the color sensor. Additionally, I would eventually like to implement extra fail-safes like a pressure and heat sensor as well in the future. However, keeping in mind pricing and with my level of expertise I would like to know which one would be the easiest to code and conceal?

Here's the code I have so far:

/*
Need to use timer
Needs to implement button
Needs to incorporate color sensor
Needs to use pressure sensor
*/
int buttonPress = 0;
void setup() {
pinMode(10, INPUT) //button press
pinMode(4, OUTPUT) //timer
pinMode(6, INPUT) //relay
}
void loop() {
buttonpressed = digitalRead(10);
if (buttonPress == HIGH) { //button pressed
//if Colorscan == yes
digitalWrite(4, LOW) //start timer
digitalWrite(6, LOW) //close relay
// open relay after timer finishes
digitalWrite(4, HIGH); //stop timer
digitalWrite(6, HIGH); //open relay
}
}

As I said before, any advice or insight would be a big help!

Read about state machine, there are many tutorials laying around

You should also read the Using millis() for timing. A beginners guide tutorial that is anchored at the top of the forum. Everything will come clear :slight_smile:

——

As a side note, read the forum rules and Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

lbrooks9214:
I have dabbled the past week in the millis method for time delays, however I do not fully understand how to implement it.

Well you need to dabble more, especially since you said originally:

begin a 5 minute timer (other functions need to be able to run- while this timer is active)

What is is that you don't understand? Time spent getting your head round that now (especially since "sometime in 2020" gives you at least a month :wink: ) will be well spent and save you loads of heartache and anguish in the future.

To get...

lbrooks9214:
the code I have so far:

.... working properly, you first (my opinion) need to look at understanding state change in buttons. That is, you look for the button to become pressed: you look for it to change from unpressed "now" to pressed "a split second later". This state change detect tutorial should help. Most folk here prefer though to wire the button to ground and use:

pinMode(pinNum, INPUT_PULLUP);

... which means you need to reverse the logic in the code. The button becomes pressed when it goes from high to low.

If I were you, I'd spend a bit of time on that. Wire the button to ground, invert the logic in the code, and get it to serial print say "new press" and "new release" reliably. (Ignore the part where it increments a counter.)

Then we can look at how to use that new press to start the timer, where we would basically capture the time at the moment of pressing (that is, millis()) into a variable. (Then check the ever-increasing-millis() against that captured time to see if time's up.)

I'm not sure what you expect this:

 pinMode(4, OUTPUT) //timer
...
digitalWrite(4, LOW) //start timer
...
digitalWrite(4, HIGH); //stop timer

... to do, by the way.

Motto here has to be, one thing at a time.

Then separately and in parallel, work on this:

lbrooks9214:
Additionally, I am not completely sure how to have the color sensor read retro reflective tape, as I'm not sure what color it would identify it as.

Whenever I encounter something new I work on in outside of my main sketch, and fiddle with it to my mind's content. Only then, do I consider moving it into the main code.

I can't help with that sensor, never even heard of it... Someone here may have experience with it. But don't cloud things by trying to get the timing and the sensor working together right away. Sort them separately, then bolt the bits together.

edit:

And what J-M-L said:

J-M-L:
Read about state machine, there are many tutorials laying around

You could start by reading what Grumpy_Mike and Nick Gammon have to say on state machines, not necessarily in that order.

Revised opinion, and sorry if this confuses you: although it's important in the long run to understand state change detect on a button, in this case it's unnecessary. I envisage 2x states, say idle and running, and if it sees that the "run" button is pressed in idle it can go to running (provided it also passes the saftey check) and in running it doesn't look at the run button. In idle it's enough to see the button is pressed; doesn't have to know it just became pressed.

Also, although I don't think you mentioned it, I would check the safety glasses all the time. Otherwise (and assuming it's a feasible thing to check in the first place) as long as the operator has them on at the start, so the machine will fire up, there's no reason s/he can't take them off once it's running.

Here's my pseudo code and state diagram:

  • Check the safety glasses each time though loop(), set a flag safetyGlassesOn
  • Start in state st_idle. Relay off.. If runButton is pressed, and safetyGlassesOn is true, capture millis(), and go to state....
  • ... st_running. Turn relay on. Monitor safetyGlassesOn: if it ever goes false, change state back to st_idle which will turn relay off. Monitor increasing millis() vs captured start time: when it's expired, go back to st_idle.

I am not completely sure how to have the color sensor read retro reflective tape, as I'm not sure what color it would identify it as.

Colour sensors don’t identify colours. They normally return three numbers for the amount of red, green and blue in a colour.

we switched to an IR sensor. However, it didn't work out because when we connected the IR and arduino UNO board, the IR heated up a lot in a matter of minutes

That means you connected it up wrong.
RFID readers are cheap in the order of less than $5.00.

OP I just wrote a sketch for the pseudo-code and state diagram from #13. Won't post it unless you want to see it. I just used a switch as a proxy for the safety test, reading it in a function, so easy to replace with "real" code for the "real" sensor.

I have updated some of the code.

Here are some of the questions as I go further:
-Can the button connect to the Arduino? If not, where would it connect to? The power tool? Or is it another source it could plug into?
-Also, I would like to implement a LCD display later down the line to display the time remaining before the device will power off. Which methods would be most effective for doing this?

I found some code online and have added code for the color sensor to run code when it detects a silver color. Now I am working on how to make the timed loop work. The idea is for it to start with the period at 0 and check the time at every time it loops, so when the 5 minutes is up the code stops, but I was confused as to how to make the loop circle back and repeat until the condition (end of the 5mins) is fulfilled.

Here's the code:

/*
*/
int Button_Press = 0;
int frequency = 0;
int color=0;
unsigned long currentMillis = millis();
unsigned long previousMillis = 0;  //will store last time LED was blinked
const long period = 300000;         // period at which to blink in ms
void setup() {
  pinMode(10, INPUT) //button press
  pinMode(4, OUTPUT) //timer
  pinMode(6, INPUT) //relay
  color = readColor();
}
void loop() {
  Button_Press = digitalRead(10);
  if (Button_Press == LOW) { //button pressed
    if(Read_Color==1){
      unsigned long previousMillis = 0;
      unsigned long currentMillis = millis(); // store the current time
      if (currentMillis - previousMillis >= period) { // check if 5 mins passed
        previousMillis = currentMillis;   // save the last time you blinked the LED
        digitalWrite(6, LOW) //close relay
      }
      else {
        //loop the func
      }
    // open relay after timer finishes
    digitalWrite(4, HIGH); //stop timer
    digitalWrite(6, HIGH); //close relay
  }
  else{
    //sound alarm
  }
}
int Read_Color() {
  // Setting red filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  int R = frequency;
  // Printing the value on the serial monitor
  Serial.print("R= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.print("  ");
  delay(50);
  // Setting Green filtered photodiodes to be read
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  int G = frequency;
  // Printing the value on the serial monitor
  Serial.print("G= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.print("  ");
  delay(50);
  // Setting Blue filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  int B = frequency;
  // Printing the value on the serial monitor
  Serial.print("B= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.println("  ");
  delay(50); 
   if(R<170 & R>211 & G<170 & G>211 & B<170 & B>211){ //170,169,173 192,192,192 211,211,211
    color = 1; // RRT
   }
   else{
    color = 0;
   }
    return color;
}

but I was confused as to how to make the loop circle back and repeat until the condition

Basically you don't. The loop function will repeat automatically forever. If you do stop it then that is it, your code will stop all functions until you reset or power cycle.

To repeat a section of code until something happens use the "while" structure. That will repeat until the test in the brackets after the "while" fails, that is returns false. Note inside the code contained in the braces { } of the while, you should have code that will potentially change what the "while" is testing for.