Arduino and the NewBee

Home Model Engine Machinist Forum

Help Support Home Model Engine Machinist Forum:

This site may earn a commission from merchant affiliate links, including eBay, Amazon, and others.

Foozer

Well-Known Member
HMEM Supporting Member
Joined
Nov 26, 2008
Messages
1,181
Reaction score
83
Location
Camano Island, WA
Can't really appreciate what talents others have untill I try it myself . .

After reading the Diving head and Arduino write up - The OCD kicked in - How hard can Arduino and Steppers be? So after a few weeks of 'What the Heck!' Tentatively have baby steps of 'If-Else' working -

Object:
1) On startup - Have X-Y table return to negative position as determined by limit switch A.
2) Advance table in positive direction at speed X until limit switch B is triggered.
3) Return to position A at speed Y (faster)
4) Advance Y axis specified amount
5) return to and repeat from number 2

Assembled a little test platform - Cigar Boxes make nice little project boxes - Arduino, Easy Driver, Nema 17 motor, cam and some switches - (OCD again - get another driver and stepper to replace the current usage of a LED that simulates the Y axis)

Now for the ADD - Mind is not one that retains names. So after many a time of 'What the heck was that command again?' Little toy does what it is asked of - Now to enter the realm of keypad input so the amount of Y axis travel can be chosen externally . .

Few more weeks and shall see how hardware debounce works
the 74HC or the MC 14490

Stepper%20Project_zpsnkdmhymu.jpg

Stepper%20Project_zpsnkdmhymu.jpg.html
 
Started as a 'What the Heck, Why Not' lets just do this thing - But mounting a little vise upon the table naturally requires something with few moving parts . In making the somethings with few moving parts quickly became tiresome to hand crank the table.

First came the windshield wiper motor gizmo - although it would run in either direction, it clearly favoured one over another - Steppers, leads to power supply, drivers and of course some way to automate - Arduino - Fixed roller switches at center point with adj triggers for the gross X-travel - - Now to fashion with reduction a mount for the other stepper upon the Y-Axis - -
And what was it i wanted to do again? Have already done what i wanted to do but am making something to do what has already been done - Circular?

If nothing else - It is Fun . .

X-Y%20setup_zpshquu2gww.jpg
 
Long way around the fence, but finally got the test model to run
A to B back to A, stop, then run C predetermined amount, stop, then repeat

Now to add a sixth counter to stop Y increments when total depth of cut is reached.

Kudos to those that know this stuff - Myold brian is tired . .


// Working 5-7-16 No Schmidt Trigger setup on test platform - -

#define sw2 2 // Move Table in Positive X Direction SW2 N.O. Type CW
#define sw3 3 // Move Table in Negative X Direction SW3 N.O. Type CCW
#define RSTX 5 // LOW, all STEP commands are ignored
#define enableX 6 // Enable X Axis Motor On/Off
#define stepX 8
#define dirX 9
#define enableY 10 // Enable Y Axis Motor On/Off
#define stepY 11
#define dirY 12


const int rotateSpeedA = 1600; // Slowest
const int rotateSpeedB = 1200; // Slower
const int rotateSpeedC = 800; // Slow
const int rotateSpeedD = 300; // Fast



// Counters Begin With "c"
// Switches Begin With "s"
int ssw2 = 0; // Limit Switch X Neg
int ssw3 = 0; // Limit Switch X Pos
int csw2 = 0; // count var for Switch SW2
int csw3 = 0; // count var for Switch SW3
int ctX = 0; // count var for X-Axis
int ctY = 0; // count var for Y Axis
int crtY = 0; // count var for Y axis compare to crtY
int moveY = 160; // count var for keypad input [future]
int cstepY = 0; // count var for sstepY check
int sstepY = (moveY); //
// changing moveY determines Y axis move - 1600 steps = 0.100 inch


void setup()

