Soft start for stepper

I am using the Aurdino Uno as a pulse generator to drive a stepper motor control board Gecko G201X. I have a potentiometer set up on the Analog in to set the step speed. Here is the problem if the potentiometer is set too high the stepper just buzzes when it is turned on. The pulse speed needs to be ramped up from 0 but really only when it is first turned on if the pot is set to provide a frequency above 9000 hz. I am using the tone function to generate the frequency. Does anyone have code that will accomplish this? I can make it ramp using a for loop but the darned thing keeps cycling from 0 to the count. I do have programming experience but not in C so I am struggling. Thanks

AccelStepper library perhaps?

alanbredbury: I can make it ramp using a for loop but the darned thing keeps cycling from 0 to the count. I do have programming experience but not in C so I am struggling. Thanks

Since you haven't posted your code it's a bit difficult (i.e. impossible) to give advice.

If you know how to solve the problem in another language it shouldn't be difficult to convert that the C++.

...R

Ok tell me how to initialize a global variable (this I figured out) and assign it a value before I enter the continuous loop (this I don't know how to do. If I can assign a value to a global variable before the void loop I can do what I want no problem. If that is not possible I can post code and a description of what I am trying to do in detail. Thanks

You can set the value of a global variable when you declare it or you can set it in setup(). For example either of these works.

int myGlobal = 123;

void setup() {

}

void loop() {

}

or

int myGlobal;

void setup() {
  myGlobal = 123;

}

void loop() {

}

I would have thought that would have been fairly obvious from studying the examples that come with the IDE. And by the way I'm not a fan of C/C++, I prefer JRuby.

Just in case it's not obvious the "void" before a function is just to tell the compiler that the function doesn't return a result.

...R

GAK I tried to initialize and set the value both in the void setup area. It threw an error. I will try again. Obviously I can then create a loop inside the void loop that will only run once at startup. Thanks

Code that only needs to run once at startup should go in setup(). After the code in setup() has run once loop() repeats continuously while the Arduino is powered up.

If you have code that doesn't work and you don't understand why post a copy here (in code tags). Short examples that are complete are best, excerpts are rarely a good idea.

...R

alanbredbury: GAK I tried to initialize and set the value both in the void setup area. It threw an error. I will try again. Obviously I can then create a loop inside the void loop that will only run once at startup. Thanks

Guessing, I expect you tried to declare and initialize the variable in setup. You need to declare it outside setup as Robin2 illustrated.

All set, works like a charm Thanks

// simple stepper motor ramp up control using
// Gecko drive G251 for a potentiometer

int sensorPin = 0; // The potentiometer is connected to
// analog pin 0
int motorPin = 5; // The pulse lead for the stepper control board is
// connected to pin 5
int hasrun = 1; // global variable so the ramp up only runs once when turned on

void setup() // this function runs once when the sketch starts up
{
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
}
int x;
void loop() // this function runs repeatedly after setup() finishes
{
// First we’ll declare another integer variable
// to store the value of the potentiometer:
int sensorValue;

// sensorValue returns an integer
// number that ranges from 0 (0 Volts) to 1023 (5 Volts).
// We’re sticking this value into the sensorValue variable:
sensorValue = (analogRead(sensorPin)*30);

// sensor value is multiplied by 30 to give a frequenct of
// 1023 * 30 the 30 could be changed to a higher number to achieve a higher frequency
// 30 gave me the top speed I needed

x=(sensorValue);
Serial.print(“Frequency :”);
Serial.println(sensorValue);
//delay(1000);

if (hasrun == 1)
{
for (x=0; x <= (sensorValue); x++){
tone(5,(x));
delay(.5);
hasrun = 0;
}
}
else
{
tone(5,(x));
delay(.5);
}
}