TheBricktronics libraries come with a bunch of examples. Use them to make sure your hardware is working, and then expand them and make your own creations! We like the examples we have, but we’re always looking for more. Let us know if you have some examples you’d like to contribute.
All these examples are built into the Bricktronics libraries, so once you’ve downloaded and installed them, you can get to these examples from the Arduino IDE menu system.
For example, look in File->Examples->BricktronicsColor->ColorDebounce.
// Bricktronics Example: ColorButton// http://www.wayneandlayne.com/bricktronics// This example uses a LEGO NXT Color Sensor.//// Color sensor readings are taken repeatedly, and when they change,// they're debounces so the colors don't switch back and forth on a// transition. The debounced color is printed to the serial console.// This isn't necessarily the simplest or best way to debounce,// but it works for what we're using it for!//// Written in 2015 by Matthew Beckler and Adam Wolf for Wayne and Layne, LLC// To the extent possible under law, the author(s) have dedicated all// copyright and related and neighboring rights to this software to the// public domain worldwide. This software is distributed without any warranty.// You should have received a copy of the CC0 Public Domain Dedication along// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. // Include the Bricktronics ColorSensor library#include <BricktronicsColor.h>// This example can be run in three different ways. Pick one, and un-comment// the code lines corresponding to your chosen method. Comment-out the lines// for the other methods that you aren't using.// 1. With a Bricktronics Shield - Include these lines and be sure to// call BricktronicsShield::begin() in the setup() function below.// You also need to install the Adafruit MCP23017 library:// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library// Select the sensor port (SENSOR_3 or SENSOR_4) in the constructor below.// Use the jumpers to connect only pins 3-4 for the color sensor.//// Config 1 - arduino:avr:uno//#include <Wire.h>//#include <Adafruit_MCP23017.h>//#include <BricktronicsShield.h>//BricktronicsColor c(BricktronicsShield::SENSOR_3);// Config end// 2. With a Bricktronics Megashield - Include these lines but do not// call BricktronicsShield::begin() in the setup() function below.// Select the sensor port (SENSOR_1 through SENSOR_4) in the constructor below.// Use the jumpers to connect only pins 3-4 for the color sensor.//// Config 2 - arduino:avr:mega:cpu=atmega2560//#include <BricktronicsMegashield.h>//BricktronicsColor c(BricktronicsMegashield::SENSOR_3);// Config end// 3. With a Bricktronics Breakout board - No additional includes needed, just// update the pin assignments in the BricktronicsColor constructor below.//// For the color sensor, connect these pins on the Bricktronics Breakout board:// Pin 1 - Unused// Pin 2 - Connect to Ground// Pin 3 - Connect to Ground// Pin 4 - Connect to 5V// Pin 5 - Connect to any digital pin// Pin 6 - Connect to any analog input pin//// The BricktronicsColor() arguments are: clockPin, dataPin// clockPin is where the breakout board's pin 5 is connected// dataPin is where the breakout board's pin 6 is connected// This must be an analog pin.//// Config 3 - arduino:avr:uno//BricktronicsColor c(8, 16);// Config endvoid setup(){// Be sure to set your serial console to 115200 baud
Serial.begin(115200);// Only call this if you are using a Bricktronics Shield,// otherwise leave it commented-out.// Config 1 - arduino:avr:uno//BricktronicsShield::begin();// Config end// Initialize the color sensor connections
c.begin();}char stable =0;char lastColor = c.getColor();char newReading;void loop(){
stable =0;
newReading = c.getColor();if(newReading != lastColor){while(stable <10){
delay(10);if(c.getColor()== newReading){
stable +=1;}else{
newReading = c.getColor();
stable =0;}}if(newReading != lastColor){
c.printColor(newReading);
Serial.println();
lastColor = newReading;}}
delay(10);}
// Bricktronics Example: ColorButton
// http://www.wayneandlayne.com/bricktronics
// This example uses a LEGO NXT Color Sensor.
//
// Color sensor readings are taken repeatedly, and when they change,
// they're debounces so the colors don't switch back and forth on a
// transition. The debounced color is printed to the serial console.
// This isn't necessarily the simplest or best way to debounce,
// but it works for what we're using it for!
//
// Written in 2015 by Matthew Beckler and Adam Wolf for Wayne and Layne, LLC
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any warranty.
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
// Include the Bricktronics ColorSensor library
#include <BricktronicsColor.h>
// This example can be run in three different ways. Pick one, and un-comment
// the code lines corresponding to your chosen method. Comment-out the lines
// for the other methods that you aren't using.
// 1. With a Bricktronics Shield - Include these lines and be sure to
// call BricktronicsShield::begin() in the setup() function below.
// You also need to install the Adafruit MCP23017 library:
// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
// Select the sensor port (SENSOR_3 or SENSOR_4) in the constructor below.
// Use the jumpers to connect only pins 3-4 for the color sensor.
//
// Config 1 - arduino:avr:uno
//#include <Wire.h>
//#include <Adafruit_MCP23017.h>
//#include <BricktronicsShield.h>
//BricktronicsColor c(BricktronicsShield::SENSOR_3);
// Config end
// 2. With a Bricktronics Megashield - Include these lines but do not
// call BricktronicsShield::begin() in the setup() function below.
// Select the sensor port (SENSOR_1 through SENSOR_4) in the constructor below.
// Use the jumpers to connect only pins 3-4 for the color sensor.
//
// Config 2 - arduino:avr:mega:cpu=atmega2560
//#include <BricktronicsMegashield.h>
//BricktronicsColor c(BricktronicsMegashield::SENSOR_3);
// Config end
// 3. With a Bricktronics Breakout board - No additional includes needed, just
// update the pin assignments in the BricktronicsColor constructor below.
//
// For the color sensor, connect these pins on the Bricktronics Breakout board:
// Pin 1 - Unused
// Pin 2 - Connect to Ground
// Pin 3 - Connect to Ground
// Pin 4 - Connect to 5V
// Pin 5 - Connect to any digital pin
// Pin 6 - Connect to any analog input pin
//
// The BricktronicsColor() arguments are: clockPin, dataPin
// clockPin is where the breakout board's pin 5 is connected
// dataPin is where the breakout board's pin 6 is connected
// This must be an analog pin.
//
// Config 3 - arduino:avr:uno
//BricktronicsColor c(8, 16);
// Config end
void setup()
{
// Be sure to set your serial console to 115200 baud
Serial.begin(115200);
// Only call this if you are using a Bricktronics Shield,
// otherwise leave it commented-out.
// Config 1 - arduino:avr:uno
//BricktronicsShield::begin();
// Config end
// Initialize the color sensor connections
c.begin();
}
char stable = 0;
char lastColor = c.getColor();
char newReading;
void loop()
{
stable = 0;
newReading = c.getColor();
if (newReading != lastColor)
{
while (stable < 10)
{
delay(10);
if (c.getColor() == newReading)
{
stable += 1;
}
else
{
newReading = c.getColor();
stable = 0;
}
}
if (newReading != lastColor)
{
c.printColor(newReading);
Serial.println();
lastColor = newReading;
}
}
delay(10);
}
// Bricktronics Example: ColorLamp// http://www.wayneandlayne.com/bricktronics// This example uses a LEGO NXT Color Sensor.//// The Color sensor can also act like a red, green, or blue lamp.// This example switches between these three colors in turn.//// Written in 2015 by Matthew Beckler and Adam Wolf for Wayne and Layne, LLC// To the extent possible under law, the author(s) have dedicated all// copyright and related and neighboring rights to this software to the// public domain worldwide. This software is distributed without any warranty.// You should have received a copy of the CC0 Public Domain Dedication along// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. // Include the BricktronicsColor library#include <BricktronicsColor.h>// This example can be run in three different ways. Pick one, and un-comment// the code lines corresponding to your chosen method. Comment-out the lines// for the other methods that you aren't using.// 1. With a Bricktronics Shield - Include these lines and be sure to// call BricktronicsShield::begin() in the setup() function below.// You also need to install the Adafruit MCP23017 library:// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library// Select the sensor port for the BricktronicsColor (SENSOR_3 or SENSOR_4) below.// Use the jumpers to connect only pins 3-4 for the color sensor.//// Config 1 - arduino:avr:uno//#include <Wire.h>//#include <Adafruit_MCP23017.h>//#include <BricktronicsShield.h>//BricktronicsColor c(BricktronicsShield::SENSOR_3);// Config end// 2. With a Bricktronics Megashield - Include these lines but do not// call BricktronicsShield::begin() in the setup() function below.// Select the sensor port for the BricktronicsColor (SENSOR_1 through SENSOR_4) below.// Use the jumpers to connect only pins 3-4 for the color sensor.//// Config 2 - arduino:avr:mega:cpu=atmega2560//#include <BricktronicsMegashield.h>//BricktronicsColor c(BricktronicsMegashield::SENSOR_3);// Config end// 3. With a Bricktronics Breakout board - No additional includes needed, just// update the pin assignments in the BricktronicsColor constructor below.//// // For the color sensor, connect these pins on the Bricktronics Breakout board:// Pin 1 - Unused// Pin 2 - Connect to Ground// Pin 3 - Connect to Ground// Pin 4 - Connect to 5V// Pin 5 - Connect to any digital pin// Pin 6 - Connect to any analog input pin//// The BricktronicsColor() arguments are: clockPin, dataPin// clockPin is where the breakout board's pin 5 is connected// dataPin is where the breakout board's pin 6 is connected// This must be an analog pin.//// Config 3 - arduino:avr:uno//BricktronicsColor c(8, 16);// Config endvoid setup(){// Only call this if you are using a Bricktronics Shield,// otherwise leave it commented-out.// Config 1 - arduino:avr:uno//BricktronicsShield::begin();// Config end// Initialize the color sensor connections
c.begin();}void loop(){
c.begin(TYPE_COLORRED);
delay(500);
c.begin(TYPE_COLORBLUE);
delay(500);
c.begin(TYPE_COLORGREEN);
delay(500);
c.begin(TYPE_COLORNONE);
delay(500);}
// Bricktronics Example: ColorLamp
// http://www.wayneandlayne.com/bricktronics
// This example uses a LEGO NXT Color Sensor.
//
// The Color sensor can also act like a red, green, or blue lamp.
// This example switches between these three colors in turn.
//
// Written in 2015 by Matthew Beckler and Adam Wolf for Wayne and Layne, LLC
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any warranty.
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
// Include the BricktronicsColor library
#include <BricktronicsColor.h>
// This example can be run in three different ways. Pick one, and un-comment
// the code lines corresponding to your chosen method. Comment-out the lines
// for the other methods that you aren't using.
// 1. With a Bricktronics Shield - Include these lines and be sure to
// call BricktronicsShield::begin() in the setup() function below.
// You also need to install the Adafruit MCP23017 library:
// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
// Select the sensor port for the BricktronicsColor (SENSOR_3 or SENSOR_4) below.
// Use the jumpers to connect only pins 3-4 for the color sensor.
//
// Config 1 - arduino:avr:uno
//#include <Wire.h>
//#include <Adafruit_MCP23017.h>
//#include <BricktronicsShield.h>
//BricktronicsColor c(BricktronicsShield::SENSOR_3);
// Config end
// 2. With a Bricktronics Megashield - Include these lines but do not
// call BricktronicsShield::begin() in the setup() function below.
// Select the sensor port for the BricktronicsColor (SENSOR_1 through SENSOR_4) below.
// Use the jumpers to connect only pins 3-4 for the color sensor.
//
// Config 2 - arduino:avr:mega:cpu=atmega2560
//#include <BricktronicsMegashield.h>
//BricktronicsColor c(BricktronicsMegashield::SENSOR_3);
// Config end
// 3. With a Bricktronics Breakout board - No additional includes needed, just
// update the pin assignments in the BricktronicsColor constructor below.
//
// // For the color sensor, connect these pins on the Bricktronics Breakout board:
// Pin 1 - Unused
// Pin 2 - Connect to Ground
// Pin 3 - Connect to Ground
// Pin 4 - Connect to 5V
// Pin 5 - Connect to any digital pin
// Pin 6 - Connect to any analog input pin
//
// The BricktronicsColor() arguments are: clockPin, dataPin
// clockPin is where the breakout board's pin 5 is connected
// dataPin is where the breakout board's pin 6 is connected
// This must be an analog pin.
//
// Config 3 - arduino:avr:uno
//BricktronicsColor c(8, 16);
// Config end
void setup()
{
// Only call this if you are using a Bricktronics Shield,
// otherwise leave it commented-out.
// Config 1 - arduino:avr:uno
//BricktronicsShield::begin();
// Config end
// Initialize the color sensor connections
c.begin();
}
void loop()
{
c.begin(TYPE_COLORRED);
delay(500);
c.begin(TYPE_COLORBLUE);
delay(500);
c.begin(TYPE_COLORGREEN);
delay(500);
c.begin(TYPE_COLORNONE);
delay(500);
}
// Bricktronics Example: ColorButton// http://www.wayneandlayne.com/bricktronics// This example uses a LEGO NXT Pushbutton Sensor and Color Sensor.// // When the button is pressed, a single reading from the color sensor is taken// and converted into a color name and printed over the Serial Console.//// Written in 2015 by Matthew Beckler and Adam Wolf for Wayne and Layne, LLC// To the extent possible under law, the author(s) have dedicated all// copyright and related and neighboring rights to this software to the// public domain worldwide. This software is distributed without any warranty.// You should have received a copy of the CC0 Public Domain Dedication along// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. // Include the Bricktronics Button and ColorSensor libraries#include <BricktronicsButton.h>#include <BricktronicsColor.h>// This example can be run in three different ways. Pick one, and un-comment// the code lines corresponding to your chosen method. Comment-out the lines// for the other methods that you aren't using.// 1. With a Bricktronics Shield - Include these lines and be sure to// call BricktronicsShield::begin() in the setup() function below.// You also need to install the Adafruit MCP23017 library:// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library// Select the sensor ports for the Button (SENSOR_1 through SENSOR_4)// and ColorSensor (SENSOR_3 or SENSOR_4) in their constructors below.// If your chosen port has jumpers (ports 3 and 4), connect pins 2-3 and// 4-5 for the button, and connect only pins 3-4 for the color sensor.//// Config 1 - arduino:avr:uno//#include <Wire.h>//#include <Adafruit_MCP23017.h>//#include <BricktronicsShield.h>//BricktronicsButton b(BricktronicsShield::SENSOR_1);//BricktronicsColor c(BricktronicsShield::SENSOR_3);// Config end// 2. With a Bricktronics Megashield - Include these lines but do not// call BricktronicsShield::begin() in the setup() function below.// Select the sensor ports for the button (SENSOR_1 through SENSOR_4)// and color sensor (SENSOR_1 through SENSOR_4) in their constructors below.// If your chosen port has jumpers, connect pins 2-3 and 4-5 for the button,// and connect only pins 3-4 for the color sensor.//// Config 2 - arduino:avr:mega:cpu=atmega2560//#include <BricktronicsMegashield.h>//BricktronicsButton b(BricktronicsMegashield::SENSOR_1);//BricktronicsColor c(BricktronicsMegashield::SENSOR_3);// Config end// 3. With two Bricktronics Breakout boards - No additional includes needed, just// update the pin assignments in the button and color sensor constructors below.//// For the pushbutton, connect these pins on the Bricktronics Breakout board:// Pin 1 - Connect to any digital input pin// Pin 2 - Connect to Ground// Pin 3 - Connect to Ground// Pin 4 - Connect to 5V// Pin 5 - No connection// Pin 6 - No connection//// For the color sensor, connect these pins on the Bricktronics Breakout board:// Pin 1 - Unused// Pin 2 - Connect to Ground// Pin 3 - Connect to Ground// Pin 4 - Connect to 5V// Pin 5 - Connect to any digital pin// Pin 6 - Connect to any analog input pin//// The BricktronicsButton() argument is simply the pin where the button's// pin 1 is connected.//// The BricktronicsColor() arguments are: clockPin, dataPin// clockPin is where the breakout board's pin 5 is connected// dataPin is where the breakout board's pin 6 is connected// This must be an analog pin.//// Config 3 - arduino:avr:uno//BricktronicsButton b(7);//BricktronicsColor c(8, 16);// Config endvoid setup(){// Be sure to set your serial console to 115200 baud
Serial.begin(115200);// Only call this if you are using a Bricktronics Shield,// otherwise leave it commented-out.// Config 1 - arduino:avr:uno//BricktronicsShield::begin();// Config end// Initialize the button and color sensor connections
b.begin();
c.begin();}void loop(){// Wait until the button is pressedwhile(b.isReleased()){// Nothing to do here}// To get here, the button was pushed!
c.printColor(c.getColor());
Serial.println();// In order to debounce the button, we wait a little bit here
delay(100);// Wait until the button is releasedwhile(b.isPressed()){// Nothing to do here}// In order to debounce the button, we wait a little bit here
delay(100);}
// Bricktronics Example: ColorButton
// http://www.wayneandlayne.com/bricktronics
// This example uses a LEGO NXT Pushbutton Sensor and Color Sensor.
//
// When the button is pressed, a single reading from the color sensor is taken
// and converted into a color name and printed over the Serial Console.
//
// Written in 2015 by Matthew Beckler and Adam Wolf for Wayne and Layne, LLC
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any warranty.
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
// Include the Bricktronics Button and ColorSensor libraries
#include <BricktronicsButton.h>
#include <BricktronicsColor.h>
// This example can be run in three different ways. Pick one, and un-comment
// the code lines corresponding to your chosen method. Comment-out the lines
// for the other methods that you aren't using.
// 1. With a Bricktronics Shield - Include these lines and be sure to
// call BricktronicsShield::begin() in the setup() function below.
// You also need to install the Adafruit MCP23017 library:
// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
// Select the sensor ports for the Button (SENSOR_1 through SENSOR_4)
// and ColorSensor (SENSOR_3 or SENSOR_4) in their constructors below.
// If your chosen port has jumpers (ports 3 and 4), connect pins 2-3 and
// 4-5 for the button, and connect only pins 3-4 for the color sensor.
//
// Config 1 - arduino:avr:uno
//#include <Wire.h>
//#include <Adafruit_MCP23017.h>
//#include <BricktronicsShield.h>
//BricktronicsButton b(BricktronicsShield::SENSOR_1);
//BricktronicsColor c(BricktronicsShield::SENSOR_3);
// Config end
// 2. With a Bricktronics Megashield - Include these lines but do not
// call BricktronicsShield::begin() in the setup() function below.
// Select the sensor ports for the button (SENSOR_1 through SENSOR_4)
// and color sensor (SENSOR_1 through SENSOR_4) in their constructors below.
// If your chosen port has jumpers, connect pins 2-3 and 4-5 for the button,
// and connect only pins 3-4 for the color sensor.
//
// Config 2 - arduino:avr:mega:cpu=atmega2560
//#include <BricktronicsMegashield.h>
//BricktronicsButton b(BricktronicsMegashield::SENSOR_1);
//BricktronicsColor c(BricktronicsMegashield::SENSOR_3);
// Config end
// 3. With two Bricktronics Breakout boards - No additional includes needed, just
// update the pin assignments in the button and color sensor constructors below.
//
// For the pushbutton, connect these pins on the Bricktronics Breakout board:
// Pin 1 - Connect to any digital input pin
// Pin 2 - Connect to Ground
// Pin 3 - Connect to Ground
// Pin 4 - Connect to 5V
// Pin 5 - No connection
// Pin 6 - No connection
//
// For the color sensor, connect these pins on the Bricktronics Breakout board:
// Pin 1 - Unused
// Pin 2 - Connect to Ground
// Pin 3 - Connect to Ground
// Pin 4 - Connect to 5V
// Pin 5 - Connect to any digital pin
// Pin 6 - Connect to any analog input pin
//
// The BricktronicsButton() argument is simply the pin where the button's
// pin 1 is connected.
//
// The BricktronicsColor() arguments are: clockPin, dataPin
// clockPin is where the breakout board's pin 5 is connected
// dataPin is where the breakout board's pin 6 is connected
// This must be an analog pin.
//
// Config 3 - arduino:avr:uno
//BricktronicsButton b(7);
//BricktronicsColor c(8, 16);
// Config end
void setup()
{
// Be sure to set your serial console to 115200 baud
Serial.begin(115200);
// Only call this if you are using a Bricktronics Shield,
// otherwise leave it commented-out.
// Config 1 - arduino:avr:uno
//BricktronicsShield::begin();
// Config end
// Initialize the button and color sensor connections
b.begin();
c.begin();
}
void loop()
{
// Wait until the button is pressed
while (b.isReleased())
{
// Nothing to do here
}
// To get here, the button was pushed!
c.printColor(c.getColor());
Serial.println();
// In order to debounce the button, we wait a little bit here
delay(100);
// Wait until the button is released
while (b.isPressed())
{
// Nothing to do here
}
// In order to debounce the button, we wait a little bit here
delay(100);
}
// Bricktronics Example: MotorSingle// http://www.wayneandlayne.com/bricktronics// This example uses a LEGO NXT Motor.//// This example starts the motor at an intermediate speed,// then speeds it up to full speed, and does the same in reverse.//// This example uses a motor, so it needs more power than a USB port can give.// We really don't recommend running motors off of USB ports (they will be// slow and sluggish, other things won't quite work right, things can get hot)// it's just not a good idea. Use an external power supply that provides// between 7.2 and 9 volts DC, and can provide at least 600 mA per motor// (1 amp preferably). Two options that work really well are a 9V wall adapter// or a 6xAA battery pack (2.1mm plug, center positive).//// Written in 2015 by Matthew Beckler and Adam Wolf for Wayne and Layne, LLC// To the extent possible under law, the author(s) have dedicated all// copyright and related and neighboring rights to this software to the// public domain worldwide. This software is distributed without any warranty.// You should have received a copy of the CC0 Public Domain Dedication along// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. // Include the Bricktronics Motor library and helper libraries// Helper libraries can be downloaded from:// https://www.pjrc.com/teensy/td_libs_Encoder.html// https://github.com/br3ttb/Arduino-PID-Library/// Be sure to rename unzipped folder PID_v1#include <Encoder.h>#include <PID_v1.h>#include <BricktronicsMotor.h>// This example can be run in three different ways. Pick one, and un-comment// the code lines corresponding to your chosen method. Comment-out the lines// for the other methods that you aren't using.// 1. With a Bricktronics Shield - Include these lines and be sure to// call BricktronicsShield::begin() in the setup() function below.// You also need to install the Adafruit MCP23017 library:// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library// Select the motor port (MOTOR_1 or MOTOR_2) in the constructor below.//// Config 1 - arduino:avr:uno//#include <Wire.h>//#include <Adafruit_MCP23017.h>//#include <BricktronicsShield.h>//BricktronicsMotor m(BricktronicsShield::MOTOR_1);// Config end// 2. With a Bricktronics Megashield - Include these lines below but do not// call BricktronicsShield::begin() in the setup() function below. Select the// desired motor port (MOTOR_1 through MOTOR_6) in the constructor below.//// Config 2 - arduino:avr:mega:cpu=atmega2560//#include <BricktronicsMegashield.h>//BricktronicsMotor m(BricktronicsMegashield::MOTOR_1);// Config end// 3. With a Bricktronics Motor Driver - No additional #includes needed,// just update the five pin assignments in the constructor below.// The arguments are: enPin, dirPin, pwmPin, encoderPin1, encoderPin2// There are a few considerations for pin assignments:// A. pwmPin needs to be a pin with PWM capabilities (that is, it supports analogWrite)// Uno: pins 3, 5, 6, 9, 10, and 11// Mega 2560: pins 2 - 13 and 44 - 46// B. There are three ways to connect the encoder pins (labeled T1/T2 on the board).// ** Best performance: Both signals are connected to true interrupt pins (listed below).// ** Good performance: The FIRST signal (T1) is connected to an interrupt pin, the second signal is a regular pin. This is the mode used for the Bricktronics Shield/Megashield. For this mode it is CRITICAL that the true interrupt pin is used for T1 and not T2.// ** Low performance: Both signals are connected to non-interrupt pins.// Regardless of which performance mode used, you MUST list the pin T1 before T2 in// the constructor, otherwise the encoder will be connected backwards and the// PID algorithm will get all confused and freak out.// Location of true interrupt pins:// Uno: pins 2 and 3// Mega 2560: pins 2, 3, 21, 20, 19, and 18//// Config 3 - arduino:avr:uno//BricktronicsMotor m(3, 4, 10, 2, 5);// Config endvoid setup(){// Be sure to set your serial console to 115200 baud
Serial.begin(115200);// Only call this line if you are using a Bricktronics Shield,// otherwise leave it commented-out.// Config 1 - arduino:avr:uno//BricktronicsShield::begin();// Config end// Initialize the motor connections
m.begin();}void loop(){
Serial.println("Going forward.");
m.setFixedDrive(75);
delay(1000);
m.setFixedDrive(255);
delay(1000);
Serial.println("Going in reverse.");
m.setFixedDrive(-75);
delay(1000);
m.setFixedDrive(-255);
delay(1000);}
// Bricktronics Example: MotorSingle
// http://www.wayneandlayne.com/bricktronics
// This example uses a LEGO NXT Motor.
//
// This example starts the motor at an intermediate speed,
// then speeds it up to full speed, and does the same in reverse.
//
// This example uses a motor, so it needs more power than a USB port can give.
// We really don't recommend running motors off of USB ports (they will be
// slow and sluggish, other things won't quite work right, things can get hot)
// it's just not a good idea. Use an external power supply that provides
// between 7.2 and 9 volts DC, and can provide at least 600 mA per motor
// (1 amp preferably). Two options that work really well are a 9V wall adapter
// or a 6xAA battery pack (2.1mm plug, center positive).
//
// Written in 2015 by Matthew Beckler and Adam Wolf for Wayne and Layne, LLC
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any warranty.
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
// Include the Bricktronics Motor library and helper libraries
// Helper libraries can be downloaded from:
// https://www.pjrc.com/teensy/td_libs_Encoder.html
// https://github.com/br3ttb/Arduino-PID-Library/
// Be sure to rename unzipped folder PID_v1
#include <Encoder.h>
#include <PID_v1.h>
#include <BricktronicsMotor.h>
// This example can be run in three different ways. Pick one, and un-comment
// the code lines corresponding to your chosen method. Comment-out the lines
// for the other methods that you aren't using.
// 1. With a Bricktronics Shield - Include these lines and be sure to
// call BricktronicsShield::begin() in the setup() function below.
// You also need to install the Adafruit MCP23017 library:
// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
// Select the motor port (MOTOR_1 or MOTOR_2) in the constructor below.
//
// Config 1 - arduino:avr:uno
//#include <Wire.h>
//#include <Adafruit_MCP23017.h>
//#include <BricktronicsShield.h>
//BricktronicsMotor m(BricktronicsShield::MOTOR_1);
// Config end
// 2. With a Bricktronics Megashield - Include these lines below but do not
// call BricktronicsShield::begin() in the setup() function below. Select the
// desired motor port (MOTOR_1 through MOTOR_6) in the constructor below.
//
// Config 2 - arduino:avr:mega:cpu=atmega2560
//#include <BricktronicsMegashield.h>
//BricktronicsMotor m(BricktronicsMegashield::MOTOR_1);
// Config end
// 3. With a Bricktronics Motor Driver - No additional #includes needed,
// just update the five pin assignments in the constructor below.
// The arguments are: enPin, dirPin, pwmPin, encoderPin1, encoderPin2
// There are a few considerations for pin assignments:
// A. pwmPin needs to be a pin with PWM capabilities (that is, it supports analogWrite)
// Uno: pins 3, 5, 6, 9, 10, and 11
// Mega 2560: pins 2 - 13 and 44 - 46
// B. There are three ways to connect the encoder pins (labeled T1/T2 on the board).
// ** Best performance: Both signals are connected to true interrupt pins (listed below).
// ** Good performance: The FIRST signal (T1) is connected to an interrupt pin, the second signal is a regular pin. This is the mode used for the Bricktronics Shield/Megashield. For this mode it is CRITICAL that the true interrupt pin is used for T1 and not T2.
// ** Low performance: Both signals are connected to non-interrupt pins.
// Regardless of which performance mode used, you MUST list the pin T1 before T2 in
// the constructor, otherwise the encoder will be connected backwards and the
// PID algorithm will get all confused and freak out.
// Location of true interrupt pins:
// Uno: pins 2 and 3
// Mega 2560: pins 2, 3, 21, 20, 19, and 18
//
// Config 3 - arduino:avr:uno
//BricktronicsMotor m(3, 4, 10, 2, 5);
// Config end
void setup()
{
// Be sure to set your serial console to 115200 baud
Serial.begin(115200);
// Only call this line if you are using a Bricktronics Shield,
// otherwise leave it commented-out.
// Config 1 - arduino:avr:uno
//BricktronicsShield::begin();
// Config end
// Initialize the motor connections
m.begin();
}
void loop()
{
Serial.println("Going forward.");
m.setFixedDrive(75);
delay(1000);
m.setFixedDrive(255);
delay(1000);
Serial.println("Going in reverse.");
m.setFixedDrive(-75);
delay(1000);
m.setFixedDrive(-255);
delay(1000);
}
// Bricktronics Example: MotorButton// http://www.wayneandlayne.com/bricktronics// This example uses a LEGO NXT Motor and a Pushbutton sensor.//// This example starts the motor at full speed, then waits for// the button to be pressed and released, then reverses direction.// It does this forever! (or until you turn off the power,// unplug stuff, or reprogram the Arduino.)//// This example uses a motor, so it needs more power than a USB port can give.// We really don't recommend running motors off of USB ports (they will be// slow and sluggish, other things won't quite work right, things can get hot)// it's just not a good idea. Use an external power supply that provides// between 7.2 and 9 volts DC, and can provide at least 600 mA per motor// (1 amp preferably). Two options that work really well are a 9V wall adapter// or a 6xAA battery pack (2.1mm plug, center positive).//// Written in 2015 by Matthew Beckler and Adam Wolf for Wayne and Layne, LLC// To the extent possible under law, the author(s) have dedicated all// copyright and related and neighboring rights to this software to the// public domain worldwide. This software is distributed without any warranty.// You should have received a copy of the CC0 Public Domain Dedication along// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. // Include the Bricktronics Motor library and helper libraries// Helper libraries can be downloaded from:// https://www.pjrc.com/teensy/td_libs_Encoder.html// https://github.com/br3ttb/Arduino-PID-Library/// Be sure to rename unzipped folder PID_v1#include <Encoder.h>#include <PID_v1.h>#include <BricktronicsMotor.h>// Include the Bricktronics Button libraries#include <BricktronicsButton.h>// This example can be run in three different ways. Pick one, and un-comment// the code lines corresponding to your chosen method. Comment-out the lines// for the other methods that you aren't using.// 1. With a Bricktronics Shield - Include these lines and be sure to// call BricktronicsShield::begin() in the setup() function below.// You also need to install the Adafruit MCP23017 library:// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library// Select the motor port (MOTOR_1 or MOTOR_2) and sensor port// (SENSOR_1 through SENSOR_4) in the constructors below.// If your chosen sensor port has jumpers (ports 3 and 4), connect pins 2-3 and 4-5.//// Config 1 - arduino:avr:uno//#include <Wire.h>//#include <Adafruit_MCP23017.h>//#include <BricktronicsShield.h>//BricktronicsMotor m(BricktronicsShield::MOTOR_1);//BricktronicsButton b(BricktronicsShield::SENSOR_1);// Config end// 2. With a Bricktronics Megashield - Include these lines but do not// call BricktronicsShield::begin() in the setup() function below.// Select the desired motor port (MOTOR_1 through MOTOR_6) and sensor port// (SENSOR_1 through SENSOR_4) in the constructors below.// Connect pins 2-3 and 4-5 on the chosen sensor port.//// Config 2 - arduino:avr:mega:cpu=atmega2560//#include <BricktronicsMegashield.h>//BricktronicsMotor m(BricktronicsMegashield::MOTOR_1);//BricktronicsButton b(BricktronicsMegashield::SENSOR_1);// Config end// 3. With a Bricktronics Motor Driver - No additional #includes needed,// just update the five pin assignments in the constructor below.// The arguments are: enPin, dirPin, pwmPin, encoderPin1, encoderPin2// There are a few considerations for pin assignments:// A. pwmPin needs to be a pin with PWM capabilities (supports analogWrite)// Uno: pins 3, 5, 6, 9, 10, and 11// Mega 2560: pins 2 to 13 and 44 to 46// B. There are three ways to connect the encoder pins (labeled T1/T2 on the board).// ** Best performance: Both signals are connected to true interrupt pins (listed below).// ** Good performance: The FIRST signal (T1) is connected to an interrupt pin, the second signa is a regular pin. This is the mode used for the Bricktronics Shield/Megashield. For this mode it is CRITICAL that the true interrupt pin is used for T1 and not T2.// ** Low performance: Both signals are connected to non-interrupt pins.// Regardless of which performance mode used, you MUST list the pin T1 before T2 in// the constructor, otherwise the encoder will be connected backwards and the// PID algorithm will get all confused and freak out.// Location of true interrupt pins:// Uno: pins 2 and 3// Mega 2560: pins 2, 3, 21, 20, 19, and 18//// The BricktronicsButton() argument is simply the pin the button is connected to,// that is, wherever pin 1 of the Breakout board is connected (also connect the grounds).// No worries about PWM or interrupt pins here.//// Config 3 - arduino:avr:uno//BricktronicsMotor m(3, 4, 10, 2, 5);//BricktronicsButton b(7);// Config endvoid setup(){// Only call this if you are using a Bricktronics Shield,// otherwise leave it commented-out.// Config 1 - arduino:avr:uno//BricktronicsShield::begin();// Config end// Initialize the motor and button connections
m.begin();
b.begin();}int theSpeed =150;void loop(){
m.setFixedDrive(theSpeed);// Wait until the button is pressedwhile(b.isReleased()){// Nothing to do here}// To get here, the button was pushed!// While the button is held down, turn on the dynamic brake
m.brake();// In order to debounce the button, we wait a little bit here
delay(100);// Wait until the button is releasedwhile(b.isPressed()){// Nothing to do here}// In order to debounce the button, we wait a little bit here
delay(100);// Reverse direction
theSpeed *=-1;}
// Bricktronics Example: MotorButton
// http://www.wayneandlayne.com/bricktronics
// This example uses a LEGO NXT Motor and a Pushbutton sensor.
//
// This example starts the motor at full speed, then waits for
// the button to be pressed and released, then reverses direction.
// It does this forever! (or until you turn off the power,
// unplug stuff, or reprogram the Arduino.)
//
// This example uses a motor, so it needs more power than a USB port can give.
// We really don't recommend running motors off of USB ports (they will be
// slow and sluggish, other things won't quite work right, things can get hot)
// it's just not a good idea. Use an external power supply that provides
// between 7.2 and 9 volts DC, and can provide at least 600 mA per motor
// (1 amp preferably). Two options that work really well are a 9V wall adapter
// or a 6xAA battery pack (2.1mm plug, center positive).
//
// Written in 2015 by Matthew Beckler and Adam Wolf for Wayne and Layne, LLC
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any warranty.
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
// Include the Bricktronics Motor library and helper libraries
// Helper libraries can be downloaded from:
// https://www.pjrc.com/teensy/td_libs_Encoder.html
// https://github.com/br3ttb/Arduino-PID-Library/
// Be sure to rename unzipped folder PID_v1
#include <Encoder.h>
#include <PID_v1.h>
#include <BricktronicsMotor.h>
// Include the Bricktronics Button libraries
#include <BricktronicsButton.h>
// This example can be run in three different ways. Pick one, and un-comment
// the code lines corresponding to your chosen method. Comment-out the lines
// for the other methods that you aren't using.
// 1. With a Bricktronics Shield - Include these lines and be sure to
// call BricktronicsShield::begin() in the setup() function below.
// You also need to install the Adafruit MCP23017 library:
// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
// Select the motor port (MOTOR_1 or MOTOR_2) and sensor port
// (SENSOR_1 through SENSOR_4) in the constructors below.
// If your chosen sensor port has jumpers (ports 3 and 4), connect pins 2-3 and 4-5.
//
// Config 1 - arduino:avr:uno
//#include <Wire.h>
//#include <Adafruit_MCP23017.h>
//#include <BricktronicsShield.h>
//BricktronicsMotor m(BricktronicsShield::MOTOR_1);
//BricktronicsButton b(BricktronicsShield::SENSOR_1);
// Config end
// 2. With a Bricktronics Megashield - Include these lines but do not
// call BricktronicsShield::begin() in the setup() function below.
// Select the desired motor port (MOTOR_1 through MOTOR_6) and sensor port
// (SENSOR_1 through SENSOR_4) in the constructors below.
// Connect pins 2-3 and 4-5 on the chosen sensor port.
//
// Config 2 - arduino:avr:mega:cpu=atmega2560
//#include <BricktronicsMegashield.h>
//BricktronicsMotor m(BricktronicsMegashield::MOTOR_1);
//BricktronicsButton b(BricktronicsMegashield::SENSOR_1);
// Config end
// 3. With a Bricktronics Motor Driver - No additional #includes needed,
// just update the five pin assignments in the constructor below.
// The arguments are: enPin, dirPin, pwmPin, encoderPin1, encoderPin2
// There are a few considerations for pin assignments:
// A. pwmPin needs to be a pin with PWM capabilities (supports analogWrite)
// Uno: pins 3, 5, 6, 9, 10, and 11
// Mega 2560: pins 2 to 13 and 44 to 46
// B. There are three ways to connect the encoder pins (labeled T1/T2 on the board).
// ** Best performance: Both signals are connected to true interrupt pins (listed below).
// ** Good performance: The FIRST signal (T1) is connected to an interrupt pin, the second signa is a regular pin. This is the mode used for the Bricktronics Shield/Megashield. For this mode it is CRITICAL that the true interrupt pin is used for T1 and not T2.
// ** Low performance: Both signals are connected to non-interrupt pins.
// Regardless of which performance mode used, you MUST list the pin T1 before T2 in
// the constructor, otherwise the encoder will be connected backwards and the
// PID algorithm will get all confused and freak out.
// Location of true interrupt pins:
// Uno: pins 2 and 3
// Mega 2560: pins 2, 3, 21, 20, 19, and 18
//
// The BricktronicsButton() argument is simply the pin the button is connected to,
// that is, wherever pin 1 of the Breakout board is connected (also connect the grounds).
// No worries about PWM or interrupt pins here.
//
// Config 3 - arduino:avr:uno
//BricktronicsMotor m(3, 4, 10, 2, 5);
//BricktronicsButton b(7);
// Config end
void setup()
{
// Only call this if you are using a Bricktronics Shield,
// otherwise leave it commented-out.
// Config 1 - arduino:avr:uno
//BricktronicsShield::begin();
// Config end
// Initialize the motor and button connections
m.begin();
b.begin();
}
int theSpeed = 150;
void loop()
{
m.setFixedDrive(theSpeed);
// Wait until the button is pressed
while (b.isReleased())
{
// Nothing to do here
}
// To get here, the button was pushed!
// While the button is held down, turn on the dynamic brake
m.brake();
// In order to debounce the button, we wait a little bit here
delay(100);
// Wait until the button is released
while (b.isPressed())
{
// Nothing to do here
}
// In order to debounce the button, we wait a little bit here
delay(100);
// Reverse direction
theSpeed *= -1;
}
// Bricktronics Example: UltrasonicSerial// http://www.wayneandlayne.com/bricktronics// This example uses a LEGO NXT Ultrasonic Sensor.//// Ultrasonic readings are taken every 100 milliseconds, and // printed out over the serial console. Be sure to set your serial// console to 115200 baud. The ultrasonic sensor reports the distance// to an obstruction in front of the sensor (in centimeters). It has// a range of 2.5 meters, a resolution of 1 cm, and reported accuracy// of +/- 3 cm. 255 is sometimes reported as an error state.//// This example uses an Ultrasonic Sensor, so it needs more voltage// than a USB port usually gives. Use an external power supply that// provides between 7.2 and 9 volts DC. Two options that work really// well are a 9V wall adapter or a 6xAA battery pack (2.1mm plug).//// Written in 2015 by Matthew Beckler and Adam Wolf for Wayne and Layne, LLC// To the extent possible under law, the author(s) have dedicated all// copyright and related and neighboring rights to this software to the// public domain worldwide. This software is distributed without any warranty.// You should have received a copy of the CC0 Public Domain Dedication along// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. // Include the BricktronicsUltrasonic library#include <BricktronicsUltrasonic.h>// This example can be run in three different ways. Pick one, and un-comment// the code lines corresponding to your chosen method. Comment-out the lines// for the other methods that you aren't using.// 1. With a Bricktronics Shield - Include these lines and be sure to// call BricktronicsShield::begin() in the setup() function below.// You also need to install the Adafruit MCP23017 library:// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library// Select the sensor port for the sensor (SENSOR_3 and SENSOR_4) below.// Use the jumpers to connect pins 1-2 and 4-5 for the ultrasonic sensor.//// Config 1 - arduino:avr:uno//#include <Wire.h>//#include <Adafruit_MCP23017.h>//#include <BricktronicsShield.h>//BricktronicsUltrasonic u(BricktronicsShield::SENSOR_4);// Config end// 2. With a Bricktronics Megashield - Include these lines but do not// call BricktronicsShield::begin() in the setup() function below.// Select the sensor port for the sensor (SENSOR_1 through SENSOR_4) below.// Use the jumpers to connect pins 1-2 and 4-5 for the ultrasonic sensor.//// Config 2 - arduino:avr:mega:cpu=atmega2560//#include <BricktronicsMegashield.h>//BricktronicsUltrasonic u(BricktronicsMegashield::SENSOR_4);// Config end// 3. With a Bricktronics Breakout board - No additional includes needed, just// update the pin assignments in the Ultrasonic constructor below.//// Connect these pins on the Bricktronics Breakout board:// Pin 1 - Connect to an external power supply between 7.2 and 9 volts DC// Pin 2 - Connect to Ground// Pin 3 - Connect to Ground// Pin 4 - Connect to 5V// Pin 5 - Connect to any digital pin (sclPin)// Pin 6 - Connect to any digital pin (sdaPin)//// The BricktronicsUltrasonic() arguments are:// sclPin (pin 5), sdaPin (pin 6)//// Config 3 - arduino:avr:uno//BricktronicsUltrasonic u(8, 12);// Config endvoid setup(){// Be sure to set your serial console to 115200 baud
Serial.begin(115200);// Only call this if you are using a Bricktronics Shield,// otherwise leave it commented-out.// Config 1 - arduino:avr:uno//BricktronicsShield::begin();// Config end// Initialize the ultrasonic sensor connections
u.begin();}void loop(){
Serial.println(u.getDistance());
delay(100);}
// Bricktronics Example: UltrasonicSerial
// http://www.wayneandlayne.com/bricktronics
// This example uses a LEGO NXT Ultrasonic Sensor.
//
// Ultrasonic readings are taken every 100 milliseconds, and
// printed out over the serial console. Be sure to set your serial
// console to 115200 baud. The ultrasonic sensor reports the distance
// to an obstruction in front of the sensor (in centimeters). It has
// a range of 2.5 meters, a resolution of 1 cm, and reported accuracy
// of +/- 3 cm. 255 is sometimes reported as an error state.
//
// This example uses an Ultrasonic Sensor, so it needs more voltage
// than a USB port usually gives. Use an external power supply that
// provides between 7.2 and 9 volts DC. Two options that work really
// well are a 9V wall adapter or a 6xAA battery pack (2.1mm plug).
//
// Written in 2015 by Matthew Beckler and Adam Wolf for Wayne and Layne, LLC
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any warranty.
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
// Include the BricktronicsUltrasonic library
#include <BricktronicsUltrasonic.h>
// This example can be run in three different ways. Pick one, and un-comment
// the code lines corresponding to your chosen method. Comment-out the lines
// for the other methods that you aren't using.
// 1. With a Bricktronics Shield - Include these lines and be sure to
// call BricktronicsShield::begin() in the setup() function below.
// You also need to install the Adafruit MCP23017 library:
// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
// Select the sensor port for the sensor (SENSOR_3 and SENSOR_4) below.
// Use the jumpers to connect pins 1-2 and 4-5 for the ultrasonic sensor.
//
// Config 1 - arduino:avr:uno
//#include <Wire.h>
//#include <Adafruit_MCP23017.h>
//#include <BricktronicsShield.h>
//BricktronicsUltrasonic u(BricktronicsShield::SENSOR_4);
// Config end
// 2. With a Bricktronics Megashield - Include these lines but do not
// call BricktronicsShield::begin() in the setup() function below.
// Select the sensor port for the sensor (SENSOR_1 through SENSOR_4) below.
// Use the jumpers to connect pins 1-2 and 4-5 for the ultrasonic sensor.
//
// Config 2 - arduino:avr:mega:cpu=atmega2560
//#include <BricktronicsMegashield.h>
//BricktronicsUltrasonic u(BricktronicsMegashield::SENSOR_4);
// Config end
// 3. With a Bricktronics Breakout board - No additional includes needed, just
// update the pin assignments in the Ultrasonic constructor below.
//
// Connect these pins on the Bricktronics Breakout board:
// Pin 1 - Connect to an external power supply between 7.2 and 9 volts DC
// Pin 2 - Connect to Ground
// Pin 3 - Connect to Ground
// Pin 4 - Connect to 5V
// Pin 5 - Connect to any digital pin (sclPin)
// Pin 6 - Connect to any digital pin (sdaPin)
//
// The BricktronicsUltrasonic() arguments are:
// sclPin (pin 5), sdaPin (pin 6)
//
// Config 3 - arduino:avr:uno
//BricktronicsUltrasonic u(8, 12);
// Config end
void setup()
{
// Be sure to set your serial console to 115200 baud
Serial.begin(115200);
// Only call this if you are using a Bricktronics Shield,
// otherwise leave it commented-out.
// Config 1 - arduino:avr:uno
//BricktronicsShield::begin();
// Config end
// Initialize the ultrasonic sensor connections
u.begin();
}
void loop()
{
Serial.println(u.getDistance());
delay(100);
}