My code is attached below, but the goal is that once zstate changes to 0 the motor spins one way 40 times, a stepper motor spins, and then the motor spins the other way 40 times. The values are being read constantly so z is usually one, and only changes to 0 for a moment before it changes back, I'm not sure how to get the Uno to hold this zero value for the 19 seconds before beginning to read again. Right now it takes one half step in one direction and then one in the other direction.
Thank you!
//setup things
#include <Servo.h>
Servo ClawServo;
int n=1, ct; //used to cycle through the half steps
int yState = 0; //these are used to define if the toggle is moved forward, back or not at all
int xState = 0;
int zState = 1;
int a1;
int a2;
int b1;
int b2;
int d = 1; //this gives it time to move after each command
const int yPin = A4; //to make sense when called to later
const int xPin = A3;
void setup() {
for (int i = 1; i<=12; i++){ //sets all the motors pins as output
pinMode(i, OUTPUT);
}
Serial.begin(9600);
ClawServo.attach(13);
pinMode(0, INPUT_PULLUP); //says this is a button that will give 0 when pressed - kinda flipflopped
} //setup ending
void loop() {
Serial.println(digitalRead(0));
Serial.println(analogRead(yPin));
Serial.println(analogRead(xPin));
yState = analogRead(yPin); //read the toggle value, if being pushed up make it 1, if down make it -1, if nothing make it 0:
if (yState > 700 ){ //want a high y value to make it move 'up'
a1 = 1; //set pins for y motors
a2 = 3;
b1 = 2;
b2 = 4;
half_step(1);
}
if (yState < 300 ) {
a1 = 1; //set pins for y motors
a2 = 3;
b1 = 2;
b2 = 4;
half_step(-1);
}
xState = analogRead(xPin); //read the toggle value
if (xState > 700 ){ // want a high x to make it move to the right
a1 = 5; //set pins for x motor
a2 = 7;
b1 = 6;
b2 = 8;
half_step(1);
}
if (xState < 300 ){
a1 = 5; //set pins for x motor
a2 = 7;
b1 = 6;
b2 = 8;
half_step(-1);
}
// else
// xState = 0;
bool zero;
zState = digitalRead(0); //reads if the joystick is being pressed, 0 pushed, 1 not pushed thanks to pullup function in setup
if (zState == 0){ //if the current state is 0 - meaning that it had to go from 1 to 0, or be pushed
zero = true;
}
if(zero){
a1 = 9; //set pins for z motor
a2 = 11;
b1 = 10;
b2 = 12;
for (int s = 1; s<=40; s++){ //saying it takes 40 half steps to go down
a1 = 9; //set pins for z motor
a2 = 11;
b1 = 10;
b2 = 12;
half_step(-1);
}
delay(8000);
int a; //servo stuff
a = analogRead(0);
a = map(a,0,1023,0,180);
ClawServo.write(a);
delay(3000);//time for claw to open and close rn 3 seconds
for (int su = 1; su<=40; su++){
a1 = 9; //set pins for z motor
a2 = 11;
b1 = 10;
b2 = 12;
half_step(1);
}
}
delay(8000);
zero = false; //reset zero for next loop
}
}//loop ending
//////////////////THIS IS THE MAIN FUNCTION FOR HALF STEP//////////////////////
void half_step(double dn){
n=n+dn;
if (n>8) n=1;
if (n<1) n=8;
switch (n){
case 1:
digitalWrite(a1,HIGH);
digitalWrite(a2,LOW);
digitalWrite(b1,LOW);
digitalWrite(b2,LOW);
break;
case 2:
digitalWrite(a1,HIGH);
digitalWrite(a2,LOW);
digitalWrite(b1,HIGH);
digitalWrite(b2,LOW);
break;
case 3:
digitalWrite(a1,LOW);
digitalWrite(a2,LOW);
digitalWrite(b1,HIGH);
digitalWrite(b2,LOW);
break;
case 4:
digitalWrite(a1,LOW);
digitalWrite(a2,HIGH);
digitalWrite(b1,HIGH);
digitalWrite(b2,LOW);
break;
case 5:
digitalWrite(a1,LOW);
digitalWrite(a2,HIGH);
digitalWrite(b1,LOW);
digitalWrite(b2,LOW);
break;
case 6:
digitalWrite(a1,LOW);
digitalWrite(a2,HIGH);
digitalWrite(b1,LOW);
digitalWrite(b2,HIGH);
break;
case 7:
digitalWrite(a1,LOW);
digitalWrite(a2,LOW);
digitalWrite(b1,LOW);
digitalWrite(b2,HIGH);
break;
case 8:
digitalWrite(a1,HIGH);
digitalWrite(a2,LOW);
digitalWrite(b1,LOW);
digitalWrite(b2,HIGH);
break;
}
delay(d);
}