Pages: [1]   Go Down
Author Topic: Custom library not working :(  (Read 317 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've written a library that allows my to define a stepper I wish to control. However at the moment, everything compiles and I don't get an error, but it does not work.
The enable pin for the driver does not go high, I can't see what is wrong as I'm new to writing libraries:

This code can also be found at: https://github.com/IAM-Carbon-Based/XY-CNC under the Cust_Func Branch.

Main.cpp
Code:
/*
 * XY_CNC.cpp
 *
 * Created: 3/2/2013 10:12:18 PM
 *
 * Description: Main Source
 */


#include <arduino.h>
#include <Stepper/Stepper.h>
#include <Cust_Func/Cust_Funcs.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;

boolean btn1v = 0; // Value for Button 1
boolean btn2v = 0; // Value for Button 2

ctrl Y_AXIS(&s1, en); // Create ctrl instance for Y_AXIS stepper.


/********************** Custom Functions Here ***********************/


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() {


s1_RPM = adj_dial(adj1, 50, 300);

display(s1_RPM);

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

if (btn1v == 0 && btn2v == 1){
Y_AXIS.run(s1_RPM, 50, 1);
}

if (btn1v == 1 && btn2v == 0){
Y_AXIS.run(s1_RPM, 50, 0);
}

}

Library.h
Code:
/*
 * IncFile1.h
 *
 * Created: 3/9/2013 11:41:10 AM
 *  Author:
 */


#ifndef INCFILE1_H_
#define INCFILE1_H_

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


class ctrl
{
public:
ctrl(Stepper *STPR, int en);
void run(int RPM,  int numstep, byte dir);

private:
Stepper *STPR;
int numstep;
int en;
int RPM;
byte dir;

};



#endif /* INCFILE1_H_ */

Library.cpp
Code:
/*
 * CPPFile1.cpp
 *
 * Created: 3/9/2013 11:41:15 AM
 *  Author:
 */

#include <Arduino.h>
#include <Cust_Func/Cust_Funcs.h>
#include <Stepper/Stepper.h>

ctrl::ctrl(Stepper *STPR, int en)
{

/* This function controls the forward and reverse of the stepper motor.
As well as the minimum # of steps per function call the motor will operate at,

"numstep " is the minimum number of steps the motor should make as required by the stepper library.  */

}


void ctrl::run(int RPM, int numstep, byte dir)
{
STPR->setSpeed(RPM); // Sets the speed of the stepper


if (dir == 1){
digitalWrite(en, HIGH); // Enable driver output
STPR->step(numstep);
}

else if (dir == 0){
digitalWrite(en, HIGH);
STPR->step(-numstep);
}

digitalWrite(en, LOW); // Disable driver output
}

Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You never set your instance variables "STPR" or "en"

You also mask both of the names in your constructor.
Logged

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

Could you elaborate I'm not sure what you mean.

EDIT: I just figured it out thanks.
« Last Edit: March 09, 2013, 04:20:45 pm by IAM_Carbon_Based » Logged

Pages: [1]   Go Up
Jump to: