I am having a little trouble getting my first sketch to run correctly. I am controlling a stepper motor. I have it set up to get a speed command (delay) and a length command ( step count ) from 2 different pots. I have introduced a switch, which seems to be giving me the problem. The sketch will run as I wish when I power up the board. However I want it to only run when I flip the switch on. It seems to disregard the line ( if ( oscswitch== HIGH )) in my sketch and run the loop regardless of switch position. I tested the switch with a multimeter and the switch outputs 4.9v as desired when switched on. Please help! I'm stuck.
[/
int S = 0;
int L = 0;
int D = 0;
int once = 0;
int homeswitch = 0;
int oscswitch = 0;
int limit = 0;
int countdown = 500;
void setup() {
pinMode (9,OUTPUT);//direction pin
pinMode (10, OUTPUT);// pulse pin
pinMode (11, INPUT);// Home Switch Position
pinMode (12, INPUT);// Oscillate Switch Position
pinMode (13, INPUT);// Limit Switch
digitalWrite (8, LOW);
digitalWrite (10, LOW);
digitalWrite (11, LOW);
digitalWrite (12, LOW);
digitalWrite (13, LOW);
}
void loop() {
homeswitch=digitalRead(11);
oscswitch=digitalRead(12);
limit=digitalRead(13);
if ( oscswitch== HIGH ) {
if(countdown==0){
S = analogRead(A1);
S = map(S,0,1023,400,60);
L = analogRead(A2),
L = map(L,0,1023,50,5000);
countdown= 500;}
digitalWrite (10, HIGH);
delayMicroseconds(S);
digitalWrite(10, LOW);
delayMicroseconds(S);
D++;
countdown = countdown-1;
if( D >= L )
{
if (digitalRead(9) == LOW){
digitalWrite(9, HIGH);}
else{digitalWrite(9,LOW);
}
D=0;
delay(S);
}
}
}code]