Subversion Repositories group.electronics

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
121 pfowler 1
 
2
/* 
3
 433Mhz Reviever Connections
4
     View from front
5
  __________________________
6
 |                          |
7
 |_A_G_G_V__________V_D_D_G_|
8
   | | | |          | | | |
9
                    ^ ^   ^
10
                   5V D2  Gnd 
11
 
12
 MotorShield Connections
13
 
14
    M1___G__M2_
15
   |o|o||o||o|o|
16
    ^ ^     ^ ^
17
    | |     | |
18
   +R -R   -L +L
19
 R = Right motor, L = Left motor
20
*/
21
 
22
#include <VirtualWire.h>
23
#include <AFMotor.h>
24
 
25
AF_DCMotor motor1(1, MOTOR12_2KHZ);  //Right
26
AF_DCMotor motor2(2, MOTOR12_2KHZ);  //Left
27
 
28
const int  rxPin    = 2;
29
const int  rxBps    = 2000;
30
 
31
const int  MRIGHT   = 0;
32
const int  MLEFT    = 1;
33
 
34
const int  MFORWARD = 1;
35
const int  MREVERSE = 0;
36
 
37
void setup()
38
{
39
    Serial.begin(9600);	// Debugging only
40
    Serial.println("setup");
41
 
42
    vw_set_rx_pin(rxPin);
43
    vw_setup(rxBps);	 // Bits per sec
44
 
45
    vw_rx_start();       // Start the receiver PLL running
46
 
47
    // Start motors idle
48
    motor1.setSpeed(0);
49
    motor2.setSpeed(0);
50
 
51
    motor1.run(RELEASE);
52
    motor2.run(RELEASE);    
53
}
54
 
55
void loop()
56
{
57
    uint8_t buf[VW_MAX_MESSAGE_LEN];
58
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
59
 
60
    if (vw_get_message(buf, &buflen)) // Non-blocking
61
    {
62
        digitalWrite(13, true); // Flash a light to show received good message
63
        digitalWrite(13, false);
64
	/*
65
        int i;
66
        Serial.print("Got: ");
67
	for (i = 0; i < buflen; i++)
68
	{
69
	    Serial.print(buf[i], HEX);
70
	    Serial.print(" ");
71
	}
72
	Serial.println("");
73
        */
74
 
75
        int mspd[2] = {0, 0};    //Speed of right track
76
        int options = 0;         // Options
77
 
78
        if (buf[0])      // Read the options
79
          options = buf[0] - 1;
80
 
81
       if (buf[1])      // Right speed
82
          mspd[MRIGHT] = buf[1] -1;     
83
 
84
       if (buf[2])      // Left speed
85
          mspd[MLEFT] = buf[2] -1;
86
 
87
 
88
       Serial.print("options "); Serial.print(options, BIN);
89
       Serial.print(" r_spd "); Serial.print(mspd[MRIGHT], DEC);
90
       Serial.print(" l_spd "); Serial.print(mspd[MLEFT], DEC);
91
       Serial.println("");
92
 
93
 
94
       // Start the tracks rolling
95
       if (mspd[MRIGHT] > 0) {
96
         motor1.setSpeed(mspd[MRIGHT]);
97
         if (bitRead(options, MRIGHT))
98
           motor1.run(FORWARD);
99
         else
100
           motor1.run(BACKWARD);
101
       } else
102
         motor1.run(RELEASE);
103
 
104
       if (mspd[MLEFT] > 0) {
105
         motor2.setSpeed(mspd[MLEFT]);
106
         if (bitRead(options, MLEFT))
107
           motor2.run(FORWARD);
108
         else
109
           motor2.run(BACKWARD);
110
       } else
111
         motor2.run(RELEASE);        
112
    }
113
}