# PWM programming on Arduino Nano

Hello
Pretty new in this area but won’t start learning about different way to program a stepper motor. My first project is to make a camera move the same speed as the earth is moving, for long time exposure. I tried a couple of month ago, but now I have the plan to use microsteps instead of fullsteps. So far I manage to drive the motor with forty steps instead of four. My motor is a 200 steps per revolution and for one revolution on the camera it takes another 46 rev on my gear. I want the stepper to be as smooth as possible, if it is possible to control that? This is my code so far, but i realize I do it to complicated! I be glad if someone can help me to a easier way of making my camera take great night pictures…: I use Arduino Nano.

#include <Stepper.h>
int motorPin1 = 3;
int motorPin2 = 5;
int motorPin3 = 6;
int motorPin4 = 9;
int a = 255;
int b = 254;
int c = 250;
int d = 243;
int e = 234;
int f = 221;
int g = 204;
int h = 182;
int i = 153;
int j = 111;
int k = 0;

#define runEvery(t) for (static typeof(t) _lasttime;(typeof(t))((typeof(t))millis() - _lasttime) > (t);_lasttime += (t))

unsigned long previousMillis;
unsigned long steps;
byte motor1Step = 0;
int stepInterval;

void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);

}
void loop(){

{
stepInterval = 234;
steps = 0; // reset steps
}

// Clockwise stepper code
if (millis() >= previousMillis){
previousMillis = previousMillis+stepInterval;
motor1Step = motor1Step +1;

if(motor1Step == 5){
motor1Step = 1;
steps = steps +1;
}
}
switch (motor1Step){
case 1:
analogWrite(motorPin1, a); //1
analogWrite(motorPin2, k);
analogWrite(motorPin3, k);
analogWrite(motorPin4, k);
break;
case 2:
analogWrite(motorPin1, b); //2
analogWrite(motorPin2, j);
analogWrite(motorPin3, k);
analogWrite(motorPin4, k);
break;
case 3:

analogWrite(motorPin1, c); //3
analogWrite(motorPin2 , i);
analogWrite(motorPin3, k);
analogWrite(motorPin4, k);
break;
case 4:

analogWrite(motorPin1, d); //4
analogWrite(motorPin2, h);
analogWrite(motorPin3, k);
analogWrite(motorPin4, k);
break;
case 5:

analogWrite(motorPin1, e); //5
analogWrite(motorPin2, g);
analogWrite(motorPin3, k);
analogWrite(motorPin4, k);
break;
case 6:
analogWrite(motorPin1, f); //6
analogWrite(motorPin2, f);
analogWrite(motorPin3, k);
analogWrite(motorPin4, k);
break;
case 7:

analogWrite(motorPin1, g); //7
analogWrite(motorPin2, e);
analogWrite(motorPin3, k);
analogWrite(motorPin4, k);
break;
case 8:

analogWrite(motorPin1, h); //8
analogWrite(motorPin2, d);
analogWrite(motorPin3, k);
analogWrite(motorPin4, k);
break;
case 9:

analogWrite(motorPin1, i); //9
analogWrite(motorPin2, c);
analogWrite(motorPin3, k);
analogWrite(motorPin4, k);

break;
case 10:
analogWrite(motorPin1, j); //10
analogWrite(motorPin2, b);
analogWrite(motorPin3, k);
analogWrite(motorPin4, k);
break;
case 11:
analogWrite(motorPin1, k); //11
analogWrite(motorPin2, a);
analogWrite(motorPin3, k);
analogWrite(motorPin4, k);
break;
case 12:

analogWrite(motorPin1, k); //12
analogWrite(motorPin2, b);
analogWrite(motorPin3, j);
analogWrite(motorPin4, k);

break;
case 13:
analogWrite(motorPin1, k); //13
analogWrite(motorPin2, c);
analogWrite(motorPin3, i);
analogWrite(motorPin4, k);
break;
case 14:

analogWrite(motorPin1, k); //14
analogWrite(motorPin2, d);
analogWrite(motorPin3, h);
analogWrite(motorPin4, k);
break;
case 15:

analogWrite(motorPin1, k); //15
analogWrite(motorPin2, e);
analogWrite(motorPin3, g);
analogWrite(motorPin4, k);
break;
case 16:

analogWrite(motorPin1, k); //16
analogWrite(motorPin2, f);
analogWrite(motorPin3, f);
analogWrite(motorPin4, k);
break;
case 17:

analogWrite(motorPin1, k); //17
analogWrite(motorPin2, g);
analogWrite(motorPin3, e);
analogWrite(motorPin4, k);
break;
case 18:

analogWrite(motorPin1, k); //18
analogWrite(motorPin2, h);
analogWrite(motorPin3, d);
analogWrite(motorPin4, k);
break;
case 19:

analogWrite(motorPin1, k); //19
analogWrite(motorPin2, i);
analogWrite(motorPin3, c);
analogWrite(motorPin4, k);
break;
case 20:

analogWrite(motorPin1, k); //20
analogWrite(motorPin2, j);
analogWrite(motorPin3, b);
analogWrite(motorPin4, k);
break;
case 21:

analogWrite(motorPin1, k); //21
analogWrite(motorPin2, k);
analogWrite(motorPin3, a);
analogWrite(motorPin4, k);
break;
case 22:

analogWrite(motorPin1, k); //22
analogWrite(motorPin2, k);
analogWrite(motorPin3, b);
analogWrite(motorPin4, j);
break;
case 23:

analogWrite(motorPin1, k); //23
analogWrite(motorPin2, k);
analogWrite(motorPin3, c);
analogWrite(motorPin4, i);
break;
case 24:

analogWrite(motorPin1, k); //24
analogWrite(motorPin2, k);
analogWrite(motorPin3, d);
analogWrite(motorPin4, h);
break;
case 25:

analogWrite(motorPin1, k); //25
analogWrite(motorPin2, k);
analogWrite(motorPin3, e);
analogWrite(motorPin4, g);
break;
case 26:

analogWrite(motorPin1, k); //26
analogWrite(motorPin2, k);
analogWrite(motorPin3, f);
analogWrite(motorPin4, f);

break;
case 27:
analogWrite(motorPin1, k); //27
analogWrite(motorPin2, k);
analogWrite(motorPin3, g);
analogWrite(motorPin4, e);
break;
case 28:

analogWrite(motorPin1, k); //28
analogWrite(motorPin2, k);
analogWrite(motorPin3, h);
analogWrite(motorPin4, d);
break;
case 29:

analogWrite(motorPin1, k); //29
analogWrite(motorPin2, k);
analogWrite(motorPin3, i);
analogWrite(motorPin4, c);
break;
case 30:

analogWrite(motorPin1, k); //30
analogWrite(motorPin2, k);
analogWrite(motorPin3, j);
analogWrite(motorPin4, b);
break;
case 31:

analogWrite(motorPin1, k); //31
analogWrite(motorPin2, k);
analogWrite(motorPin3, k);
analogWrite(motorPin4, a);
break;
case 32:

analogWrite(motorPin1, j); //32
analogWrite(motorPin2, k);
analogWrite(motorPin3, k);
analogWrite(motorPin4, b);
break;
case 33:

analogWrite(motorPin1, i); //33
analogWrite(motorPin2, k);
analogWrite(motorPin3, k);
analogWrite(motorPin4, c);
break;
case 34:

analogWrite(motorPin1, h); //34
analogWrite(motorPin2, k);
analogWrite(motorPin3, k);
analogWrite(motorPin4, d);
break;
case 35:

analogWrite(motorPin1, g); //35
analogWrite(motorPin2, k);
analogWrite(motorPin3, k);
analogWrite(motorPin4, e);
break;
case 36:

analogWrite(motorPin1, f); //36
analogWrite(motorPin2, k);
analogWrite(motorPin3, k);
analogWrite(motorPin4, f);
break;
case 37:

analogWrite(motorPin1, e); //37
analogWrite(motorPin2, k);
analogWrite(motorPin3, k);
analogWrite(motorPin4, g);
break;
case 38:

analogWrite(motorPin1, d); //38
analogWrite(motorPin2, k);
analogWrite(motorPin3, k);
analogWrite(motorPin4, h);
break;
case 39:

analogWrite(motorPin1, c); //39
analogWrite(motorPin2, k);
analogWrite(motorPin3, k);
analogWrite(motorPin4, i);
break;
case 40:

analogWrite(motorPin1, b); //40
analogWrite(motorPin2, k);
analogWrite(motorPin3, k);
analogWrite(motorPin4, j);
break;

}}

What do you have connected to the Arduino? Using PWM to drive stepper motors is highly unusual. The Arduino can't provide near enough current to power a stepper motor.

Pretty new in this area...

# 7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html