// http://www.airspayce.com/mikem/arduino/AccelStepper/
const byte dirPin = 9; // stepper as speed-indicating pot
const byte stepPin = 10; //
const byte enPin =13;
const int ROW_NUM = 1; //four rows
const int COLUMN_NUM = 2; //three columns
#define key1 6 //connect wire 1 to pin 2
#define key2 7 //connect wire 2 to pin 3
#define key3 11 //connect wire 1 to pin 2
#define key4 12 //connect wire 2 to pin 3
int key;
void setup()
{
Serial.begin(115200);
Serial.println("key");
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
pinMode(key1, INPUT_PULLUP);// set pin as input
pinMode(key2, INPUT_PULLUP);// set pin as input
pinMode(key3, INPUT_PULLUP);// set pin as input
pinMode(key4, INPUT_PULLUP);// set pin as input
key=1;
}
void loop()
{
int pot_val = analogRead(A0);
key=3;
int key1S = digitalRead(key1);// read if key1 is pressed
int key2S = digitalRead(key2);// read if key1 is pressed
int key3S = digitalRead(key3);// read if key1 is pressed
int key4S = digitalRead(key4);// read if key1 is pressed
// Code written for Robojax.com video Tutorial
if(!key1S){
Serial.println("key 1 is pressed");
key=1;
}
if(!key2S){
Serial.println("key 2 is pressed");
key=2;
}
if(!key3S){
Serial.println("key 3 is pressed");
key=4;
}
if(!key4S){
Serial.println("key 4 is pressed");
key=4;
}
if(key==1)
{
Serial.println(key);
//key=1;
//Serial.println( map(pot_val, 0, 1023, 0, 1000));
digitalWrite(enPin, LOW);
digitalWrite(dirPin, HIGH);
// Spin the stepper motor:
for (int i = 0; i < 100; i++)
{
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
//delay(tdelay);
//delayMicroseconds(1000000);
digitalWrite(stepPin, LOW);
delay(map(pot_val, 0, 1023, 0, 1000));
// delayMicroseconds(1000000);
}
}
if(key==2)
{
Serial.println(key);
//key=1;
digitalWrite(enPin, LOW);
digitalWrite(dirPin, LOW);
// Spin the stepper motor:
for (int i = 0; i < 100; i++)
{
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
//delay(tdelay);
//delayMicroseconds(1000000);
digitalWrite(stepPin, LOW);
delay(map(pot_val, 0, 1023, 0, 1000));
// delayMicroseconds(1000000);
}
}
delay(1000);
}