Blame | Last modification | View Log | RSS feed
/*433Mhz Reviever ConnectionsView from front__________________________| ||_A_G_G_V__________V_D_D_G_|| | | | | | | |^ ^ ^5V D2 GndMotorShield ConnectionsM1___G__M2_|o|o||o||o|o|^ ^ ^ ^| | | |+R -R -L +LR = Right motor, L = Left motor*/#include <VirtualWire.h>#include <AFMotor.h>AF_DCMotor motor1(1, MOTOR12_2KHZ); //RightAF_DCMotor motor2(2, MOTOR12_2KHZ); //Leftconst int rheIntPin = 0;const int ledPin = 13;const int rxPin = 2;const int rxBps = 2000;const int MRIGHT = 0;const int MLEFT = 1;const int MFORWARD = 1;const int MREVERSE = 0;int options = 0; // Optionsint mspd[2] = {0, 0}; //Speed of right trackvolatile byte rpmcount = 0;unsigned int rpm = 0;unsigned long timeold = 0;void setup(){Serial.begin(9600); // Debugging onlySerial.println("setup");pinMode(ledPin, OUTPUT);digitalWrite(ledPin, false);// Startup radio receivervw_set_rx_pin(rxPin);vw_setup(rxBps); // Bits per secvw_rx_start(); // Start the receiver PLL running// Hall effect intterupt setup//pinMode(rhePin, INPUT);attachInterrupt(rheIntPin, rpm_rhe, FALLING);// Hall effect debugging//mspd[MRIGHT] = 50;update_motors();}void loop(){uint8_t buf[VW_MAX_MESSAGE_LEN];uint8_t buflen = VW_MAX_MESSAGE_LEN;if (vw_get_message(buf, &buflen)) // Non-blocking{// Flash on msg get//digitalWrite(13, true);//digitalWrite(13, false);/*int i;Serial.print("Got: ");for (i = 0; i < buflen; i++){Serial.print(buf[i], HEX);Serial.print(" ");}Serial.println("");*/if (buf[0]) // Read the optionsoptions = buf[0] - 1;if (buf[1]) // Right speedmspd[MRIGHT] = buf[1] -1;if (buf[2]) // Left speedmspd[MLEFT] = buf[2] -1;Serial.print("options "); Serial.print(options, BIN);Serial.print(" r_spd "); Serial.print(mspd[MRIGHT], DEC);Serial.print(" l_spd "); Serial.print(mspd[MLEFT], DEC);Serial.println("");update_motors();}if (rpmcount >= 5) {rpm = 30*1000/(millis() - timeold)*rpmcount;timeold = millis();rpmcount = 0;Serial.println(rpm,DEC);}}void rpm_rhe() {rpmcount++;digitalWrite(13, true);digitalWrite(13, false);}void update_motors() {// Start the tracks rollingif (mspd[MRIGHT] > 0) {motor1.setSpeed(mspd[MRIGHT]);if (bitRead(options, MRIGHT))motor1.run(FORWARD);elsemotor1.run(BACKWARD);} elsemotor1.run(RELEASE);if (mspd[MLEFT] > 0) {motor2.setSpeed(mspd[MLEFT]);if (bitRead(options, MLEFT))motor2.run(FORWARD);elsemotor2.run(BACKWARD);} elsemotor2.run(RELEASE);}