Hello all, I'm learning programming for the first time for my senior design project and I could use a hand.
I have a loop full of if statements and I need them to trigger in consecutive order, so I have assigned a variable 'stage' that increases by one within each if statement, then I make the next if statement require my 'stage' variable to be a certain number before it will run that command. In this example I'm just trying to swap between stages 0 and 1 consecutively.
//define constants
const int dirPin = 2; // Direction
const int stepPin = 3; // Step
const int dirPin_2 = 4;
const int stepPin_2 = 5;
const int solenoidPin = 7; //solenoid
const int Detector = 8; //laser receiver
const int STEPS_PER_REV = 200;
int time;
int dir;
int steps;
int speed;
int stage = 0;
void setup() {
// Setup the pins
pinMode(stepPin,OUTPUT);
pinMode(stepPin_2,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(dirPin_2,OUTPUT);
pinMode(solenoidPin, OUTPUT);
pinMode(Detector, INPUT);
}
//solenoid function
int solenoidControl(int time) {
digitalWrite(solenoidPin, HIGH);
delay(time);
digitalWrite(solenoidPin, LOW);
delay(10);
}
//stepper function
int motorControl(int dir, int steps, int speed) {
//direction
digitalWrite(dirPin,dir);
for(int x = 0; x < steps; x++) { //"steps" controls number of rotations
digitalWrite(stepPin,HIGH);
delayMicroseconds(speed); //controls speed
digitalWrite(stepPin,LOW);
delayMicroseconds(speed);
}
}
void loop() {
if (stage = 0) {
if (digitalRead(Detector) == LOW) {
int time = 1000;
solenoidControl(time);
delay(10);
int dir = LOW;
int steps = STEPS_PER_REV*2;
int speed = 2000;
motorControl(dir, steps, speed);
int stage = 1;
delay(10);
}
}
if (stage = 1) {
if (digitalRead(Detector) == LOW) {
int dir = HIGH;
int steps = STEPS_PER_REV;
int speed = 1000;
motorControl(dir, steps, speed);
int stage = 0;
delay (10);
}
}
}
When I go to run this code, the motor just sits there and spins, the solenoid doesn't even move.
Once I get this basic indexing to work I will apply what I learn to my full code which will have 60 stages or so.
Any ideas as to why this is happening?
Thanks in advance