Bulbaderp Game

From STEAMwiki
Jump to: navigation, search

This was a 1 day project by the Toronto Public Library's innovation digital design technicians and service specialists in innovation! http://www.torontopubliclibrary.ca/using-the-library/computer-services/innovation-spaces/

https://twitter.com/STEAM_Labs/status/805891557870202880

They started with one of our pokemon capture robot platforms.

they used Autodesk 123D Make to turn the bulbasaur 3D model into layers for laser cutting.

Code:

// This #include statement was automatically added by the Particle IDE.
#include "neopixel/neopixel.h"

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN A5
#define PIXEL_COUNT 62
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

// Servos
Servo servoArr[4]; //Create 4 servo objects

// Debug
bool debugOn = true;

int drivingint = 0;
int servo_1 = 0;
int servo_2 = 0;
int my_Score = 0;
bool ball_in = false;
int sensor_value = 0;

void setup() {
    // Drive motors
    pinMode(A0, OUTPUT);
    pinMode(A1, OUTPUT);
    pinMode(A2, OUTPUT);
    pinMode(A3, OUTPUT);
    pinMode(A4, INPUT);
    pinMode(A5, OUTPUT);
    Particle.function("goForwards", goForwards);
    Particle.function("goLeft", goLeft);
    Particle.function("goRight", goRight);
    Particle.function("goBack", goBack);
    Particle.function("goStop", goStop);


    // Servos
    servoArr[0].attach(D0);
    servoArr[1].attach(D1);
    servoArr[2].attach(D2);
    servoArr[3].attach(D3);
    Particle.function("servoControl", servoControl);
    
    // Motor Outputs
    pinMode(D4, OUTPUT);
    pinMode(D5, OUTPUT);
    pinMode(D6, OUTPUT);
    pinMode(D7, OUTPUT);
    Particle.function("motorControl", motorControl);
    strip.show(); // Initialize all pixels to 'off'
    
    if (debugOn) {
        Serial.begin(9600);
    }
    
}

void loop() {
   
    
    for (int i=0; i<PIXEL_COUNT; i++) {
        strip.setPixelColor(i+2, 0, 0, 1);
        strip.setPixelColor(i+1, 0, 0, 20);
        strip.setPixelColor(i, 0, 0, 120);
        strip.setPixelColor(i-1, 0, 0, 20);
        strip.setPixelColor(i-2, 0, 0, 1);
        strip.setPixelColor(i-3, 0, 0, 0);
        //delay(100);
        strip.show();
        if(i>6) {strip.setPixelColor(i, 0,  0, 0);strip.setPixelColor(i-1, 0,  0, 0);strip.setPixelColor(i-2, 0,  0, 0); }
    }
    
    drivingint = random(4);
    switch(drivingint) {
    case 1:
        goLeft(0);
        break;
    case 2:
        goRight(0);
        break;
    case 3:
        goForwards(0);
        break;
    case 4:
        goBack(0);
        break;
    default:
        goForwards(0);
        break;
  }
  delay(200);
  
  sensor_value = analogRead(A4);
  Serial.println(analogRead(A4));
  servo_1 = random(45);
  servo_2 = random(45);
  servoArr[1].write(servo_1);
  servoArr[0].write(servo_2);
  if(sensor_value < 400 && ball_in == false) {
      ball_in = true;
      ++my_Score;
      
      Particle.publish("Score", "0");
      for(int i=0; i<5; i++) {
      rainbow(5);
      }
      Particle.publish("ScoreOff", "0");
      
      Serial.print("Score: "); Serial.println(my_Score);
  }
  else if(sensor_value >= 400 && ball_in == true) {
      ball_in = false;
  }
}

void debug(String message) {
    // Usage:  debug(String(motorPins[0]));
    
    if (debugOn) {
        Serial.println(message);
    }
}

int goForwards (String command) {
    digitalWrite(A0, LOW);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, HIGH);
    digitalWrite(A3, LOW);
    return 1;
}

int goLeft (String command) {
    digitalWrite(A0, LOW);
    digitalWrite(A1, HIGH);
    digitalWrite(A2, LOW);
    digitalWrite(A3, HIGH);
    return 1;
}

int goRight (String command) {
    digitalWrite(A0, HIGH);
    digitalWrite(A1, LOW);
    digitalWrite(A2, HIGH);
    digitalWrite(A3, LOW);
    return 1;
}

int goBack (String command) {
    digitalWrite(A0, HIGH);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, HIGH);
    return 1;
}

int goStop (String command) {
    digitalWrite(A0, LOW);
    digitalWrite(A1, LOW);
    digitalWrite(A2, LOW);
    digitalWrite(A3, LOW);
    return 1;
}

int servoControl(String command)
{
   debug(command);
    // Parse the command into servo number and angle
    // Message format: "1-90" means turn servo 1 to 90 degrees
   int servoNumber = command.substring(0,1).toInt() -1;
   int servoPosition = command.substring(2).toInt();
   
   // Make sure it is in the right range
   // And set the position
   servoPosition = constrain( servoPosition, 0 , 360);

   // Set the servo
   servoArr[servoNumber].write( servoPosition );
  // done
   return 1;
}

int motorControl (String command) {
    debug(command);
    
    // Parse the command into servo number and angle
    // Message format: "1-F" means turn motor 1 forwards
    // F = Forwards
    // B = Backwards
    // S = Stop
    
    int motorNumber = command.substring(0,1).toInt() -1;
    String motorDirection = command.substring(2);
    int motorPins[2];
    if (motorNumber == 1) {
        motorPins[0] = D4;
        motorPins[1] = D5;
    } else {
        motorPins[0] = D6;
        motorPins[1] = D7;

    }
    debug(String(motorPins[0]));
    
    if (motorDirection == "F") {
        digitalWrite(motorPins[0], HIGH);
        digitalWrite(motorPins[1], LOW);
    } else if (motorDirection == "B" || motorDirection == "R") {
        digitalWrite(motorPins[0], LOW);
        digitalWrite(motorPins[1], HIGH);
    } else if (motorDirection == "S") {
        digitalWrite(motorPins[0], LOW);
        digitalWrite(motorPins[1], LOW);
    } 
   
   
    return 1;
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 75) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 75;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 75;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
   
}


}