const int controlPin1 = 2; const int controlPin2 = 3; const int enablePin = 6;//green wire on the H bridge6 const int directionSwitchPin = 13; const int onOffSwitchStateSwitchPin = 12; const int potPin = A1; //this is the potentiometer pin const int led = 5; const int redled = 7; int temp = A0; int onOffSwitchState = 0; int previousOnOffSwitchState = 0; int directionSwitchState = 0; int previousDirectionSwitchState = 0; int motorEnabled = 0; int motorSpeed = 0; int motorDirection = 0; void setup() { pinMode(controlPin1, OUTPUT); pinMode(controlPin2, OUTPUT); pinMode(enablePin, OUTPUT); pinMode(directionSwitchPin,INPUT); pinMode(onOffSwitchStateSwitchPin,INPUT); pinMode(led, INPUT); pinMode(redled, OUTPUT); pinMode(temp, INPUT); digitalWrite(enablePin,LOW);//The motor then intializes at OFF Serial.begin(9600); } void loop() { int temp = analogRead(A0); temp = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125); Serial.print("Temperature: "); Serial.print(temp); Serial.println(" C"); if (temp>40) { Serial.println("Fan Auto Shutdown"); digitalWrite(controlPin1, LOW); digitalWrite(controlPin2, LOW); } else if (temp<20) { Serial.println("Fan Auto Shutdown"); digitalWrite(controlPin1, LOW); digitalWrite(controlPin2, LOW); } else if(digitalRead(led) == HIGH){ Motor_Pot(); MotorControl(); digitalWrite(redled, LOW); }else{ digitalWrite(redled, HIGH); } previousDirectionSwitchState = directionSwitchState; previousOnOffSwitchState = onOffSwitchState; } void Motor_Pot() { onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin); delay(1); directionSwitchState = digitalRead(directionSwitchPin); motorSpeed = analogRead(potPin)/4; if(onOffSwitchState != previousOnOffSwitchState){ if(onOffSwitchState == HIGH){ motorEnabled = !motorEnabled; } } } void MotorControl() { if(directionSwitchState != previousDirectionSwitchState){ if(directionSwitchState == HIGH){ motorDirection = !motorDirection;} }if(motorDirection == 1){// if the direction is 1, turn left. digitalWrite(controlPin1, HIGH); digitalWrite(controlPin2, LOW); } else{//if the direction is 0, turn right. digitalWrite(controlPin1, LOW); digitalWrite(controlPin2, HIGH); } if(motorEnabled == 1){ analogWrite(enablePin, motorSpeed); } else{ analogWrite(enablePin, 0); //if the motor is turned off ser EN to low } }