Go Down

Topic: PROJECT 10 small modification! (Read 913 times) previous topic - next topic

sina92

Hi guys. I just finished project 10 (gave up on my own code and pretty much copied whatever was in the book  :smiley-mr-green:).

I want to make a small modification:

When I hold the motorSwitch button down, I want to wait 2 seconds before the motor starts spinning. However, when I want to shut it down, I just press the switch.

Can someone please explain how I can do this in a smart way (all of my ideas were terrible)?

The code so far:

Code: [Select]
int enablePin = 9;
int OnOffMotorSwitch = 5;
int directionSwitch = 4;

int OnOffMotorState = 0;
int directionState = 0;

int previousMotorState = 0;
int previousDirectionState = 0;

int motorEnable = 0;
int directionEnable = 0;

void setup(){
pinMode(enablePin, OUTPUT);
pinMode(OnOffMotorSwitch, INPUT);
pinMode(directionSwitch, INPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(6, OUTPUT);

digitalWrite(enablePin, LOW);

Serial.begin(9600);

}


void loop(){
  int motorDC = analogRead(A0) / 4;
  int OnOffMotorState = digitalRead(OnOffMotorSwitch);
  int directionState = digitalRead(directionSwitch);

if(OnOffMotorState != previousMotorState){
   if(OnOffMotorState == HIGH){
     motorEnable = !motorEnable;
     }
   }
 
if(directionState != previousDirectionState){
  if(directionState == HIGH){
   directionEnable = !directionEnable;
  }
}

if(motorEnable == true){
  analogWrite(enablePin, motorDC);
}
else{
   analogWrite(enablePin, 0);
}


if(directionEnable == true){
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
}
else{
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
}


 
  previousMotorState = OnOffMotorState;
  previousDirectionState = directionState;
 
}


Thanks in advance.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy