int const button = 12,
step = 7,
a1 = 5,
a2 = 4,
b1 = 3,
b2 = 2;
// Global Variables
bool tt [4][4] = { /* Truth table - 4rows, 4columns - 2b,2a,1a,1b*/
{0,1,1,0},
{1,0,1,0},
{1,0,0,1},
{0,1,0,1}
};
int speed = 750;
int past_step = -1;
// User defined functions
void motor(int s){
if(s>0){ // +ve num
for(int i=0; i<s; i++){
if(past_step = 3){
past_step = -1;
}
past_step++;
digitalWrite(b2, tt[past_step][0]);
digitalWrite(a2, tt[past_step][1]);
digitalWrite(a1, tt[past_step][2]);
digitalWrite(b1, tt[past_step][3]);
delay(750);
Serial.println(i);
}
}
else{ // -ve num
for(int i=0; i<s; i++){
if(past_step = 0){
past_step = 4;
}
past_step--;
digitalWrite(b2, tt[past_step][0]);
digitalWrite(a2, tt[past_step][1]);
digitalWrite(a1, tt[past_step][2]);
digitalWrite(b1, tt[past_step][3]);
delay(750);
Serial.println(s);
}
}
}
void setup() {
Serial.begin(9600);
Serial.println("Hello World");
pinMode(button, INPUT_PULLUP);
pinMode(step, OUTPUT);
pinMode(a1, OUTPUT);
pinMode(a2, OUTPUT);
pinMode(b1, OUTPUT);
pinMode(b2, OUTPUT);
}
void loop() {
// Taking input from user
while(!Serial.available());
String input = Serial.readString();
int num = input.toInt();
// Rotating the motor
motor(num);
}
/*
for(int i=0; i<4; i++){
digitalWrite(b2, tt[i][0]);
digitalWrite(a2, tt[i][1]);
digitalWrite(a1, tt[i][2]);
digitalWrite(b1, tt[i][3]);
delay(speed);
}
*/