Customizing Your Robot

Start 

Now that your robot is fully constructed the possibilities for customization are endless! In this tutorial we'll show you how to use your RGB lights and buzzer modules to turn your robot into a car!

Forward and Reverse Lights in Action!

Parts

All Parts
All Parts
RGB LED Module
Cables
Buzzer Module
1x1 Wrapper
2x1 Wrapper
PartQuantity
RGB LED Module 2
Cables 3
Buzzer Module 1
1x1 Wrapper 1
2x1 Wrapper 1

Adding the Taillights 

Begin by grabbing 2 cables and 2 RGB LED modules.

  1. Plug one side of one cable into the IN side of one RGB module and the other into any Digital pin.

  2. Take the other cable and plug it into the OUT side of the first RGB and the other end of the cable into the IN side of the second RGB.

  3. Take your buzzer module and one more cable. Plug one side into the module and the other into any Digital pin.

Use a smaller wrapper to attach the buzzer to the front of the robot. Use tape for the RGB LED modules

Parts for adding LEDs
Parts for adding LEDs
Take both cables
Unwrap them
Plug one into D6
The other into the IN side of an LED
A new cable into the OUT side
And in the IN side of the other LED
And in the IN side of the other LED
We will mount them here
Like this
With tape
Sides are taped in place
Parts to add the buzzer
Take a wrapper screw
Place into wrapper
Then onto chassis
Tighten down
Take buzzer module
Place into wrapper
Place into wrapper
Take remaining cable
Unwrap it
Plug one side into the buzzer socket
The other into D5
Ready for upload

Upload

Upload the following sketch. The example below uses:

  • The left motor as MOTOR1
  • The right motor as MOTOR2
  • The left encoder in D2
  • The right encoder in D3
  • RGB modules in D6
  • Buzzer in D4
Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "WiFiEsp.h"
#include "SoftwareSerial.h"
#include "Grove_I2C_Motor_Driver.h"
#include <ChainableLED.h>

// default I2C address is 0x0f
#define I2C_ADDRESS 0x0f
// Amount of chained LEDs
#define NUM_LEDS  2
//Change here if you're using a different socket
#define buzzerSocket 4

// Create Serial communication Object
SoftwareSerial Serial1(8, 9); // RX, TX

//Wifi server object
WiFiEspServer server(80);

// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);

//Create led object
ChainableLED leds(5, 6, NUM_LEDS);

//Wifi settings
char ssid[] = "My WiFi Robot";    // the name of your access point
char pass[] = "password";        // the password for your access point
int status = WL_IDLE_STATUS;     // the Wifi radio's status

// CHANGE THIS TO YOUR OFFSET VALUE
int offset = 3;

// variable for power -> from -100 to 100 (negative numbers are backwards)
int power = 70;

//Increase for larger turns. Decrease for smaller turns
int turnAmount = 180;

// Variable for running the moving code
bool moving = false;
bool buzzerSounds = false;
int buzzerState = LOW;

// Variables to time and stop moving
int interval;
unsigned long startTime;
int buzzerInterval = 50;
unsigned long buzzerStartTime;

// change these values to match your motor orientation
unsigned char left = MOTOR2;
unsigned char right = MOTOR1;

void setup()
{
  // Start Serial Communication with computer and WiFi module
  Serial.begin(9600);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);

  // initialize LEDs
  leds.init();

  // initialize buzzer
  pinMode(buzzerSocket, OUTPUT);

  // start motor driver
  Motor.begin(I2C_ADDRESS);
  // correct pwm cycles
  Motor.frequence(F_490Hz);

  // Print out debugging messages
  Serial.print("Attempting to start AP ");
  Serial.println(ssid);

  // start access point
  status = WiFi.beginAP(ssid);

  // start the web server on port 80
  server.begin();
  IPAddress ip = WiFi.localIP();
  Serial.println("Server started");
  Serial.print("To see this page in action, connect to ");
  Serial.print(ssid);
  Serial.print(" and open a browser to http://");
  Serial.println(ip);
  Serial.println();

  leds.setColorRGB(0, 128, 128, 0);
  leds.setColorRGB(1, 128, 128, 0);
}

void loop()
{

  // Wait for a device to connect to the WiFi
  WiFiEspClient client = server.available();  // listen for incoming clients

  // if you get a client,
  if (client) {
    // print a message out the serial port
    Serial.println("New client");
    // initialize the circular buffer
    buf.init();

    // loop while the client's connected
    while (client.connected()) {
      // if there's bytes to read from the client,
      if (client.available()) {
        // read a byte, then
        char c = client.read();
        // push it to the ring buffer
        buf.push(c);

        String request = client.readStringUntil('\r');
        client.flush();

        // Match the request
        // if the string "/fwd" is found,
        //then call the moveDistance() function
        if (request.indexOf("/fwd") != -1) {
          //moveDistance(35, power);
          leds.setColorRGB(0, 0, 255, 0);
          leds.setColorRGB(1, 0, 255, 0);
          moveTime(2, power);
        }
        // if the string "/bwd" is found,
        //then call the moveDistance function
        if (request.indexOf("/bwd") != -1) {
          //moveDistance(35, -power);
          leds.setColorRGB(0, 255, 0, 0);
          leds.setColorRGB(1, 255, 0, 0);
          moveTime(2, -power);
          buzzerSounds = true;
          buzzerStartTime = millis();
        }
        // if the string "/left" is found,
        //then call the turn() function
        if (request.indexOf("/left") != -1) {
          turn('l');
        }
        // if the string "/right" is found,
        // then call the turn() function
        if (request.indexOf("/right") != -1) {
          turn('r');
        }
        //Check to see if the buzzer should be active
        checkBuzzer();

        // Return the response
        client.print(
          "HTTP/1.1 200 OK\r\n"
          "Content-Type: text/html\r\n");
        //Check to see if the buzzer should be active
        checkBuzzer();

        client.println();

        //Check to see if the buzzer should be active
        checkBuzzer();

        // CSS to style the on/off buttons
        client.print(F(
                       "<!DOCTYPE HTML>\r\n"
                       "<html>\r\n"
                       "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\r\n"
                       "<link rel=\"icon\" href=\"data:,\">\r\n"

                       // change the font and size of each button here
                       "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\r\n"
                       ".button { background-color: #ff6666; border: black; color: black; padding: 16px 40px;\r\n"
                       "text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}\r\n"
                       ".button:hover {background: #eee;}\r\n"

                       // change the background color of each button here
                       ".forward {background-color: #ff6666;}\r\n"
                       ".backward {background-color: #aaff80;}\r\n"
                       ".left {background-color: #ff9900;}\r\n"
                       ".right {background-color: #ffff66;}</style></head>\r\n"

                       // change the heading of the web server here
                       "<h2>My WiFi Robot Controls</h2>\r\n"));

        //Check to see if the buzzer should be active
        checkBuzzer();

        // change the function and label of the buttons here
        client.print(F(
                       "<button class=\"button forward\" onClick=location.href='/fwd'>forward</button>\r\n"
                       "<p><button class=\"button left\" onClick=location.href='/left'>left</button>\r\n"
                       "<button class=\"button right\" onClick=location.href='/right'>right</button></p>\r\n"
                       "<button class=\"button backward\" onClick=location.href='/bwd'>backward</button>\r\n"));

        //Check to see if the buzzer should be active
        checkBuzzer();

        client.println("</body></html>");

        //Check to see if the buzzer should be active
        checkBuzzer();

        break;
      }

    }
    //Check to see if the buzzer should be active
    checkBuzzer();

    // close the connection
    client.stop();
    Serial.println("Client disconnected");
    //Check to see if the buzzer should be active
    checkBuzzer();
  }

  //Run if moving
  if (moving) {
    //Checks the interval timing
    if (millis() - startTime > interval) {
      //Stop the motors
      Motor.stop(left);
      Motor.stop(right);
      //Change moving variable
      moving = false;
      //Check to see if the buzzer should be active
      buzzerState = HIGH;
      checkBuzzer();
      buzzerSounds = false;
      leds.setColorRGB(0, 128, 128, 0);
      leds.setColorRGB(1, 128, 128, 0);
    }
  }
}


// function to move robot forward or backward
void moveTime(int seconds, int motorPower) {
  //Set the starting time
  startTime = millis();
  //Set this true to run the move code
  moving = true;
  //Set the time to run
  interval = seconds * 1000;

  //Set correct motor polarity
  int leftPower = motorPower;
  int rightPower = -motorPower;

  //Set motor speed
  Motor.speed(left, leftPower - offset);
  Motor.speed(right, rightPower - offset);
}


// function to turn robot left or right
void turn(char Dir) {
  //Set this true to run the move code
  moving = true;
  //Set the time to run
  interval = turnAmount;

  //Switch case for the two directions
  switch (Dir) {
    // if the direction (Dir) is left, turn on the right motor
    case 'l':
      Motor.speed(right, - power - offset);
      break;

    // if the direction is right, turn on the left motor
    case 'r':
      Motor.speed(left,  power - offset);
      break;
  }
}

void checkBuzzer() {
  if (buzzerSounds) {
    if (millis() - buzzerStartTime > buzzerInterval) {
      buzzerStartTime = millis();
      // if the buzzer is off turn it on and vice-versa:
      if (buzzerState == LOW) {
        buzzerState = HIGH;
      } else {
        buzzerState = LOW;
      }
      // set the state of the buzzer
      digitalWrite(buzzerSocket, buzzerState);
    }
  }
}

Observe

Open the Serial Monitor and make sure your motor driver is connected to your battery. Your WiFi module should be initialized and should create an access point. Follow the link displayed (http://[ your WiFi module's IP address]) to your web server to see your controls. Disconnect your robot from the computer and insert the 9v battery into the barrel jack on the Seeeduino. Using any smart device (a phone or computer) connect to your robot by selecting My WiFi Robot in your WiFi settings. Open an Internet browser and navigate to your web server.

When your robot drives forward the RGB lights should turn green. When your robot reverses the RGB lights should turn red and the buzzer should make a sound similar to a truck reversing.

Lights are yellow when not moving
Lights are yellow when not moving
Green when going forward
Red for reverse

Experiment

Change the color of your lights or make your little robot car play a song when it reverses! You can add more sensors and buttons to customize your robot to do all sorts of things.