{ // Runs Once at Startup
pinMode (sw2, INPUT_PULLUP); // Pin 2
pinMode (sw3, INPUT_PULLUP); // Pin 3
pinMode (enableX, OUTPUT); // Pin 4
pinMode (RSTX, OUTPUT); // Pin 5
pinMode (stepX, OUTPUT); // Pin 8
pinMode (dirX, OUTPUT); // Pin 9
pinMode (enableY, OUTPUT); // Pin 10
pinMode (stepY, OUTPUT); // Pin 11
pinMode (dirY, OUTPUT); // Pin 12

Serial.begin(9600);
delay(1500);
digitalWrite(RSTX, HIGH);

// Begin - Table moves <-----

if(digitalRead(sw2)==HIGH && digitalRead(sw3)==HIGH); // Both Open

{
do
{
digitalWrite(RSTX, HIGH); //
digitalWrite(dirX, HIGH/*CCW*/);
digitalWrite(stepX, LOW);
delayMicroseconds(rotateSpeedD);
digitalWrite(stepX, HIGH);
delayMicroseconds(rotateSpeedD);
}

while (digitalRead(sw2) ==HIGH); // while switch is open
delay(2000);
}
// 0-1 Shift
if(digitalRead(sw2) ==LOW)csw2=1; // when sw2 closes csw2=1
}

void loop()
{
{
// 11110 Run Check
if(csw2==1 && csw3==1 && ctY==1 && crtY==1 && cstepY==0)
{
do // Move Y Axis
{

digitalWrite(dirY, HIGH);// CW ---->
digitalWrite(stepY, LOW);
delayMicroseconds(rotateSpeedB);
digitalWrite(stepY, HIGH);
delayMicroseconds(rotateSpeedB);
cstepY=(cstepY+1); // Increment by 1 each cycle
}
while(cstepY<=(sstepY-1));
}
// for debugging
Serial.print("cstepY = ");
Serial.println(cstepY);
delayMicroseconds(200);
Serial.print("sstepY = ");
Serial.println(sstepY);
delayMicroseconds(200);

// 110 Run Check
if(csw2==1 && csw3==1 && ctY==0)
{
do // Move X Axis back to SW2
{
digitalWrite(dirX, HIGH);// CW ---->
digitalWrite(stepX, LOW);
delayMicroseconds(rotateSpeedD); // Cutting Speed
digitalWrite(stepX, HIGH);
delayMicroseconds(rotateSpeedD);
}
while (digitalRead(sw2) ==HIGH);
}

// 100 Run Check
if(csw2==1 && csw3==0 && ctY==0)
{
do // Move X Axis to SW3
{
digitalWrite(dirX, LOW); // CCW Table Moves <----
digitalWrite(stepX, LOW);
delayMicroseconds(rotateSpeedC);
digitalWrite(stepX, HIGH);
delayMicroseconds(rotateSpeedC);
}
while (digitalRead(sw3) ==HIGH/*Open*/);
}
// for debugging
delayMicroseconds(200);
Serial.print("csw2 = ");
Serial.println(csw2);

// 11100 check
if(csw2==1 && csw3==1 && ctY==1)crtY=1;
// debug
delayMicroseconds(200);
Serial.print("crtY = ");
Serial.println(crtY);

// 11000 check
if(csw2==1 && csw3==1 && ctY==0)ctY=1;
// for debugging
delayMicroseconds(200);
Serial.print("ctY = ");
Serial.println(ctY);

// 10000 check
if(csw2==1 && csw3==0 && ctY==0)csw3=1;
// for debugging
delayMicroseconds(200);
Serial.print("csw3 = ");
Serial.println(csw3);

if(cstepY==(sstepY-1))crtY=0;
if(cstepY==sstepY)ctY=0;
if(cstepY==sstepY)csw3=0;
if(cstepY==sstepY)cstepY=0;
// for debugging
Serial.println("----------------");
Serial.println("Go Mow The Yard");
}
}
// End Program
 
This is Bad - Actually within a few minutes added the 6th counter
Takes the total depth of cut desired e.g. 0.250, cycle increment Y by 0.010 [25 times] until the tdoc is reached, shuts Arduino down [infinite loop] and turns on a Led Done light . .

