Funduino Funbot – Assembly and Programming
FunduinoPublished on
Vom Zusammenbau bis zum selbstfahrenden Roboter – in elf Lektionen
Choose a microcontroller
Pins, schematic, and sketch in the steps change with your selection. It remains saved.
What makes the Funbot special
Assemble the Funbot
Power supply
The Funbot modules at a glance
Lesson 1: The flashing headlights
Lesson 1: The flashing headlights
int modul_LED = A1;
void setup()
{
pinMode(modul_LED, OUTPUT);
}
void loop()
{
digitalWrite(modul_LED, HIGH); // Headlight on
delay(1000); // wait one second
digitalWrite(modul_LED, LOW); // Headlight off
delay(1000); // wait one second
}Lesson 2: Reading the two push buttons
Lesson 2: Read both buttons
int modul_TasterL = A2;
int modul_TasterR = A3;
int modul_LED = A1;
int TasterL_status = 0;
int TasterR_status = 0;
void setup()
{
pinMode(modul_TasterL, INPUT);
pinMode(modul_TasterR, INPUT); // auch der rechte Taster ist ein Eingang
pinMode(modul_LED, OUTPUT);
}
void loop()
{
// Linker Taster: Scheinwerfer fuer 2 Sekunden
TasterL_status = digitalRead(modul_TasterL);
if (TasterL_status == 1)
{
digitalWrite(modul_LED, HIGH);
delay(2000);
digitalWrite(modul_LED, LOW);
}
// Rechter Taster: Scheinwerfer fuer 4 Sekunden
TasterR_status = digitalRead(modul_TasterR);
if (TasterR_status == 1)
{
digitalWrite(modul_LED, HIGH);
delay(4000);
digitalWrite(modul_LED, LOW);
}
}Lesson 3: Tones with the Speaker
Lesson 3: Sound and light alternating
int modul_speaker = A0;
int modul_led = A1;
void setup()
{
pinMode(modul_led, OUTPUT);
// Fuer den Lautsprecher ist kein pinMode noetig:
// der tone-Befehl kuemmert sich selbst darum.
}
void loop()
{
tone(modul_speaker, 100); // Ton mit 100 Hertz ausgeben
digitalWrite(modul_led, HIGH); // Scheinwerfer an
delay(1000);
noTone(modul_speaker); // Ton abschalten
digitalWrite(modul_led, LOW); // Scheinwerfer aus
delay(1000);
}Lesson 4: The drive motors
Lesson 4: Let the left motor turn
int MSL = 9; // Motor Speed Links - Enable-Pin des linken Motors
int MotorLa = 7; // Richtungspin a
int MotorLb = 8; // Richtungspin b
void setup()
{
pinMode(MSL, OUTPUT);
pinMode(MotorLa, OUTPUT);
pinMode(MotorLb, OUTPUT);
}
void loop()
{
analogWrite(MSL, 255); // volle Geschwindigkeit (0 bis 255)
digitalWrite(MotorLa, LOW); // eine Richtung: a LOW, b HIGH
digitalWrite(MotorLb, HIGH);
}Lesson 5: Drive, little robot!
Lesson 5: Both Motors - the Funbot drives
int MSL = 9; // left motor
int MotorLa = 7;
int MotorLb = 8;
int MSR = 6; // right motor
int MotorRa = 5;
int MotorRb = 4;
void setup()
{
pinMode(MSL, OUTPUT);
pinMode(MotorLa, OUTPUT);
pinMode(MotorLb, OUTPUT);
pinMode(MSR, OUTPUT);
pinMode(MotorRa, OUTPUT);
pinMode(MotorRb, OUTPUT);
}
void loop()
{
analogWrite(MSL, 255);
digitalWrite(MotorLa, LOW);
digitalWrite(MotorLb, HIGH);
analogWrite(MSR, 255);
digitalWrite(MotorRa, LOW);
digitalWrite(MotorRb, HIGH);
}Lesson 6: Follow the line
Lesson 6: Follow the line
int modul_Tracking = 12;
int Tracking_status = 0;
int MSL = 9;
int MotorLa = 7;
int MotorLb = 8;
int MSR = 6;
int MotorRa = 5;
int MotorRb = 4;
void setup()
{
pinMode(modul_Tracking, INPUT);
pinMode(MSL, OUTPUT);
pinMode(MotorLa, OUTPUT);
pinMode(MotorLb, OUTPUT);
pinMode(MSR, OUTPUT);
pinMode(MotorRa, OUTPUT);
pinMode(MotorRb, OUTPUT);
}
void loop()
{
Tracking_status = digitalRead(modul_Tracking);
if (Tracking_status == 1)
{
// Heller Untergrund erkannt: Rechtskurve,
// der linke Motor dreht schnell, der rechte langsam.
analogWrite(MSL, 255);
digitalWrite(MotorLa, LOW);
digitalWrite(MotorLb, HIGH);
analogWrite(MSR, 40);
digitalWrite(MotorRa, LOW);
digitalWrite(MotorRb, HIGH);
}
else
{
// Dunkle Linie erkannt: Linkskurve,
// jetzt dreht der rechte Motor schnell.
analogWrite(MSL, 40);
digitalWrite(MotorLa, LOW);
digitalWrite(MotorLb, HIGH);
analogWrite(MSR, 255);
digitalWrite(MotorRa, LOW);
digitalWrite(MotorRb, HIGH);
}
}Lesson 7: Measuring distance with ultrasound
Lesson 7: Measure distance and light up when under 30 cm
int modul_led = A1;
int modul_trigger = 10;
int modul_echo = 11;
float entfernung = 0;
float gemessene_zeit = 0;
float zeit_pro_strecke = 0;
// Schallgeschwindigkeit in Zentimetern je Mikrosekunde
float schallgeschwindigkeit = 0.0341;
void setup()
{
pinMode(modul_led, OUTPUT);
pinMode(modul_trigger, OUTPUT);
pinMode(modul_echo, INPUT);
}
void loop()
{
// Trigger kurz ausschalten, damit keine Stoersignale anliegen
digitalWrite(modul_trigger, LOW);
delayMicroseconds(5);
// Messung starten: 10 Mikrosekunden HIGH
digitalWrite(modul_trigger, HIGH);
delayMicroseconds(10);
digitalWrite(modul_trigger, LOW);
// Zeit messen, bis das Echo eintrifft
gemessene_zeit = pulseIn(modul_echo, HIGH);
// Nur der Hinweg zaehlt, also durch zwei teilen
zeit_pro_strecke = gemessene_zeit / 2;
// Weg = Zeit mal Geschwindigkeit
entfernung = zeit_pro_strecke * schallgeschwindigkeit;
if (entfernung < 30)
{
digitalWrite(modul_led, HIGH);
delay(1000);
digitalWrite(modul_led, LOW);
}
}Lesson 8: Driving and stopping in front of obstacles
Lesson 8: Drive and stop in front of an obstacle
int modul_led = A1;
int modul_trigger = 10;
int modul_echo = 11;
float entfernung = 0;
float gemessene_zeit = 0;
float zeit_pro_strecke = 0;
float schallgeschwindigkeit = 0.0341;
int MSL = 9;
int MotorLa = 7;
int MotorLb = 8;
int MSR = 6;
int MotorRa = 5;
int MotorRb = 4;
void setup()
{
pinMode(modul_led, OUTPUT);
pinMode(modul_trigger, OUTPUT);
pinMode(modul_echo, INPUT);
pinMode(MSL, OUTPUT);
pinMode(MotorLa, OUTPUT);
pinMode(MotorLb, OUTPUT);
pinMode(MSR, OUTPUT);
pinMode(MotorRa, OUTPUT);
pinMode(MotorRb, OUTPUT);
}
void loop()
{
// Beide Motoren vorwaerts
analogWrite(MSR, 255);
digitalWrite(MotorRa, LOW);
digitalWrite(MotorRb, HIGH);
analogWrite(MSL, 255);
digitalWrite(MotorLa, LOW);
digitalWrite(MotorLb, HIGH);
// Abstand messen
digitalWrite(modul_trigger, LOW);
delayMicroseconds(5);
digitalWrite(modul_trigger, HIGH);
delayMicroseconds(10);
digitalWrite(modul_trigger, LOW);
gemessene_zeit = pulseIn(modul_echo, HIGH);
zeit_pro_strecke = gemessene_zeit / 2;
entfernung = zeit_pro_strecke * schallgeschwindigkeit;
// Hindernis naeher als 30 cm: beide Motoren stoppen
if (entfernung < 30)
{
analogWrite(MSR, 0);
digitalWrite(MotorRa, LOW);
digitalWrite(MotorRb, LOW);
analogWrite(MSL, 0);
digitalWrite(MotorLa, LOW);
digitalWrite(MotorLb, LOW);
}
}Lesson 9: The underbody lighting
Lesson 9: The Six Pixels in Three Colors
#include <Adafruit_NeoPixel.h>
#define PIN 13 // Pixel data line
#define NUMPIXELS 6 // Number of pixels on the Funbot
Adafruit_NeoPixel BodenLED = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
BodenLED.begin(); // Start the object - comparable to pinMode
}
void loop()
{
// setPixelColor sets the color, but does not display it yet.
BodenLED.setPixelColor(0, BodenLED.Color(255, 0, 0)); // red
BodenLED.setPixelColor(1, BodenLED.Color(255, 0, 0));
BodenLED.setPixelColor(2, BodenLED.Color(0, 255, 0)); // green
BodenLED.setPixelColor(3, BodenLED.Color(0, 255, 0));
BodenLED.setPixelColor(4, BodenLED.Color(0, 0, 255)); // blue
BodenLED.setPixelColor(5, BodenLED.Color(0, 0, 255));
BodenLED.show(); // Only this command actually switches the pixels
}Lesson 10: The MPU6050 accelerometer
Lesson 10: Read the Z-axis angle
#include <TinyMPU6050.h>
// Object for the sensor. (Wire) means: communication via I2C.
MPU6050 MPU(Wire);
void setup()
{
MPU.Initialize();
Serial.begin(9600);
Serial.println("Calibration starting, please wait!");
MPU.Calibrate();
Serial.println("Calibration complete");
}
void loop()
{
MPU.Execute(); // retrieve current measurement data
Serial.print("Z-axis angle = ");
Serial.println(MPU.GetAngZ());
}Lesson 11: Drive straight using the position sensor
Lesson 11: Drive straight using the position sensor
// First, include the libraries and create the variables.
// 1. For the orientation sensor
#include <TinyMPU6050.h>
MPU6050 MPU(Wire);
float InitialPosition = 0;
float Deviation = 0;
// 2. For the motors
int MSL = 9;
int MotorLa = 7;
int MotorLb = 8;
int MSR = 6;
int MotorRa = 5;
int MotorRb = 4;
void setup() {
// Start serial communication to read the measured values
Serial.begin(9600);
// Set the inputs and outputs
pinMode(MSL, OUTPUT);
pinMode(MotorLa, OUTPUT);
pinMode(MotorLb, OUTPUT);
pinMode(MSR, OUTPUT);
pinMode(MotorRa, OUTPUT);
pinMode(MotorRb, OUTPUT);
// Start and calibrate the sensor
MPU.Initialize();
Serial.println("Calibration starting, please wait!");
MPU.Calibrate();
Serial.println("Calibration complete");
// Record and store the initial position once
MPU.Execute();
InitialPosition = MPU.GetAngZ();
}
void loop() {
// Get the current readings from the orientation sensor
MPU.Execute();
// How far has the Funbot deviated from its initial position?
// To find out, subtract the initial position from the current value.
Deviation = MPU.GetAngZ() - InitialPosition;
Serial.println(Deviation);
// Note: The value increases counterclockwise,
// and decreases clockwise, becoming negative as well.
if (Deviation >= 0) {
// Deviated to the left, so countersteer slightly to the right
analogWrite(MSL, 255);
digitalWrite(MotorLa, LOW);
digitalWrite(MotorLb, HIGH);
analogWrite(MSR, 40);
digitalWrite(MotorRa, LOW);
digitalWrite(MotorRb, HIGH);
}
else {
// Deviated to the right, so countersteer slightly to the left
analogWrite(MSL, 40);
digitalWrite(MotorLa, LOW);
digitalWrite(MotorLb, HIGH);
analogWrite(MSR, 255);
digitalWrite(MotorRa, LOW);
digitalWrite(MotorRb, HIGH);
}
}Rate the instructions