help with first project

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]

What does the meter say when the switch is off? Do you have a pull down resistor from the switch input pin to ground?

Throw some serial prints in there and watch your countdown variable. You'll only get that if to work when countdown is EXACTLY 0. If you miss that point then countdown goes negative and it will be a long time before it wraps back around to 0.

groundFungus

with switch off the reading is 0 volts. I don't have a resistor to ground.

Delta_G

I see what you are saying now. I have the countdown in so that it only reads (A1, and A2) once every 500 loops. This seems to help because the uno doesn't seem to have enough speed to read the analog inputs each loop, and maintain motor speed. Do you have any suggestions to accomplish both the analog reads on countdown (every 500 loops) and have the loop only run when it reads pin 12 HIGH.

fisherman:
groundFungus

with switch off the reading is 0 volts. I don't have a resistor to ground.

We should talk about that then, because it is a bug waiting to bite you. If you don't have a pull-down resistor and aren't using the built in pull-up resistors then when the switch is open the pin is floating and it could read either HIGH or LOW depending on what stray interference it is picking up at the moment.

Read this: Gammon Forum : Electronics : Microprocessors : Switches tutorial

fisherman:
Delta_G

I see what you are saying now. I have the countdown in so that it only reads (A1, and A2) once every 500 loops. This seems to help because the uno doesn't seem to have enough speed to read the analog inputs each loop, and maintain motor speed. Do you have any suggestions to accomplish both the analog reads on countdown (every 500 loops) and have the loop only run when it reads pin 12 HIGH.

You'll have to separate those two conditions. Or put the line that decrements countdown in an if statement so it only does so when it is greater than 0.

thank you Delta_G you have been very helpful. I will make changes and get back with outcome in the morning.