Project 10

Everything is wired correct and coded correct. But the onoffswitch does not work well. I attached code in txt file . please let me know how to fix this problem.

Thanks,

project 10.txt (1.58 KB)

Attached code for those who may not be able to download.

const int controlPin1 = 2;
const int controlPin2 = 3;
const int enablePin = 9;
const int directionSwitchPin =4;
const int onOffSwitchStateSwitchPin = 5;
const int potPin = A0;

int onOffSwitchState = 0;
int previousOnOffSwitchState= 0;
int directionSwitchState =0;
int previousDirectionSwitchState = 0;
int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(directionSwitchPin, INPUT);
  pinMode(onOffSwitchStateSwitchPin, INPUT);
  pinMode(controlPin1, OUTPUT);
  pinMode(controlPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
  onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
  delay(1);
  directionSwitchState = digitalRead(directionSwitchPin);
  motorSpeed = analogRead(potPin)/4;
  
  if(onOffSwitchState != previousOnOffSwitchState){
   if(onOffSwitchState == HIGH) {
     motorEnabled = !motorEnabled;
   }
  }
  if(directionSwitchState != previousDirectionSwitchState){
   if(directionSwitchState == HIGH) {
     motorDirection = !motorDirection;
   }
  }
  if (motorDirection == 1) {
    digitalWrite(controlPin1, HIGH);
    digitalWrite(controlPin2, LOW);
  }
  else {
    digitalWrite(controlPin1, LOW);
    digitalWrite(controlPin2, HIGH);
  }
  if (motorEnabled = 1){
    analogWrite(enablePin, motorSpeed);
  }
  else {
    analogWrite(enablePin, 0);
  }
  previousDirectionSwitchState = directionSwitchState;
  previousOnOffSwitchState = onOffSwitchState;
}

What do you mean when you say it doesn't work well? What happens? How does that compare to what you expect? Details of the problem are a must.

That switch may need to be debounced.

I am sorry for your confusion. When I hook up with the usb power source, I expect the motor to be not running but it runs as soon as I plug usb cord and also, the button for onoff switch does not function as start/stop.

Got an external pull-down on that pin?

I am sorry, but I don’t understand what you mean by external pull-down on that pin. I attached the picture for your information.

Thanks,

OK, you do have the pulldown resistors. Those are the resistors between your button pins and ground so the pins read 0V when the button isn't pressed. Good, so it's not a floating pin.

Which really leaves us just with the bouncing switch. Look here. Gammon Forum : Electronics : Microprocessors : Switches tutorial about halfway down the page is where debouncing starts.

Thank you for checking, I keep trying to debounce the switch by adding delay time like below. Is it right way to fix the problem?

if(onOffSwitchState != previousOnOffSwitchState){
if(onOffSwitchState == HIGH) {
motorEnabled = !motorEnabled;
delay(10);
}
}

I would use more like 50ms. It’s not the best method since it blocks, but it will get the job done for now while you learn other things. If that fixes things then it was definitely a bounce issue.

It still shows the same problem. Maybe it is not bouncing problem?

OK, can you describe the problem a little better? You press the button and nothing happens? Sometimes it stops and starts the motor but not always?

Damn. I gotta get my eyes checked. Can't believe I missed this one:

if (motorEnabled = 1){

= is asignment
== is comparison

http://www.gammon.com.au/tips
See #3

wow. Thank you so much for your help! I never knew that that affects a lot of arduino behaviour.

hunterlee347:
wow. Thank you so much for your help! I never knew that that affects a lot of arduino behaviour.

It's not really that it affects its behavior so much as it makes that if statement always true. So the motor will never turn off.