Connect a motor two push buttons and two LEDS to the Arduino
Connect a motor, two push buttons and two LEDS to the Arduino as shown below. The fan should operate as listed.
Begin with
int buttonPin1 = 2;
 int buttonPin2 = 4;
 int ledPinGreen = 11;
 int ledPinRed = 6;
 int motorPin = 9;
Please program in Arduino as the following:
1. Fan is started or stopped by pressing push button 1
When starting the fan, ramp up using 110ms delay from 0 to 4 volts (levels 0 to 200), fan remains running at 4 volts.
When stopping the fan, slow down using the same values (110ms delay and level 200 to 0).
2. When operator presses the overload switch (button 2), fan starts working at full speed (255) and the overload alarm is lit (red LED).
3. When the overload switch (button 2) is pressed again, fan returns to normal operation at 4 volts (level 200).
4. Pressing the overload button when the fan is not operating has no effect.
5. Anytime the fan is on, on/off indictor is lit, otherwise it is off.
Solution
const int buttonPin1 = 2;
 const int buttonPin2 = 4;
 const int ledPinGreen = 11;
 const int ledPinRed = 6;
 const int motorPin = 9
//state varibles
 boolean B1_state = false; //button 1 state
 boolean B2_state = false; //button 2 state
// function declaration
 void motorRun_Stop(boolean);
 void overload(boolean,boolean);
void setup()
 {
 Serial.begin(9600); // for debugging
 pinMode(buttonPin1,INPUT); // declaring as input
 digitalWrite(buttonPin1,HIGH); // activating the pullup resistor
 pinMode(buttonPin2,INPUT); // declaring as input
 digitalWrite(buttonPin1,HIGH); // activating the pullup resistor
 pinMode(ledPinGreen,OUTPUT);
 pinMode(ledPinRed,OUTPUT);
 pinMode(motorPin, OUTPUT);
 }
void loop()
 {
 if(digitalRead(buttonPin1)== 1)
 {
     B1_state != B1_state;
     motorRun_Stop(B1_state);
 }
 else if(digitalRead(buttonPin2)== 1)
 {
      B2_state != B2_state;
     overload();
 }
 else
 {
 }
 }
void motorRun_Stop(boolean x)
 {
 byte wait = 0;
 if(x == true )
 {
     Serial.println(\"Motor has Overloaded\");
     digitalWrite(ledPinGreen,HIGH);
     for (int i = 0; i<200;i++)
     {
       analogWrite(motorPin,i);
       wait = micros();
       while((micros()-wait)>550);
     }
 }
 else if(x == false)
 {
   
     for (int i = 200; i>0;i--)
     {
       analogWrite(motorPin,i);
       wait = micros();
       while((micros()-wait)>550);
     }
     Serial.println(\"Motor has Stoped\");
     digitalWrite(ledPinGreen,LOW);
 }
 else
 {
 }
 }
void overload(boolean x,boolean y)
 {
 byte wait = 0;
 if(x == true && y == true)
 {
     analogWrite(motorPin,255);
     Serial.println(\"Motor has overloaded\");
     digitalWrite(ledPinRed,HIGH);
   
 }
 else if(x == true && y==false)
 {
     analogWrite(motorPin,200);
     Serial.println(\"Motor has stopped Overloading\");
     digitalWrite(ledPinRed,LOW);
 }
 else
 {
 }
 }