A couple of weeks of
wallbash.gif
and maybe, just maybe, I might have the basic understanding of 'If' statements understood . .

Now to explore keypad entry to input the tdoc variable from an external source.
 
@63 learning something new - - Well
wallbash.gif
On the test platform can get x to run back and forth, increment Y on each pass until the desired depth of cut is reached, but don't like the idea of having to reload Arduino for any change in depth of cut. Count button presses she says . . . But dear their are only so many pins on the board that can be used - - And that stops you why? she says . .

She leaves and I go Grrr . . Yes Dear . .

An old NES Controller to the rescue . . Has a Start Button, I can work with that, a Select Button - now becomes the button to count for Total Depth of Cut - Hit it say 5 times, Hit the Start button and X will cycle back and forth incrementing Y 5 times.

See you can do it, she says . . . She says, See you have a brain
Should I tell her the hour spent trying to read the NES controller with the power to it turned off? That I'm blessed with an 8bit brain in a 256bit world?
 
Been there. Learning something new is frustrating some times.
I pay my bills being a programmer, let me know if you need any help or have any doubt, i'll be glad to help.

Are you doing this as a learning exercise? On the same Arduino route there are shields and software that could allow you to write just gcode to reach your goals. (look for RAMPS on google, it could be easily configured for a mill or lathe)

Norberto
 
Been there. Learning something new is frustrating some times.

Are you doing this as a learning exercise? On the same Arduino route there are shields and software that could allow you to write just gcode to reach your goals. (look for RAMPS on google, it could be easily configured for a mill or lathe)

Norberto

Haven't done anything digital since 71/72 wherein military MOS was 26L20F5 - Tropospheric Scatter Radio - Roughly what we now carry around in our pockets as a cell phone was something that took a duce and half to carry . .

Learning is a hobby, this go around is the little black box called X-Y table, a little black box called Stepper Motor, a little black box called Arduino all working together to do what I want.

The Box 'X-Y table' and the Box 'Me' didn't work so well as I would forget which way to turn the wheel . The Box 'X-Y table' and Box 'windshield wiper motor' and Box 'Me' didn't work so well as I would forget which way to flip the direction switch. The Box 'X-Y table' Box 'Stepper Motor,' box 'Me' didn't work so well as again, I would forget which way to flip the direction button.'

Seeing as how the Last Good to First Bad pointed to 'Me' as the bad, figured to give Box 'Arduino' a try . . .

So right now it's learn enough about telling Arduino what to do. To cycle Table to travel in direction X --->, back to start <--- and increment Y a set amount e.g. 0.010 until the desired total e.g 0.250 is reached using a NES Gamepad as an overall input device. Eight buttons should at this stage give me enough choices.

Just now got it to wait and count the button presses to set a counter var used to increment Y, then hit button marked Start, it goes on it's merry way [with 'Me' out of the picture] and stops.

Was ticked pink with getting the Start button to work

if(digitalRead(sw4)==LOW || csw==1)digitalWrite(led1,HIGH);
// sw4 normally open switch - csw must be set to 1 for if statements to be true
offon();

void offon()
{ if(digitalRead(sw4)==LOW)csw=1,csw2=1,digitalWrite(led1,LOW),
Serial.println("ON");
}

Live on an island, way to far away from the community college so taking a class - - -

Now to fight my natural Abstract Sequential thinking and get more Concrete Sequential to find the counter bug - after last X travel cycle Y increments one extra.
 
And, not happy with one head banger, also decided to change the cam and lifters [to a 3896962] in the old Monte for a trip to the Crazy Horse Monument this fall - - Figured as long as had some access, might as well reroute the firewall wires out of sight . . .

Wire, Wire, where does she go - -

Note to Self - Why? . . .

Monte_zpstkw9dbpr.jpg
 
Yay! If it's to easy make it harder, if it's to hard make it easier.

