Step 6: Assemble Toy Circuitry

Picture of Assemble Toy Circuitry
The arduino which goes into the toy will send values depending on whether the flex sensors and the accelerometer are giving high or low values.  The flex sensors read high if at least one of them is high.  If both the flex sensors and the accelerometer are low, transmit 0; if the flex sensors only are high, transmit 1; if the accelerometer only is high, transmit 2; if both components are high, transmit 3.  This significantly simplifies the processing which must be done on the receiving arduino.

Now, there's a bit of a pickle here.  The arduino usually transmits a value of '0'. However, for reasons we have not yet determined, the arduino sometimes transmits everything as a ASCII character that is 48 above whatever you desired. We got around this difficulty by just having the receiving arduino test for both possible values, but we'll be sure to update this if we figure out why we get funny values sometimes. It helps to make sure you use the Serial.write() command, not the Serial.print() or Serial.println() commands when you are hoping to actually transmit data across the radios. Also, at this point, as you start plugging things in and putting code on the Arduinos, you'll want to avoid uploading code to an Arduino that has anything plugged into it's TX or RX pins (pins 0 & 1).

We used three 1.5V batteries in series to power the arduino (three AAs).  The arduino performs some internal power regulation, so you can power the arduino off of 4.5V or 6V.

Solder the ground leads of the two flex sensors and the arduino together, leaving the end of one lead free to plug into the board.  Do the same with the three power leads.  Plug the ground leads into one of the ground pins on the arduino and the power leads into the 5V power pin.  Plug the power lead of the XBee into the 3.3V pin and the ground lead of the XBee into another ground pin, then plug the RX/DIN lead into the TX pin of the arduino.  (The XBee is receiving from the arduino, hence the input of the XBee being connected to the output of the arduino.)  Finally, plug the ground lead of the batter pack into the final ground pin and the power lead of the battery pack into the Vin pin.  

The toy circuitry is now assembled!  Now all you need to do is program the arduino.  We used the below code to control our toy.

// Values to be read
int accelVal = 0;
int bendVal1 = 0;
int bendVal2 = 0;
int accelRef;
int bendRef1;
int bendRef2;
boolean accelOn = false;
boolean bendOn = false;
int sendVal = 0;

void setup() {
  // This code runs once, at the beginning
  Serial.begin(9600); // Initialize serial monitor

  // Get reference values: these allow us to calibrate the values we send for any variation in component behavior
  accelRef = analogRead(A3); 
  bendRef1 = analogRead(A4);
  bendRef2 = analogRead(A5);

void loop() {
  // This code runs continuously
  // Get values
  accelVal = analogRead(A3);
  bendVal1 = analogRead(A4);
  bendVal2 = analogRead(A5);
  // Check if accelerometer is on
  if ((accelVal - accelRef) > (accelRef / 3)){ // This is an arbitrary reference that we found worked well
    accelOn = true;
  } else {
    accelOn = false;
  // Check if bend sensors are on
  if ((bendVal1 < (3 * bendRef1 / 4)) || (bendVal2 < (3 * bendRef2 / 4))){ // This is an arbitrary reference
    bendOn = true;
  } else {
    bendOn = false;

  // Determine the correct value to transmit based on the sensors
  if (accelOn == false) {
    if (bendOn == false) {
      sendVal = 0;
    } else {
      sendVal = 1;
  } else {
    if (bendOn == false) {
      sendVal = 2;
    } else {
      sendVal = 3;
  // Transmit the value