/* MLA Adjustment Motor Used hardware: ITEAD MBoard Motor Control created Nov 2018 by Uwe Gartmann HB9FZG */ const int pinSpeed = A0; // potentiometer for low speed adjustment, not used at the moment. const int pinBTN1 = A5; // button 1 of remote const int pinBTN2 = A4; // button 2 of remote const int pinBTN3 = A3; // button 3 of remote const int pinBTN4 = A2; // button 4 of remote const int pinCMDready = A1; // valid remote command received const int pinM1A = 7; // motor signal A const int pinM1B = 8; // motor signal B const int pinPWM1 = 10; // PWM for motor 1 const int motorHighSpeed = 255; // PWM for full speed // motor states const int motorStop = 0; const int motorHighCW = 1; const int motorHighCCW = 2; const int motorLowCW = 3; const int motorLowCCW = 4; // start values int motorLowSpeed = 100; // adjust value for reliable motor rotation int motorCurrentState = 99; // set a unknown state to be recognized in main loop int motorNewState = motorStop; void setup() { //set motor shield pins as output pinMode(pinM1A, OUTPUT); pinMode(pinM1B, OUTPUT); pinMode(pinPWM1, OUTPUT); //set remote receiver pins as input pinMode(pinBTN1,INPUT); pinMode(pinBTN2,INPUT); pinMode(pinBTN3,INPUT); pinMode(pinBTN4,INPUT); pinMode(pinCMDready, INPUT); //set potentiometer pin as input, not used in this project pinMode(pinSpeed, INPUT); //Onboard LED pinMode(LED_BUILTIN, OUTPUT); //Initialize serial and wait for port to open: //Serial.begin(9600); } void loop() { // remove comment marks for poti operation //motorLowSpeed = analogRead(pinSpeed) / 4; if (motorCurrentState != motorNewState) { switch (motorNewState) { case motorStop: motorCurrentState = motorNewState; motorOff(); break; case motorHighCW: motorCurrentState = motorNewState; motorCW(motorHighSpeed); break; case motorHighCCW: motorCurrentState = motorNewState; motorCCW(motorHighSpeed); break; case motorLowCW: motorCurrentState = motorNewState; motorCW(motorHighSpeed); delay(5); motorCW(motorLowSpeed); delay(70); motorOff(); delay(500); break; case motorLowCCW: motorCurrentState = motorNewState; motorCCW(motorHighSpeed); delay(5); motorCCW(motorLowSpeed); delay(70); motorOff(); delay(500); break; default: break; } } if (digitalRead(pinCMDready)) { if (digitalRead(pinBTN1)) { motorNewState = motorHighCW; } if (digitalRead(pinBTN2)) { motorNewState = motorHighCCW; } if (digitalRead(pinBTN3)) { motorNewState = motorLowCW; } if (digitalRead(pinBTN4)) { motorNewState = motorLowCCW; } } else { motorNewState = motorStop; } switch (motorCurrentState) { case motorLowCW: motorCW(motorLowSpeed); break; case motorLowCCW: motorCCW(motorLowSpeed); break; } } void motorCW(int speed) { digitalWrite(pinM1A, HIGH); digitalWrite(pinM1B, LOW); analogWrite(pinPWM1, speed); onLED(); } void motorCCW(int speed) { digitalWrite(pinM1A, LOW); digitalWrite(pinM1B, HIGH); analogWrite(pinPWM1, speed); onLED(); } void motorOff(void) { digitalWrite(pinM1A, LOW); digitalWrite(pinM1B, LOW); analogWrite(pinPWM1, 0); offLED(); } void toggleLED(void) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); } void onLED(void) { digitalWrite(LED_BUILTIN, HIGH); } void offLED(void) { digitalWrite(LED_BUILTIN, LOW); }