NES Button Pad to Arduino to Stepper -
if (NESButtonData==B11110111) onOff(); // Start Button
if (NESButtonData==B10111111) tableLeft(); // <--- Button
if (NESButtonData==B01111111) tableRight(); // ---> Button

And she works, hold down the <--- button, stepper runs - Hold down the ---> button, stepper runs - Like Bottles of Beer on the Wall - Don't focus on the 99, just focus on the 1 . . .

Nes%20Stepper%20Controller_zpsvawvqutz.jpg
 
Its great to see you are getting your head around coding. You have to start somewhere. Now research the switch() statement and see if you can recode the block of if() statements above using it. It should make your code easier to read. Also, think about programming defensively so that you capture an unexpected error and deal with it accordingly. switch() allows you to do this with default:

You can also do this with your if statements by setting NESButtonData to an error code before testing the result in the if() block. If it has not changed after your last if(), you have an error. Often we use 0,1 or -1 to denote error but it depends a bit on the circumstances. It is good however to be consistent through your code.
 
I . . . research the switch() statement and see if you can recode the block of if() statements above using it. It should make your code easier to read.

From the look over, Switch Case routine seems to be the next route.
Rewriting the existing plan, removing all unnecessary. Funny, was just thinking about error checking - That's what started this adventure - The big diagram on the wall "Turn Wheel this Way to Move Table That Way" Could look at it twice and still do it wrong.

Thanks for the Input

Also reading 'The Philosophy of Nietzsche' - Summary of St. Thomas Aquinas made what brain cells I have beg for mercy, now with Arduino and Nietzsche, I'm sure they'll go on strike . .
 
A light went on with the Switch-case suggestion - The function that reads the game pad is always running so variable -byte NesPad;- will always contain the received Data from the NES Control Pad and be stored [the case} So rather than a bunch of
if (NESButtonData==B11110111) // Start
//do something
if (NESButtonData==B01111111) // --->
//do something
if (NESButtonData==B10111111) // <---
//do something

Which got messy real quick

Use

switch (NesPad)
{
case 247: // 'Start Button'
Onoff()
break;
case 127: // ---> Button
RunRight()
break;
case 191: // <--- Button
RunLeft()
default:
// default is optional
break;
}
} // End void loop

Put the functions down below which on the face of it appears to be not only neater but better managed . .

void Onoff()
// do something
void RunRight()
//do something
void RunLeft()
//do something
 
Great progress Foozer. Now at the top of the program or even better in a separate header file (.h) create some defines something like
#define BTN_LEFT 191

So in the switch statement you can say
case BTN_LEFT:
RunLeft();
default:
show_error(); // should never happen but catch it anyway.

If a procedure is longer than one page, it needs to be broken up into other procedures so your code is easy to read.
 
Great progress Foozer. Now at the top of the program or even better in a separate header file (.h) create some defines something like
#define BTN_LEFT 191


case BTN_LEFT:
RunLeft();
default:
show_error(); // should never happen but catch it anyway.

So that's how to convert the abstract [e.g. 191] into the concrete [e.g. btn_Left] ... Was putting around with the under-hood car wiring yesterday (Monday) but did play with one function desire a bit . . Now to add a counter so when the button is hit the table Jogs in predetermined increments - I know there are cheap single axis controllers that will do all this - But that's no Fun

