stepper speed and direction control

I am new to Arduino and have been working to get manuel control of a stepper motor for a machane we are building here at the University of Arizona School of Architectiure. After study and looking all over to web for ansewers we finaly put together a combination of hardware and shetches that work using a Arduino and a Polou A4988 with some other bits and pieces we can stop and start, change speed and direction by flipping a switch or turning a knob. One thing to be carefull of is to set the amps on the A4988 or you will burn it up we lost 3 until we got it right. We even wrote some averaging into our sketch to smoth out the steps.

Hope this helps othesrs out there with the same proublems
Sammy ps: a guy from our computer lab helped with the final version, and his name is on the sketch.

/*
*  Author: Desone Burns II
*  Email: dburnsii@email.arizona.edu
*
*/



//simple A4988 connection
//jumper reset and sleep together
//connect  VDD to Arduino 3.3v or 5v
//connect  GND to Arduino GND (GND near VDD)
//connect  1A and 1B to stepper coil 1
//connect 2A and 2B to stepper coil 2
//connect VMOT to power source (9v battery + term)
//connect GRD to power source (9v battery - term)
int sensorPin = A0; //input from potentiometer
int stp = 13;  //connect pin 13 to step
int dir = 12;  // connect pin 12 to dir
int a = 0;     //  gen counter
int sensorValue = 0; // to be scored from pot reading
int button = 2;
int x = 0;
int avg[10];
int startup = 0;
int index = 0;


void setup() 
{                
  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);    
  pinMode(button, INPUT);
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(sensorPin);
   if(startup < 10)
   {
     avg[startup] = sensorValue;
     startup++;
   }
   else
   {
     sensorValue = average(sensorValue);
   }
  if(digitalRead(button) != x)
  {
     if(x == HIGH)
     {
       x = LOW;
     }
     else
     {
       x = HIGH;
     }
     digitalWrite(dir, x);
  }
  digitalWrite(stp, HIGH);
  delay(log10(sensorValue)*2);
  digitalWrite(stp, LOW);
  delay(log10(sensorValue)*2);
  Serial.println(sensorValue);
}

int average(int value)
{
  int i = 0;
  int output = 0;
  avg[index] = value;
  
  if(index < 10)
  {
    index++;
  }
  else
  {
     index = 0; 
  }
  
  while( i < 10)
  {
     output += avg[i];
     i++; 
  }
  return output/10;
}

since I am not able to get my drawings posted yet I will write the hook ups for the pin so you cac try this sketch and give it a try

Arduino UNO
pin 13 to pin step on controller
pin 12 to pin dir on controller
didital pin 2 to Sw2 to R1 2.2M ohm resistor to Grd
pin A0 to center pin R2 potenionmeter Rt pin on R2 to 5 volts left pin on R2 to Grd
pin 12 volts to S1 stop switch to Vmot on controller
pin Grn to Grn to both Gnd on controler + R1 and R2 already mentioned
pin 5 volts to Vdd on controler + R1 and R2 already mentioned
Barrel jack to 12 volt wall wart
USB to conputer to up load sketch

Polou A4988
all that is left is
2B to motor
2A to motor
1A to motor
1B to motor

this is simple to put on a bread board and works well

/*
*  Author: Desone Burns II
*  Email: dburnsii@email.arizona.edu
*
*/



//simple A4988 connection
//jumper reset and sleep together
//connect  VDD to Arduino 3.3v or 5v
//connect  GND to Arduino GND (GND near VDD)
//connect  1A and 1B to stepper coil 1
//connect 2A and 2B to stepper coil 2
//connect VMOT to power source (9v battery + term)
//connect GRD to power source (9v battery - term)
int sensorPin = A0; //input from potentiometer
int stp = 13;  //connect pin 13 to step
int dir = 12;  // connect pin 12 to dir
int a = 0;     //  gen counter
int sensorValue = 0; // to be scored from pot reading
int button = 2;
int x = 0;
int avg[10];
int startup = 0;
int index = 0;


void setup() 
{                
  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);    
  pinMode(button, INPUT);
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(sensorPin);
   if(startup < 10)
   {
     avg[startup] = sensorValue;
     startup++;
   }
   else
   {
     sensorValue = average(sensorValue);
   }
  if(digitalRead(button) != x)
  {
     if(x == HIGH)
     {
       x = LOW;
     }
     else
     {
       x = HIGH;
     }
     digitalWrite(dir, x);
  }
  digitalWrite(stp, HIGH);
  delay(log10(sensorValue)*2);
  digitalWrite(stp, LOW);
  delay(log10(sensorValue)*2);
  Serial.println(sensorValue);
}

int average(int value)
{
  int i = 0;
  int output = 0;
  avg[index] = value;
  
  if(index < 10)
  {
    index++;
  }
  else
  {
     index = 0; 
  }
  
  while( i < 10)
  {
     output += avg[i];
     i++; 
  }
  return output/10;
}

didital pin 2 to Sw2 to R1 2.2M ohm resistor to Grd

No, way too high. Use 10K or lower.

I switched in a 10K resistor for R1 and the set up is still working well thanks for the sugestion
sammy

But now there is much more immunity to noise.

Thanks as I said in my earler post be carefull to set the amps on the Pololu A4988 as we burned 3 of them to a crisp before we caught on.

Sammy

@segg3060 - Don't Double (or Triple) Post.

Have a look at stepper motor basics
The Pololu A4988 web page has a good wiring diagram and other useful stuff

This simple stepper code may be useful for testing

...R

Robin2:
@segg3060 - Don't Double (or Triple) Post.

Yeah, what he said

I missed it do not forget to jump Reset to Sleep on the A4988
Sammy

am trying to get some pictures posted
Sammy

That S2 is still wired up incorrectly. Please read this about connecting switched inputs:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

You are correct I put my bifocals on and looked at my bread board, and read the article you sent and S2 is wired in a pull down configuration. I have updated my schematic (thanks for erassers and#2 pencels). and am including the revised drawing

Sammy

OK are you going to rub out the " R1 = 2M2 " as well?

S2 is wired in a pull down configuration

No it is not. It is a pull up.

The fact that to switch you switch to ground does not make it a pull down configuration.
It is important to use the meaning of words that everyone else uses in order to communicate correctly.

Thank you I reread the articale you sent and I stand corrected

Sammy