Go Down

Topic: step dir motor and speed control without library (Read 7745 times) previous topic - next topic

copachino

hi im trying to program a stepper to move with a potenciometer and change the speed, also with a switch to control to rotation, cww or cw, but i have some trouble with the library, some how seems not to work fine, and IDE its asking me to declare the funtions on the library itself

Code: [Select]
#include <configMe.h>

#include <Stepper.h>


#define MI_PIN_STEP 5
#define MI_PIN_DIR 2
const int  gir = 10;   
const int stepsPerRevolution = 200;
int stepCount = 0;

void setup()
{
  //Wire.begin ();
  Serial.begin ( 57600 );
 
  pinMode ( MI_PIN_STEP, OUTPUT );
  pinMode ( MI_PIN_DIR, OUTPUT );
 
  pinMode(gir,INPUT);
 
 



 
}

void loop()
{
 
  int sensorReading = analogRead(A0);
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
 
  if (motorSpeed > 0) {
    setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    step(stepsPerRevolution / 100);
  }
 
 
// {

if(gir==HIGH)
{
  digitalWrite ( MI_PIN_DIR, HIGH ); // Poner una dirección

}
  if(gir==LOW)
  {
digitalWrite ( MI_PIN_DIR, LOW ); // Poner la otra dirección

}

  // pasos
  digitalWrite ( MI_PIN_STEP, LOW );
 
  digitalWrite ( MI_PIN_STEP, HIGH );
 
 
   
}



Code: [Select]
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
D:\arduino-1.0.6\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -ID:\arduino-1.0.6\hardware\arduino\cores\arduino -ID:\arduino-1.0.6\hardware\arduino\variants\standard -ID:\arduino-1.0.6\libraries\Stepper C:\Users\MILAU\AppData\Local\Temp\build6855970105847057216.tmp\sketch_oct18a.cpp -o C:\Users\MILAU\AppData\Local\Temp\build6855970105847057216.tmp\sketch_oct18a.cpp.o

sketch_oct18a.ino: In function 'void loop()':
sketch_oct18a:36: error: 'setSpeed' was not declared in this scope
sketch_oct18a:38: error: 'step' was not declared in this scope

Robin2

I have been trying to answer this question in your other Thread. Why have you started a new Thread just to waste people's time?

I am asking the Moderator to merge this with the other Thread.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2


i see my error, but still does not complie


I tried to compile the code in Reply #11 and the only error was on this line
Code: [Select]
Stepper myStepper(stepsPerRevolution, 5, 3,);
and the problem is that there is comma after the '3'.

When I delete that comma it compiles. That does not mean it would work because it may be that there should be a number after that extra comma.

HOWEVER ...
That code does NOT give the error you have quoted
Quote
sketch_oct18a:37: error: 'myStepper' was not declared in this scope

which leads me to the conclusion that you have not posted the code that is actually causing you a problem.

AND ...
You started another Thread about the same problem - controlling a stepper without a library. I have already explained how to do that in this Thread. If there is part of my explanation that you don't understand please let me know.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

copachino


I have been trying to answer this question in your other Thread. Why have you started a new Thread just to waste people's time?

I am asking the Moderator to merge this with the other Thread.

...R


you can you wharever you want to do...

nickgammon

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

- Moderator
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

copachino

nick you can delete the post, i have found the way to solve my problem, so this its closed

nickgammon

We don't just delete threads because they are solved. This forum is supposed to be a knowledge-base, to help other people.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

copachino

i know, but i never solved the problem with the IDE, and my bad programming, so i just wrote i new code with accelstepper, but this thread its not working save since there its not good information here

Robin2


i know, but i never solved the problem with the IDE, and my bad programming, so i just wrote i new code with accelstepper, but this thread its not working save since there its not good information here


It would help others if you post your working code and explain why the information here was not suitable for your project and how you figured out your solution.

I would also like to learn so that my advice can be better focused next time.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

copachino



i know, but i never solved the problem with the IDE, and my bad programming, so i just wrote i new code with accelstepper, but this thread its not working save since there its not good information here


It would help others if you post your working code and explain why the information here was not suitable for your project and how you figured out your solution.

I would also like to learn so that my advice can be better focused next time.

...R


ok here its the working code, and i could not figure out what was wrong in the library but i suppect there was something wrong on my object declarations, this code im posting uses accelstepper, the lib its way huge improve of the original stepper.h, and its perfect to work with step/dir drivers.

Code: [Select]
#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8);

// Define our three input button pins
#define  LEFT_PIN  4 // left and right its one switch a spdt on/on with center pin at ground
#define  STOP_PIN  3
#define  RIGHT_PIN 2

// Define our analog pot input pin
#define  SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 4000
#define  MIN_SPEED 0.1

void setup() {
  // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
  stepper1.setMaxSpeed(10000.0);
 
  // Set up the three button inputs, with pullups
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
}

void loop() {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
 
  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) {   
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }

  // We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop). 
  if (analog_read_counter > 0) {
    analog_read_counter--;
  }
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    stepper1.runSpeed();
    //  And scale the pot's value from min to max speeds
    current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
    // Update the stepper to run at this new speed
    stepper1.setSpeed(current_speed);
  }

  // This will run the stepper at a constant speed
  stepper1.runSpeed();
 
 

}

Robin2


ok here its the working code,


Thanks.

I guess I had assumed from the title of your Thread that you didn't want to use a library.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up