Arduino Code Help

Jay,

The variable stateMotor keeps track of whether the motor is On or Off. It doesn't directly control the relay. That's done with the digitalWrite(RELAY2, x) statement.

To make things a bit more clear, I'll rename stateMotor to be motorOn. motorOn is a boolean variable with the values true or false.

// state variable declaration
bool motorOn = false;

When the program comes out of reset, motorOn is false. At the start of each pass through the loop the program looks at the motorOn variable. If motorOn is false (motor is Off), it looks at the Lower Sensor. If it needs to turn on, the program sets motorOn to true and turns the relay On with digitalWrite(RELAY2, HIGH). If the motor doesn't need to turn on, the program skips over the "else" statement to the bottom then jumps back to the top of the loop. To get into the "else" statement, motorOn must be true (motor is On) where it looks at the Upper Sensor. If the motor needs to turn Off, motorOn is set false and the relay is turned off with digitalWrite(RELAY2, LOW). Then the program jumps back o the start of the loop to repeat the process.

Since the comment system doesn't allow tabs, in the code below, I used 4 spaces for each tab indentation

#define RELAY2 10 //Feeder relay is on pin 10
#define SENSORLOW 2 //Feeder Low IR Sensor is on pin 2
#define SENSORHIGH 3 //Feeder High IR Sensor is on pin 3

// state variable
bool motorOn = false;

void setup() {
pinMode(RELAY2, OUTPUT); // initialize the RELAY2 pin as an output:
pinMode(SENSORLOW, INPUT_PULLUP); // sensor Low with pullup enabled
pinMode(SENSORHIGH, INPUT_PULLUP); // sensor High with pullup enabled

// this statement was missing on the original programs
digitalWrite(RELAY2, LOW); // make sure the relay is Off after reset
}

