Close

The Arduino code for "Bored No More"

A project log for Bored No More

Interactive Science Fair Board Using Arduino, big buttons, servos, and motors create excitement while interacting with science.

JazzmynJazzmyn 08/20/2014 at 23:460 Comments

The Arduino code for "Bored No More" 

#include <PWMServo.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Adafruit_NeoPixel.h>

//neopixels
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(120, PIN, NEO_GRB + NEO_KHZ800);

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// 513 stepper motor on pin 1
Adafruit_StepperMotor *myMotor = AFMS.getStepper(513, 1);
Adafruit_DCMotor *myMotorDCSpin = AFMS.getMotor(3);
Adafruit_DCMotor *myMotorDCCan = AFMS.getMotor(4);

const int buttonPin1 = 0; // the number of the red pushbutton pin
const int buttonPin2 = 1; // the number of the white pushbutton pin
const int buttonPin3 = 2; // the number of the blue pushbutton pin
const int buttonPin4 = 3; // the number of the green pushbutton pin
const int buttonPin5 = 4; // the number of the yellow pushbutton pin

const int ledPin1 = 5; // the number of the red LED pin
const int ledPin2 = 7; // the number of the white LED pin
const int ledPin3 = 8; // the number of the blue LED pin
const int ledPin4 = 11; // the number of the green LED pin
const int ledPin5 = 12; // the number of the yellow LED pin

int button1State = 0; // variable for reading the red pushbutton status
int button2State = 0; // variable for reading the white pushbutton status
int button3State = 0; // variable for reading the blue pushbutton status
int button4State = 0; // variable for reading the green pushbutton status
int button5State = 0; // variable for reading the yellow pushbutton status

PWMServo myservoDoor; // create servo object to control the door servo
PWMServo myservoArrow; // create servo object to control the arrow servo
int posArrow = 100; // variable to store the arrow servo position

void setup()
{
myservoArrow.attach(SERVO_PIN_A); // servo on pin 9 to the servo object
myservoDoor.attach(SERVO_PIN_B); // servo on pin 10 to the servo object

AFMS.begin(); // create with the default frequency 1.6KHz
myMotor->setSpeed(5); // 5 rpm

strip.begin(); // start
strip.show(); // Initialize all pixels to 'off'
for(uint16_t i=0; i<95; i++)
{
strip.setPixelColor(i, 204, 0, 102);
strip.show();
delay(15);
}

// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);

// turns on LEDs in all the buttons
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, HIGH);

myMotorDCSpin->setSpeed(150);
myMotorDCSpin->run(FORWARD);
myMotorDCSpin->run(RELEASE);

myMotorDCCan->setSpeed(150);
myMotorDCCan->run(FORWARD);
myMotorDCCan->run(RELEASE);
}

void loop()
{
sweep(); // sweep arrow
}

void sweep() // sweep arrow
{ // and check for button press
for(posArrow = 0; posArrow < 100; posArrow += 1) // goes from 0 to 180 degrees
{
Button1();
Button2();
Button3();
Button4();
Button5();
myservoArrow.write(posArrow); // tell servo to go to'posArrow'
delay(15); // servo to reach the position
}
for(posArrow = 100; posArrow>=1; posArrow -= 1) // goes from 180 to 0 degrees
{
Button1();
Button2();
Button3();
Button4();
Button5();
myservoArrow.write(posArrow); // tell servo to go to'posArrow'
delay(15); // servo to reach the position
}
}

void Button1()
{
button1State = digitalRead(buttonPin1); // read the state of buttons
if (button1State == HIGH) // check if button is pressed
{
myservoDoor.write(3); // close door

//turn off neopixels for Problem
strip.setPixelColor(96, 0, 0, 0);
strip.setPixelColor(97, 0, 0, 0);
strip.setPixelColor(98, 0, 0, 0);
strip.setPixelColor(99, 0, 0, 0);
//turn off neopixels for Hypothesis
strip.setPixelColor(100, 0, 0, 0);
strip.setPixelColor(101, 0, 0, 0);
strip.setPixelColor(102, 0, 0, 0);
strip.setPixelColor(103, 0, 0, 0);
strip.show(); // send signal to neopixels
}
else
{
myservoDoor.write(91); // open door
//turn on RED neopixels for Problem
strip.setPixelColor(96, 255, 0, 0);
strip.setPixelColor(97, 255, 0, 0);
strip.setPixelColor(98, 255, 0, 0);
strip.setPixelColor(99, 255, 0, 0);
//turn on RED neopixels for Hypothesis
strip.setPixelColor(100, 255, 0, 0);
strip.setPixelColor(101, 255, 0, 0);
strip.setPixelColor(102, 255, 0, 0);;
strip.setPixelColor(103, 255, 0, 0);
strip.show(); // send signal to neopixels
myMotor->step(86, FORWARD, SINGLE); // spins the wheel 86 steps
myMotor->release(); // releases the stepper motor
delay(5000); // wait
myservoDoor.write(3); // close door
}
}

