Arduino:
/*ListenTonothing*/
#include <LiquidCrystal.h>
const int rs = 12, en = 13, d4 = 8, d5 = 9, d6 = 10, d7 = 11;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int button=1;//Direction Variable
int pulser = 2;//pin
int select = A4;//Select Pin
int pulse = 1;
int FSSPpulse = 1;
String inString = "";
unsigned long debounce=0;//Last Button Pressed
int asd=500;//Min time between two presses
int option=1;//Exp Select Variable
//Stepper Pins
int A=4;
int A8=5;
int B=6;
int B8=7;
int expsel=0;
const int numberOfPieces = 4;
String pieces[numberOfPieces];
String input = "";
int counter = 0;
int lastIndex = 0;
int toggle=0;//Direction
int pwm=0;//SpeedPOT
int ppulses=0;
int flag=0;
int pulses;
void setup()
{
pinMode(A,OUTPUT);
pinMode(A8,OUTPUT);
pinMode(B,OUTPUT);
pinMode(B8,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(3,INPUT);//interrupt
pinMode(pulser,INPUT);//Pulser
pinMode(A3,INPUT);//POT Delay
pinMode(select,INPUT);
lcd.begin(20, 4);
lcd.setCursor(0,0);
lcd.print("Select COM port");
lcd.setCursor(0,1);
lcd.print("on PC");
debounce=millis();
Serial.begin(9600);
while(!Serial)
{
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Select");
lcd.setCursor(0,1);
lcd.print("Experiment:");
}
void loop()
{
if(expsel == 3)
{
lcd.setCursor(0,0);
lcd.print("Full Step Single");
lcd.setCursor(0,1);
lcd.print("Phase Step Run");
if(ppulses != pulses)
{
ppulses = pulses;
flag=1;
}
exp7(1,1);
}
if(expsel == 2)
{
lcd.setCursor(0,0);
lcd.print("Full Step Single");
lcd.setCursor(0,1);
lcd.print("Phase Free Run");
freeFSSP();
}
if(expsel == 5)
{
lcd.setCursor(0,0);
lcd.print("Full Step Two");
lcd.setCursor(0,1);
lcd.print("Phase Step Run");
if(ppulses != pulses)
{
ppulses = pulses;
flag=1;
}
exp7(1,2);
}
if(expsel == 4)
{
lcd.setCursor(0,0);
lcd.print("Full Step Two");
lcd.setCursor(0,1);
lcd.print("Phase Free Run");
freeFSTP();
}
if(expsel == 7)
{
lcd.setCursor(0,0);
lcd.print("Half Step Step");
lcd.setCursor(0,1);
lcd.print("Run");
if(ppulses != pulses)
{
ppulses = pulses;
flag=1;
}
exp7(2,1);
}
if(expsel == 6)
{
lcd.setCursor(0,0);
lcd.print("Half Step Free");
lcd.setCursor(0,1);
lcd.print("Run");
freeHS();
}
if(expsel == 1)
{
lcd.setCursor(0,0);
lcd.print("Wobble Mode");
WM();
}
}
void FSSPM(int i)
{
switch(i)
{
case 1: digitalWrite(A,HIGH);
digitalWrite(A8,LOW);
digitalWrite(B,LOW);
digitalWrite(B8,LOW);
break;
case 2: digitalWrite(A,LOW);
digitalWrite(A8,HIGH);
digitalWrite(B,LOW);
digitalWrite(B8,LOW);
break;
case 3: digitalWrite(A,LOW);
digitalWrite(A8,LOW);
digitalWrite(B,HIGH);
digitalWrite(B8,LOW);
break;
case 4: digitalWrite(A,LOW);
digitalWrite(A8,LOW);
digitalWrite(B,LOW);
digitalWrite(B8,HIGH);
break;
default:break;
}
}
void stepFSSP()
{
if(flag==1)//Full Step Single Phase Step Running
{
if(button==1)
{
FSSPpulse++;
if(FSSPpulse>4)
{
FSSPpulse=1;
}
FSSPM(FSSPpulse);
delay(400);
}
if(button==0)
{
FSSPpulse--;
if(FSSPpulse<1)
{
FSSPpulse=4;
}
FSSPM(FSSPpulse);
delay(400);
}
flag=0;
}
}
void freeFSSP()
{
if(button==1)//Full Step Single Phase Free Running
{
for(int i=1; i<5; i++)
{
FSSPM(i);
delay(delayPOT());
}
}
if(button==0)
{
for(int i=4; i>0; i--)
{
FSSPM(i);
delay(delayPOT());
}
}
}
void FSTP(int i)
{
switch(i)
{
case 1: digitalWrite(A,HIGH);
digitalWrite(A8,HIGH);
digitalWrite(B,LOW);
digitalWrite(B8,LOW);
break;
case 2: digitalWrite(A,LOW);
digitalWrite(A8,HIGH);
digitalWrite(B,HIGH);
digitalWrite(B8,LOW);
break;
case 3: digitalWrite(A,LOW);
digitalWrite(A8,LOW);
digitalWrite(B,HIGH);
digitalWrite(B8,HIGH);
break;
case 4: digitalWrite(A,HIGH);
digitalWrite(A8,LOW);
digitalWrite(B,LOW);
digitalWrite(B8,HIGH);
break;
default:break;
}
}
void stepFSTP()
{
if(flag==1)//Full Step Two Phase Step Running
{
if(button==1)
{
pulse++;
if(pulse>4)
{
pulse=1;
}
FSTP(pulse);
delay(400);
}
if(button==0)
{
pulse--;
if(pulse<1)
{
pulse=4;
}
FSTP(pulse);
delay(400);
}
flag=0;
}
}
void freeFSTP()
{
if(button==1)//Full Step Two Phase Free Running
{
for(int i=1; i<5; i++)
{
FSTP(i);
delay(delayPOT());
}
}
if(button==0)
{
for(int i=4; i>0; i--)
{
FSTP(i);
delay(delayPOT());
}
}
}
void HS(int i)
{
switch(i)
{
case 1: digitalWrite(A,HIGH);
digitalWrite(A8,LOW);
digitalWrite(B,LOW);
digitalWrite(B8,LOW);
break;
case 2: digitalWrite(A,HIGH);
digitalWrite(A8,HIGH);
digitalWrite(B,LOW);
digitalWrite(B8,LOW);
break;
case 3: digitalWrite(A,LOW);
digitalWrite(A8,HIGH);
digitalWrite(B,LOW);
digitalWrite(B8,LOW);
break;
case 4: digitalWrite(A,LOW);
digitalWrite(A8,HIGH);
digitalWrite(B,HIGH);
digitalWrite(B8,LOW);
break;
case 5: digitalWrite(A,LOW);
digitalWrite(A8,LOW);
digitalWrite(B,HIGH);
digitalWrite(B8,LOW);
break;
case 6: digitalWrite(A,LOW);
digitalWrite(A8,LOW);
digitalWrite(B,HIGH);
digitalWrite(B8,HIGH);
break;
case 7: digitalWrite(A,LOW);
digitalWrite(A8,LOW);
digitalWrite(B,LOW);
digitalWrite(B8,HIGH);
break;
case 8: digitalWrite(A,HIGH);
digitalWrite(A8,LOW);
digitalWrite(B,LOW);
digitalWrite(B8,HIGH);
break;
default:break;
}
}
void stepHS()
{
if(flag==1)//Half Step Step Running
{
if(button==1)
{
FSSPpulse++;
if(FSSPpulse>8)
{
FSSPpulse=1;
}
HS(FSSPpulse);
delay(400);
}
if(button==0)
{
FSSPpulse--;
if(FSSPpulse<1)
{
FSSPpulse=8;
}
HS(FSSPpulse);
delay(400);
}
flag=0;
}
}
void freeHS()
{
if(button==1)//Half Step Free Running
{
for(int j=1; j<9; j++)
{
HS(j);
delay(delayPOT());
}
}
if(button==0)
{
for(int j=8; j>0; j--)
{
HS(j);
delay(delayPOT());
}
}
}
void serialEvent()
{
if(Serial.available()>0)
{
char ch = Serial.read();
if (ch == '\n')
{
for (int i = 0; i < input.length(); i++)
{
if (input.substring(i, i+1) == ",")
{
pieces[counter] = input.substring(lastIndex, i);
lastIndex = i + 1;
counter++;
}
if (i == input.length() - 1)
{
pieces[counter] = input.substring(lastIndex, i);
}
}
input = "";
counter = 0;
lastIndex = 0;
button = pieces[0].toInt();
expsel = pieces[1].toInt();
pwm = pieces[2].toInt();
pulses = pieces[3].toInt();
}
else
{
input += ch;
}
}
}
void WM()
{
FSSPM(1);
delay(20);
FSSPM(4);
delay(20);
}
int delayPOT()
{
int halu;
halu=pwm;
halu=map(halu,0,255,10,1000);
return halu;
}
void exp7(int a,int b)//Step Movement
{
while(a==1 && b==1)
{
stepFSSP();
}
while(a==1 && b==2)
{
stepFSTP();
}
while(a==2)
{
stepHS();
}
}