Idea being - Run Left Button checks to see that the Limit Switch is open [not tripped] -- move table as long as button is held - If Limit Switch trips - shut off Stepper [ don't over-run] and turn on Rad Led - - Can only re-enable stepper by hitting the Start Button

void Start() // 247 Start Button
{
digitalWrite(enableX, LOW); // LOW, STEP commands are processed
delay(500); // Wait Half Second
digitalWrite(ledOn, HIGH); // HIGH, ON Turn On Ready Light
}


void runLeft() // 191 <--- Button sw3
{ if(digitalRead(sw3)==LOW)digitalWrite(enableX,HIGH),digitalWrite(ledOn,LOW);

{
digitalWrite(dirX, LOW); // CCW
digitalWrite(stepX, LOW);
delayMicroseconds(800);
digitalWrite(stepX, HIGH);
delayMicroseconds(800);
}
}

*.h file Time to look into library creation - Do see how the usage of can simplify the process - -

Still haven't mowed the Yard - - Bride's beginning to weary of my
"But Look What I Can Do!!" response - -
hide.gif
 
Sometimes ya just can't avoid Downward Causality. Got that car wiring - time to route the blower motor wire - DBL - but are two DBL wires. Don't want to hook up battery just in case the Magic Smoke wants to be free. . so use a small power supply to get a readable signal - Works Fine - But that 'Magic Smoke is still written in my Downward Causality - Note to self - Don't hook the wrong power supply to Arduino - Can't complain - better to smoke arduino than a dash full of wires - -

Played with made a .h file, LEDXY.h to clean up if/when want to use an LED as an indicator - -

#include <LEDXY.h>
LEDXY led;
void setup()
{/*nothing*/}
int x=0;
void loop()
{
if(x<10) // Blink 10 times
{
led.blink(300);
x++;
}
else
{
led.on(); // Led On
}
}
 
So that's how to convert the abstract [e.g. 191] into the concrete [e.g. btn_Left] .

Foozer, great to see you make progress every day. Reading and disecting other code is a great way to learn how to code. Any code written in C will help enormously, not just Arduino code.

I don't want to blow my own trumpet, but some people said that this Auduino sketch I wrote is well written (and controls a rotary table).
http://www.homemodelenginemachinist.com/showthread.php?t=25091

At the very beginning of this thread I link to another thread containing detail. You have to be impressed with what Scott achieved with the code I wrote....

Enjoy.
 
Foozer, great to see you make progress every day.

So you're the guy that got me started on this - If X can do a thing than I can do a thing . . . Today is work on car wiring a bit and dig up a means to Idiot Proof the power supply hookup to my little stepper testing box. Motors are 17H234-055-4A and run fine on 5v - Spaced and flipped the supply switch to 12V - 30 seconds later the little regulator in Arduino let the 'Magic Smoke' out - But better it go up in smoke than the dash wiring in the car - And to fulfill the 'Rule of Three' neighbor lady ask - Can you come over and look? my dryer plug is not working -
1) Fix test model
2) Fix dryer plug
3) Look into passing more than one variable into a (.h) file
4) Mow yard? Naw . . .
 
Don't know what I'm doing with sketches, Library's? not so much either - But it is working - Little short to test the LEDXY.h - and it just sits there happy . . .
(.cpp) file
#include "LEDXY.h" //include the *.h file

LEDXY::LEDXY(){ }

//<<destructor>> Have no Idea what this means
LEDXY::~LEDXY(){/*nothing*/}
// Get input from Sketch to set Led Pin (int ledPin) and blink rate (int rate)
void LEDXY::blink(int ledPin, int rate) // Seems can grab more than one input from Sketch
// Write the Blah Blah here
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(rate);
digitalWrite(ledPin, LOW);
delay(rate);
}
void LEDXY::eek:n (int ledPin)
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
}

#include <LEDXY.h>
LEDXY led;
void setup(){}
void loop()
{
for(int x=0; x<=10;x++) // Blink 10 times
{ led.blink(52,100); } // Takes 2 inputs Pin Number and blink Rate - Red

for(int x=0; x<=10;x++) // Blink 10 times
{ led.blink(53,250); } // Green

{ led.on(53); } // Takes 1 input Pin Number - After 10th Blink Stay on till next blink cycle

}
 
Foozer, bad luck about the Arduino. I have blown up a few and ended up buying 10 off alibaba. I have a spare but on the wrong side of the world for you.

In your LED array you are using a class which is the OOP (Object oriented programming) addon on to C which was named C++.

In a class there is a constructor and a destructor which are called once when the data type is initialised and destroyed. These are where you set up the environment to suit in the constructor and then cleaned up in the destructor. Time enough for that. If you can master classes, you will be a programming wizard...
 

Latest posts

Back
Top