void Button2()
{
button2State = digitalRead(buttonPin2); // read the state of buttons
if (button2State == HIGH) // check if button is pressed
{
//turn off neopixels for Research
strip.setPixelColor(116, 0, 0, 0);
strip.setPixelColor(117, 0, 0, 0);
strip.setPixelColor(118, 0, 0, 0);
strip.setPixelColor(119, 0, 0, 0);
strip.show(); // send signal to neopixels
}
else
{
//turn on WHITE neopixels for Research
strip.setPixelColor(116, 255, 229, 204);
strip.setPixelColor(117, 255, 229, 204);
strip.setPixelColor(118, 255, 229, 204);
strip.setPixelColor(119, 255, 229, 204);
strip.show(); // send signal to neopixels
myMotor->step(86, FORWARD, SINGLE); // spins the wheel 86 steps
myMotor->release(); // releases the stepper motor
delay(5000); // wait
}
}
void Button3()
{
button3State = digitalRead(buttonPin3); // read the state of buttons
if (button3State == HIGH) // check if button is pressed
{
//turn off neopixels for Build
strip.setPixelColor(112, 0, 0, 0);
strip.setPixelColor(113, 0, 0, 0);
strip.setPixelColor(114, 0, 0, 0);
strip.setPixelColor(115, 0, 0, 0);
strip.show(); // send signal to neopixels


}
else
{
//turn on BLUE neopixels for Build
strip.setPixelColor(112, 0, 0, 255);
strip.setPixelColor(113, 0, 0, 255);
strip.setPixelColor(114, 0, 0, 255);
strip.setPixelColor(115, 0, 0, 255);
strip.show(); // send signal to neopixels
myMotor->step(86, FORWARD, SINGLE); // spins the wheel 86 steps
myMotor->release(); // releases the stepper motor

myMotorDCSpin->run(FORWARD);
myMotorDCSpin->setSpeed(150);
delay(2000);
myMotorDCSpin->run(RELEASE);

delay(5000); // wait


}
}
void Button4()
{
button4State = digitalRead(buttonPin4); // read the state of buttons
if (button4State == HIGH) // check if button is pressed
{
//turn off neopixels for Results
strip.setPixelColor(108, 0, 0, 0);
strip.setPixelColor(109, 0, 0, 0);
strip.setPixelColor(110, 0, 0, 0);
strip.setPixelColor(111, 0, 0, 0);
strip.show(); // send signal to neopixels
}
else
{
//turn on GREEN neopixels for Results
strip.setPixelColor(108, 0, 255, 0);
strip.setPixelColor(109, 0, 255, 0);
strip.setPixelColor(110, 0, 255, 0);
strip.setPixelColor(111, 0, 255, 0);
strip.show(); // send signal to neopixels
myMotor->step(86, FORWARD, SINGLE); // spins the wheel 86 steps
myMotor->release(); // releases the stepper motor
delay(5000); // wait
}
}void Button5()
{
button5State = digitalRead(buttonPin5); // read the state of buttons
if (button5State == HIGH) // check if button is pressed
{
//turn off neopixels for Conclusion
strip.setPixelColor(104, 0, 0, 0);
strip.setPixelColor(105, 0, 0, 0);
strip.setPixelColor(106, 0, 0, 0);
strip.setPixelColor(107, 0, 0, 0);
strip.show(); // send signal to neopixels
}
else
{
//turn on Yellow neopixels for Conclusion
strip.setPixelColor(104, 255, 255, 0);
strip.setPixelColor(105, 255, 255, 0);
strip.setPixelColor(106, 255, 255, 0);
strip.setPixelColor(107, 255, 255, 0);
strip.show(); // send signal to neopixels
myMotor->step(86, FORWARD, SINGLE); // spins the wheel 86 steps
myMotor->release(); // releases the stepper motor


myMotorDCCan->run(BACKWARD);
myMotorDCCan->setSpeed(180);
delay(500);
myMotorDCCan->run(RELEASE);
myMotorDCCan->setSpeed(50);
delay(3000);
myMotorDCCan->run(RELEASE);
}
}

Discussions