void loop() {
if(motorOn == false) {
if(digitalRead(SENSORLOW) == HIGH) { // turn On if Low sensor shows empty
digitalWrite(RELAY2, HIGH);
motorOn = true;
}
}
else {
if(digitalRead(SENSORHIGH == LOW) { // turn off if High sensor shows full
digitalWrite(RELAY2, LOW);
motorOn = false;
}
}
}

I've been using Arduino Nanos. They are powerful and cheap (about $4 each). So far I've built a stepper motor controller to replace the gear train on my G4000 lathe for feeding. One of these days I'll finish the version that will be able to do to threading in metric and US. I also just finished a DRO for the cross slide. It uses a $10 Harbor Freight caliper. It calculates diameters directly simplifying my work. It's working great on my current project which involves tight tolerances for ball bearings.

Glen
 
Glen, thank you for the detailed explanation. That really helps and is much appreciated. Hopefully I will be able to give it a whirl tonight.

Your lathe projects sounds great. Do you have build threads on those?

I have often though about setting up a system on the cross slide to use a stepper motor and micro controller for constant surface speed turning. I fall short when it comes to the software.
 
FYI to anyone following this thread, the code above is missing a ")" at "if(digitalRead(SENSORHIGH == LOW) { // turn off if High sensor shows full"

It should be "if(digitalRead(SENSORHIGH) == LOW) { // turn off if High sensor shows full"
 
Good catch. That's typical of software when you don't compile it before showing it to the world.

The commenting system removes extra spaces too. It didn't show up as I was typing it.

Here's the build thread for one of the projects.
Stepper replaces G4000 gear train for feeding

I'll put another together for the DRO, but it's not all that creative. It's been done many times.

Glen
 
It works!

I setup the sensors on a breadboard and it worked as planned so I decided to try adding a second feeder to the code. I did that and wired another sensor system on the breadboard and nothing?? The IR sensors were acting like they were not being read. I have the sensors powered by a separate bench supply so I checked that. I could see the emitters glowing in my phone camera so it didn't make any sense.

Thinking maybe I some how damaged the sensors I pulled it all apart and setup the single sensor code that has a LED and serial read so I could verify the Arduino was reading the sensor on the serial monitor. I also added a voltage regulator to be sure I would get a clean full 5v. Everything was laid loose and by pointing together or apart all was all working. I was getting messages on the serial monitor and the LED goes on off. Great so I checked all the IR sensors to be sure they all worked.

Thinking maybe I previously had a bad connection somewhere else I remounted the IR sensor to the breadboard and tried to break the beam. Nothing??? It was at that point I realized that at first I had been using a small steel ruler to break the beam but had changed to a postit note folded several times. Sure enough the IR goes right through the paper. Who knew? Works fine with the steel ruler. UGH! an hour wasted.

So I put it all together and tried the dual feeder code. Success!!! After that I added an alarm for another process. So far so good. Now to package it.

Here is my final code. Not sure if it is the proper way to format the dual feeder and alarm but it works.

/*
dual_feeder_w_alarm
*/

#define ASENSOR 7 //alarm IR sensor is on pin 7
#define ALED 8 //alarm LED is on pin 8
#define ABUZZ 9 //alarm buzzer relay is on pin 10
#define RELAY1 10 //feeder1 relay is on pin 10
#define RELAY2 11 //feeder2 relay is on pin 11
#define SENSOR1LOW 2 //feeder1 Low IR Sensor is on pin 2
#define SENSOR1HIGH 3 //feeder1 High IR Sensor is on pin 3
#define SENSOR2LOW 4 //feeder2 Low IR Sensor is on pin 4
#define SENSOR2HIGH 5 //feeder2 High IR Sensor is on pin 5

// state variable
int asensorState = 0;
bool motor1On = false;
bool motor2On = false;

void setup() {
pinMode(ALED, OUTPUT); // initialize the ALED pin as an output:
pinMode(ABUZZ, OUTPUT); // initialize the ABUZZ pin as an output:
pinMode(ASENSOR, INPUT); // initialize the ASENSOR pin as an input:
pinMode(RELAY1, OUTPUT); // initialize the RELAY1 pin as an output:
pinMode(RELAY2, OUTPUT); // initialize the RELAY2 pin as an output:
pinMode(SENSOR1LOW, INPUT_PULLUP); // sensor1 Low with pullup enabled
pinMode(SENSOR1HIGH, INPUT_PULLUP); // sensor1 High with pullup enabled
pinMode(SENSOR2LOW, INPUT_PULLUP); // sensor2 Low with pullup enabled
pinMode(SENSOR2HIGH, INPUT_PULLUP); // sensor2 High with pullup enabled

digitalWrite(ASENSOR, HIGH); //turn on the pullup
digitalWrite(RELAY1, LOW); // make sure the relay is Off after reset
digitalWrite(RELAY2, LOW); // make sure the relay is Off after reset
}

void loop() {
if(motor1On == false) {
if(digitalRead(SENSOR1LOW) == HIGH) { // turn On if Low sensor shows empty
digitalWrite(RELAY1, HIGH);
motor1On = true;
}
}
else {
if(digitalRead(SENSOR1HIGH) == LOW) { // turn off if High sensor shows full
digitalWrite(RELAY1, LOW);
motor1On = false;
}
}

{
if(motor2On == false) {
if(digitalRead(SENSOR2LOW) == HIGH) { // turn On if Low sensor shows empty
digitalWrite(RELAY2, HIGH);
motor2On = true;
}
}
else {
if(digitalRead(SENSOR2HIGH) == LOW) { // turn off if High sensor shows full
digitalWrite(RELAY2, LOW);
motor2On = false;
}
}

{
asensorState = digitalRead(ASENSOR); // read the state of the ASENSOR value:

if (asensorState == LOW) { // check if the sensor beam is broken, if it is, the sensorState is LOW:
digitalWrite(ALED, LOW); // turn LED on:
digitalWrite(ABUZZ, LOW); // turn BUZZER on:
}
else {
digitalWrite(ALED, HIGH); // turn LED off:
digitalWrite(ABUZZ, HIGH); // turn BUZZER off:
}
}
}
}

My prototyping mess :grin:

20180816_205504.png
 
Jay,

Your prototype looks pretty good to me. That's not a mess, it's progress.

Glen
 
I've been using Arduino Nanos. They are powerful and cheap (about $4 each). So far I've built a stepper motor controller to replace the gear train on my G4000 lathe for feeding. One of these days I'll finish the version that will be able to do to threading in metric and US. I also just finished a DRO for the cross slide. It uses a $10 Harbor Freight caliper. It calculates diameters directly simplifying my work. It's working great on my current project which involves tight tolerances for ball bearings.

Glen
How would you incorporate the lathe C axis for threading?

I bought an Arduino Mega 2560 awhile ago, plugged it into a desktop once to see what it would do and it was not what I was looking for.
You can have it for the USPS cost from NJ. Of course this may be old tech by now. It does no good sitting on my desk so its next destination is the scrap.
 
Mr. Waller,

Thanks for the offer, but I've already got an Arduino Mega 2560. It's got the same CPU as the Nano, but it's got a lot more Input/Output, Flash and RAM.

Don't scrap the Mega. Just plugging it into your computer doesn't demonstrate much. Once you've made up your mind that you want to "electronify" some of your machinery you will find that it's just the thing you need. There are a couple of things you need to make the Mega useful: an LCD display (I'd recommend a 16 character by 2 rows or 20 character by 4 rows with the I2C 2 wire interface (about $5 to $12 each) and a 4x4 keypad (about $2 each). With these two extras you can build just about anything. Software isn't for everyone, but I find it fascinating.

Threading is pretty straightforward. I have to change my current 24 slot tachometer wheel to 64 slots plus a Top Dead Center indicator. The stepper will synchronize to TDC and adjust it's speed on each tick of the tachometer sensor. The stepper system already has an electronic stop and in knows where "Home Position" is (just before the start of the thread) so it can return there after each pass. The speed of the stepper is a simple ratio calculation based on the spindle speed and the desired pitch. I will be able to produce right and left threads in any pitch either US or metric or any oddball thread in between.

This threading mechanism will be still a manual process. I just won't have to mess around with the gear train.

Glen
 
Mr. Waller,

Thanks for the offer, but I've already got an Arduino Mega 2560. It's got the same CPU as the Nano, but it's got a lot more Input/Output, Flash and RAM.

Don't scrap the Mega. Just plugging it into your computer doesn't demonstrate much. Once you've made up your mind that you want to "electronify" some of your machinery you will find that it's just the thing you need. There are a couple of things you need to make the Mega useful: an LCD display (I'd recommend a 16 character by 2 rows or 20 character by 4 rows with the I2C 2 wire interface (about $5 to $12 each) and a 4x4 keypad (about $2 each). With these two extras you can build just about anything. Software isn't for everyone, but I find it fascinating.

Threading is pretty straightforward. I have to change my current 24 slot tachometer wheel to 64 slots plus a Top Dead Center indicator. The stepper will synchronize to TDC and adjust it's speed on each tick of the tachometer sensor. The stepper system already has an electronic stop and in knows where "Home Position" is (just before the start of the thread) so it can return there after each pass. The speed of the stepper is a simple ratio calculation based on the spindle speed and the desired pitch. I will be able to produce right and left threads in any pitch either US or metric or any oddball thread in between.

This threading mechanism will be still a manual process. I just won't have to mess around with the gear train.

Glen
I understand how it works, I program and run CNC lathes for a living.
The application that I looked at the Arduino controller for is unrelated to machine tool control, ended up using a Mitsubishi PLC which works flawlessly and the programming is easily changed, it has no other HMI then START, STOP and several OH SH!* stops (-:

Also cost was not an issue.
 
I have everything almost working perfectly. The last problem is that when the system fills up to the lower break beam sensor, the next part that passes the upper sensor it is enough to trigger the sensor and stop the motor so the buffer zone never fills up to the upper sensor. I suspect if two parts fell at the right interval it could create a timing problem with the lower sensor as well. I need a way to have the sensors not trigger until the the beams have been broken for a time longer than for a part to pass by. I was looking at the milis function as a delay for the sensors but am having trouble seeing how to integrate it. I'm not even sure that is the best way to go.

Here is my current code.

/*
Feeder
*/

#define ASENSOR 7 //alarm IR sensor is on pin 7
#define ALED 8 //alarm LED is on pin 8
#define ABUZZ 9 //alarm buzzer relay is on pin 10
#define RELAY1 10 //feeder1 relay is on pin 10
#define RELAY2 11 //feeder2 relay is on pin 11
#define SENSOR1LOW 2 //feeder1 Low IR Sensor is on pin 2
#define SENSOR1HIGH 3 //feeder1 High IR Sensor is on pin 3
#define SENSOR2LOW 4 //feeder2 Low IR Sensor is on pin 4
#define SENSOR2HIGH 5 //feeder2 High IR Sensor is on pin 5

// state variable
int asensorState = 0;
bool motor1On = false;
bool motor2On = false;

void setup() {
pinMode(ALED, OUTPUT); // initialize the ALED pin as an output:
pinMode(ABUZZ, OUTPUT); // initialize the ABUZZ pin as an output:
pinMode(ASENSOR, INPUT); // initialize the ASENSOR pin as an input:
pinMode(RELAY1, OUTPUT); // initialize the RELAY1 pin as an output:
pinMode(RELAY2, OUTPUT); // initialize the RELAY2 pin as an output:
pinMode(SENSOR1LOW, INPUT_PULLUP); // sensor1 Low with pullup enabled
pinMode(SENSOR1HIGH, INPUT_PULLUP); // sensor1 High with pullup enabled
pinMode(SENSOR2LOW, INPUT_PULLUP); // sensor2 Low with pullup enabled
pinMode(SENSOR2HIGH, INPUT_PULLUP); // sensor2 High with pullup enabled

digitalWrite(ASENSOR, HIGH); //turn on the pullup
digitalWrite(RELAY1, HIGH); // make sure the relay is Off after reset
digitalWrite(RELAY2, HIGH); // make sure the relay is Off after reset
}

void loop() {
if(motor1On == false) {
if(digitalRead(SENSOR1LOW) == HIGH) { // turn On if Low sensor shows empty
digitalWrite(RELAY1, LOW);
motor1On = true;
}
}
else {
if(digitalRead(SENSOR1HIGH) == LOW) { // turn off if High sensor shows full
digitalWrite(RELAY1, HIGH);
motor1On = false;
}
}

{
if(motor2On == false) {
if(digitalRead(SENSOR2LOW) == HIGH) { // turn On if Low sensor shows empty
digitalWrite(RELAY2, LOW);
motor2On = true;
}
}
else {
if(digitalRead(SENSOR2HIGH) == LOW) { // turn off if High sensor shows full
digitalWrite(RELAY2, HIGH);
motor2On = false;
}
}

{
asensorState = digitalRead(ASENSOR); // read the state of the ASENSOR value:

if (asensorState == LOW) { // check if the sensor beam is broken, if it is, the sensorState is LOW:
digitalWrite(ALED, LOW); // turn LED on:
digitalWrite(ABUZZ, LOW); // turn BUZZER on:
}
else {
digitalWrite(ALED, HIGH); // turn LED off:
digitalWrite(ABUZZ, HIGH); // turn BUZZER off:
}
}
}
}
 
Back
Top