Pages: [1]   Go Down
Author Topic: Telling a function to use a motor  (Read 316 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have written a function to control the direction and speed of a stepper motor (fwd_rev) and would like to be able to tell this function what motor to use, as I'm going to add a second and third.
I do not want to write a new function for each motor and don't think i have to but I can't figure out have to do it.
Here's my code:

Code:
/*
 * XY_CNC.cpp
 *
 */


#include <arduino.h>
#include <Stepper/Stepper.h>


/********************** Define Global Variables Here *************************/



#define btn1 6 // Button 1 Pin
#define btn2 7 // Button 2 Pin
#define adj1 3 // Adjustment 1 input Pin

#define en 8 // Enable pin for SN754410 IC's

#define clk_pin 10 // Pin for 595
#define latch_pin 11 // Pin for 595
#define data_pin 12 // Pin for 595

Stepper s1(96, 2,3); // Y Stepper
Stepper s2(48, 4,5); // X Stepper
word s1_RPM;
word s2_RPM;

word btn1v = 0; // Value for Button 1
word btn2v = 0; // Value for Button 2
/********************** Custom Functions Here ***********************/



void fwd_rev(int b1, int b2, int RPM, int numstep){
/* This function controls the forward and reverse of the stepper motor.
Allows you to define the forward and reverse buttons to use.
As well as the minimum # of steps per function call the motor will operate at,

"b1" and "b2" are the values of the buttons controlling the motor.
"RPM" is self explanatory.
"numstep " is the minimum number of steps the motor should make as required by the stepper library.  */

s1.setSpeed(RPM);

if (b1 == 0 && b2 == 1){
digitalWrite(en, HIGH);
s1.step(numstep);
}

if (b1 == 1 && b2 == 0){
digitalWrite(en, HIGH);
s1.step(-numstep);
}

if(b1 == 1 && b2 == 1){
digitalWrite(en, LOW);
}
}




int adj_dial(int pin, int min, int max){
/* This function will read from an adjustment pin and return the value read.
the value is mapped between min and max values */

int rval; // Return Value

int adj_val = analogRead(pin);

rval = map(adj_val, 0, 1023, min, max);


return rval;
}




void display(int disp){
/* This function will take a 3 digit INT and display the number on the 7 segment display */

byte num[10] = {254,48,109,121,51,91,95,112,127,115};  // Display bits 0-9

word ones = disp % 10;
word tens = (disp - ones) % 100;
word hund = (disp - tens - ones) % 1000;

byte dc1 = ones;
byte dc2 = tens / 10;
byte dc3 = hund / 100;

digitalWrite(latch_pin, LOW);

shiftOut(data_pin, clk_pin, LSBFIRST, num[dc1]);
shiftOut(data_pin, clk_pin, LSBFIRST, num[dc2]);
shiftOut(data_pin, clk_pin, LSBFIRST, num[dc3]);

digitalWrite(latch_pin, HIGH);
}
 
 
/****************************** Setup *********************************/
 
void setup() {

pinMode(btn1, INPUT_PULLUP);
pinMode(btn2, INPUT_PULLUP);
pinMode(en, OUTPUT);

pinMode(clk_pin, OUTPUT);
pinMode(latch_pin, OUTPUT);
pinMode(data_pin, OUTPUT);

// Serial.begin(9600);


}
 
 
/******************************** Main Loop ********************************/
 
void loop() {


btn1v = digitalRead(btn1);
btn2v = digitalRead(btn2);

s1_RPM = adj_dial(adj1, 50, 300);

display(s1_RPM);

fwd_rev(btn1v, btn2v, s1_RPM, 1);  // --> I want to do this:  fwd_rev(btn1, btn2, s1, RPM, 1);



}
« Last Edit: March 05, 2013, 06:47:23 pm by IAM_Carbon_Based » Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1178
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Learn to use pointers, then you can pass a pointer to the motor object to the function. Lots of stiuff out there on this, including tutorials, but the basics are:

The address of an object or variable is given by &varname, so calling a function with
Code:
function_name(&varname);
passes a pointer to the variable.

The function declaration needs to be
Code:
function_name(type *varname)
that makes varname a pointer to a variable of type

Then to reference the methods of the object you use ->
Code:
varname->method();

Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pointers are so far the most confusing thing I've encountered so far. BUT I've got it working, Thank you.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1178
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Think of it this way.

Your house is somewhere in a street that is part of a city. To find your house you give out the address. This is effectively a pointer to your house.
A variable is at a memory address that is part of bigger memory space. To find the variable you can use the memory address of that variable. That is all that a pointer is.

You are effectively manipulating the memory address of the variable rather than the variable itself. That is a very powerful thing to do, as it allows all sorts of cool techniques to happen when programming.

Anyway, glad to see hear it works.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Johannesburg UTC+2
Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Pointers are so far the most confusing thing I've encountered so far

I was at a client many years back, where a new-ish employee had been doing quite well getting her mind round programming. She was, rightly, quite pleased with her progress.

So then one of the older hands said to one of the other guys, "Ok, shall we tell her about pointers now?" with an evil grin; unsaid was ".... she's getting a bit big for her boots."
Logged

Have you looked for answers in the Learning, Reference, Products and Playground tabs on http://arduino.cc/en/ ?
Look here http://electronicsclub.info/ for basic electronics

Pages: [1]   Go Up
Jump to: