Hi All, hope you all had a good 4th of July.
Now, I have this simple stepper motor sketch which I use I2C to run the show. ( I used I2C just to familiarize myself with I2C ).
As you can see in the code, the stepper motor spins till the stop switch is pushed.
Now, I want to change the code.
I want the motor to spin ONLY when switch1Pin or switch2Pin are pushed down. I want the motor to turn only while the switch is being pushed down and I want it to stop the moment I take the finger off the switch.
How do I do that?
Do I use "While" statements instead of an "if" statement?
I do NOT have the slave sketch. I downloaded it on a pro-mini and then deleted it from my laptop. All I remember is that case 0 is stop, case 1 is one directon and case 2 is the other direction.
Thank you for your replies!
#include <Wire.h>
const int stopSwitch = 9;
const int switch1Pin = 10;
const int switch2Pin = 11;
int switch1State = 0; //Variable to store the state of switch 1.
int switch2State = 0; //Variable to store the state of switch 2.
int stopSwitchState = 0; //Variable to store the state of the stop switch.
void setup() {
Serial.begin(9600);
Wire.begin(); // Lack of an argument in the Parentheses indicates that it is a MASTER.
// If it said Wire.begin(0x05), that would be used to set the device as
// a slave with the address 0x05.
pinMode(3, OUTPUT); // Sets pin 3 as output.
pinMode(4, OUTPUT); // Sets pin 4 as output.
pinMode(9, INPUT); // Sets pin 9 as input.
pinMode(10, INPUT); // Sets pin 10 as input.
pinMode(11, INPUT); // Sets pin 11 as input.
}
void loop() {
switch1State = digitalRead(10);
switch2State = digitalRead(11);
stopSwitchState = digitalRead(9);
if (switch1State == LOW && switch2State == LOW)
{
Serial.println("Both the switches are LOW");
}
if (switch1State == HIGH && switch2State == HIGH)
{
Serial.println("Both the switches are HIGH");
}
if (switch1State == HIGH && switch2State == LOW)
{
digitalWrite(3, HIGH);
delay(300);
digitalWrite(3, LOW);
delay(300);
Serial.println("Switch 1 was just pushed. Switch 1 has gone HIGH!");
Wire.beginTransmission(66);
Wire.write(1);
Wire.endTransmission();
delay(10);
}
if (switch1State == LOW && switch2State == HIGH)
{
digitalWrite(4, HIGH);
delay(300);
digitalWrite(4, LOW);
delay(300);
Serial.println("Switch 2 was just pushed. Switch 2 has gone HIGH!");
Wire.beginTransmission(66);
Wire.write(2);
Wire.endTransmission();
delay(10);
}
if (stopSwitchState == HIGH)
{
Serial.println("The stop switch has gone HIGH. The stepper should stop rotating any moment! ");
Wire.beginTransmission(66);
Wire.write(0);
Wire.endTransmission();
delay(50);
}
}