Stap 2: Verbind met MegaMoto
Eerste plaats moeten wij de MegaMoto draad. Voer de volgende handelingen uit:
-Sluit uw 12V levering aan de VLEERMUIS +/-terminals
-Sluit de bedieningssleutel aan de MOTA en MOTB terminals
-OPTIONELE Connect BAT + te Vin als u wilt aandrijven van de Arduino niet achter de computer, of laat de USB ingestopt.
-De MegaMoto-pinnen als volgt instellen:
-Enable = 13
-PWMA = 11
-PWMB = 3
-Sensor = A5
De linkerknop moet de bedieningssleutel uit te breiden, de rechterknop moet de bedieningssleutel intrekken. Als uw richtingen zijn omgekeerd, dan omkeren de MOTA en MOTB verbindingen.
Nu dat de knoppen zijn geconfigureerd kunnen we de LCD programma toevoegen aan de rest van het programma. We hebben vier nieuwe functies toegevoegd. Read_LCD_buttons(), updateTrip(), updateLCD() en printFeedback().
Read_LCD_buttons() is hetzelfde als het testprogramma, het leest de status van de knoppen van het LCD-scherm.
UpdateTrip() wordt gebruikt om te controleren van de resultaten van de omhoog en omlaag knoppen, en wijzigen van de limiet van de versterker om het afsluiten van de veiligheid op reis.
UpdateLCD() is de algemene LCD schrijven functie, die alle nieuwe informatie op het LCD-scherm schrijft.
PrintFeedback() schrijft de huidige stroomsterkte trekken naar het LCD-scherm. Dit staat los van de belangrijkste updateLCD()-functie, zodat we de huidige loting in real-time zien kunnen, eerder dan wachten voor het programma lus sequentieel doorzoeken.
Upload het programma om te testen is in de volgende stap die wij zal gaan over de verschillende delen van de code in detail.
Opmerking: De reis grenzen en de huidige feedback worden niet daadwerkelijk het aantal versterkers die zijn meegesleurd, ze zijn alleen de resultaten van de huidige sensor. U kunt een vergelijking in de waarde omzetten in versterkers voordat u naar het LCD-scherm schrijven.
/* Code to display the current amp draw of the actuator, and to cut power if it rises above a certain amount. Written by Progressive Automations July 16th, 2015 Hardware: - RobotPower MegaMoto control boards - LCD shield - Arduino Uno */ #include <LiquidCrystal.h> LiquidCrystal lcd(8, 9, 4, 5, 6, 7);// select the pins used on the LCD panel // define some values used by the panel and buttons #define btnRIGHT 1 #define btnUP 2 #define btnDOWN 3 #define btnLEFT 4 #define btnSELECT 5 #define btnNONE 0 int lcd_key = 0; int adc_key_in = 0;//value to read LCD button input const int btnup = 130; const int btndown = 306; const int btnleft = 479; const int btnright = 0; const int select = 720; const int none = 1023; const int threshold = 2;//Analog values for LCD buttons const int EnablePin = 13; const int PWMPinA = 11; const int PWMPinB = 3; // pins for Megamoto const int CPin1 = A5; // motor feedback int button = 0;//value to read LCD buttons int leftlatch = LOW; int rightlatch = LOW;//motor latches int hitLimits = 0; int hitLimitsmaxfront = 10; int hitLimitsmaxback = 5;//values to know if travel limits were reached long lastfeedbacktime = 0; int firstfeedbacktimedelay = 750; //first delay to ignore current spike int feedbacktimedelay = 50; //delay between feedback cycles long currentTimefeedback = 0; int debounceTime = 500; //amount to debounce long lastButtonpress = 0; // timer for debouncing long currentTimedebounce = 0; int CRaw = 0; // raw A/D value int maxAmps = 0; // trip limit long currentTimetrip = 0; long lastUpdate = 0; int updateTime = 100;//how fast can trip values update bool dontExtend = false; bool firstRun = true;//program logic char* title[] = {" PROGRESSIVE", " AUTOMATIONS"}; //Power on title void setup() { Serial.begin(9600); pinMode(EnablePin, OUTPUT); pinMode(PWMPinA, OUTPUT); pinMode(PWMPinB, OUTPUT);//Set motor outputs pinMode(CPin1, INPUT);//set feedback input lcd.begin(16, 2);// start the library lcd.setCursor(0, 0), lcd.print(title[0]), lcd.setCursor(0, 1), lcd.print(title[1]); //Print title delay(2000);//show title for 2 seconds currentTimetrip = millis(); currentTimedebounce = millis(); currentTimefeedback = 0;//Set initial times lcd.setCursor(0, 0); lcd.print("Amps Feedback"); // print the menu message updateLCD(); //write new values to the screen }//end setup void loop() { //Serial.println("Main loop"); updateTrip();//check buttons, update amperage latchButtons();//check buttons, see if we need to move moveMotor();//check latches, move motor in or out }//end main loop void updateLCD() { lcd.setCursor(0, 1); lcd.print("Amps: 0"); lcd.setCursor(8, 1); lcd.print("Trip: 0"); lcd.setCursor(13, 1); lcd.print(" ");//clear old value if (maxAmps > 99) lcd.setCursor(13, 1); else if (maxAmps > 9) lcd.setCursor(14, 1); else lcd.setCursor(15, 1); lcd.print(maxAmps);//write new value }//end updateLCD void latchButtons() { button = read_LCD_buttons(); if (button == btnLEFT && rightlatch != HIGH)//left is forwards { currentTimedebounce = millis() - lastButtonpress; if (currentTimedebounce > debounceTime && dontExtend == false)//once you've tripped dontExtend, ignore all forwards presses { leftlatch = !leftlatch; button = btnNONE; firstRun = true; lastButtonpress = millis(); return; }//end if }//end btnLEFT if (button == btnRIGHT && leftlatch != HIGH)//right is backwards { currentTimedebounce = millis() - lastButtonpress; if (currentTimedebounce > debounceTime) { rightlatch = !rightlatch; button = btnNONE; firstRun = true; lastButtonpress = millis(); return; }//end if }//end btnRIGHT }//end latchButtons int read_LCD_buttons() { adc_key_in = analogRead(A0); // read the value from the sensor delay(20); if (adc_key_in > none - threshold && adc_key_in < none + threshold) return btnNONE; if (adc_key_in > btnup - threshold && adc_key_in < btnup + threshold) return btnUP; if (adc_key_in > btndown - threshold && adc_key_in < btndown + threshold) return btnDOWN; if (adc_key_in > btnleft - threshold && adc_key_in < btnleft + threshold) return btnLEFT; if (adc_key_in > btnright - threshold && adc_key_in < btnright + threshold) return btnRIGHT; if (adc_key_in > select - threshold && adc_key_in < select + threshold) return btnSELECT; return btnNONE; // when all others fail, return none }//end readLCD void updateTrip() { if (button == btnUP) { currentTimetrip = millis() - lastUpdate; if (currentTimetrip > updateTime) { maxAmps = maxAmps + 1; if (maxAmps > 999) maxAmps = 0;//check for rollover lcd.setCursor(13, 1); lcd.print(" ");//clear old value if (maxAmps > 99) lcd.setCursor(13, 1); else if (maxAmps > 9) lcd.setCursor(14, 1); else lcd.setCursor(15, 1);//set cursor accordingly lcd.print(maxAmps);//write new value lastUpdate = millis(); }//end if }//end if btnUP else if (button == btnDOWN) { currentTimetrip = millis() - lastUpdate; if (currentTimetrip > updateTime) { maxAmps = maxAmps - 1; if (maxAmps < 0) maxAmps = 999;//check for rollover lcd.setCursor(13, 1); lcd.print(" ");//clear old value if (maxAmps > 99) lcd.setCursor(13, 1); else if (maxAmps > 9) lcd.setCursor(14, 1); else lcd.setCursor(15, 1);//set cursor accordingly lcd.print(maxAmps);//write new value lastUpdate = millis(); }//end if }//end else if btnDOWN }//end updateTrip void moveMotor() { if (leftlatch == HIGH) motorForward(255); //speed = 0-255 if (leftlatch == LOW) motorStop(); if (rightlatch == HIGH) motorBack(255); //speed = 0-255 if (rightlatch == LOW) motorStop(); }//end moveMotor void motorForward(int speeed) { while (dontExtend == false && leftlatch == HIGH) { //Serial.println("Moving forwards"); digitalWrite(EnablePin, HIGH); analogWrite(PWMPinA, speeed); analogWrite(PWMPinB, 0);//move motor if (firstRun == true) delay(firstfeedbacktimedelay); else delay(feedbacktimedelay); //small delay to get to speed getFeedback(); firstRun = false; latchButtons(); }//end while }//end motorForward void motorBack (int speeed) { while (rightlatch == HIGH) { //Serial.println("Moving Back"); digitalWrite(EnablePin, HIGH); analogWrite(PWMPinA, 0); analogWrite(PWMPinB, speeed);//move motor delay(3); //small delay to get to speed currentTimefeedback = millis() - lastfeedbacktime; if (firstRun == true && currentTimefeedback > firstfeedbacktimedelay) getFeedback(); //if it hasnt been long enough, do nothing if (firstRun == false && currentTimefeedback > feedbacktimedelay) getFeedback(); //if it hasnt been long enough, do nothing firstRun = false; latchButtons(); }//end while dontExtend = false; }//end motorBack void motorStop() { analogWrite(PWMPinA, 0); analogWrite(PWMPinB, 0); digitalWrite(EnablePin, LOW); firstRun = true;//once the motor has stopped, reenable firstRun to account for startup current spikes }//end stopMotor void getFeedback() { CRaw = analogRead(CPin1); if (CRaw == 0 && hitLimits < hitLimitsmaxback) hitLimits = hitLimits + 1; else if (CRaw == 0 && hitLimits < hitLimitsmaxfront) hitLimits = hitLimits + 1; else hitLimits = 0; if (hitLimits == hitLimitsmaxback) { rightlatch = LOW; }//end if if (hitLimits == hitLimitsmaxfront) { leftlatch = LOW; hitLimits = 0; }//end if if (CRaw > maxAmps) { dontExtend = true; leftlatch = LOW; }//end if printFeedback(); lastfeedbacktime = millis();//store previous time for receiving feedback }//end getFeedback void printFeedback() { lcd.setCursor(5, 1); lcd.print(" "); if (CRaw > 99) lcd.setCursor(5, 1); else if (CRaw > 9) lcd.setCursor(6, 1); else lcd.setCursor(7, 1); lcd.print(CRaw); }//end printFeedback