Code snippets

The following code snippets are a wild collection and have different functions.

For example, to edit your own sketches, but also to test or calibrate.

They are not only useful for a DB-01, but can also be a little helper for a variety of things.

Align servomotors using servo.write(value)

This sketch aligns 18 servo motors in the middle. Regardless of whether the servo has 90, 180 or 270 degrees of freedom of movement. Very useful when assembling arms / legs.


#ifndef ARDUINO_AVR_MEGA2560
#error Wrong board. Please choose “Arduino/Genuino Mega or Mega 2560”
#endif

#include <Servo.h>

Servo servos[18];
const int servosPins[] = { 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39};

void setup() {
for (int i = 0; i < 18; i++) {
servos[i].attach(servosPins[i]);
servos[i].write(90);
}
}

void loop() {
// put your main code here, to run repeatedly:
}


Align servomotors using servo.writeMicroseconds(value)

This sketch aligns 18 servo motors in the middle. Very useful when assembling arms / legs.

According to thr Arduino reference (https://www.arduino.cc/en/Reference/ServoWriteMicroseconds):

Writes a value in microseconds (uS) to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft. On standard servos a parameter value of 1000 is fully counter-clockwise, 2000 is fully clockwise, and 1500 is in the middle.

Note that some manufactures do not follow this standard very closely so that servos often respond to values between 700 and 2300. Feel free to increase these endpoints until the servo no longer continues to increase its range. Note however that attempting to drive a servo past its endpoints (often indicated by a growling sound) is a high-current state, and should be avoided.

Continuous-rotation servos will respond to the writeMicrosecond function in an analogous manner to the write function.

Using a LDX-227 or LDX-218?

The servos LDX-227 and LDX-218 works with 500-2500us!


#ifndef ARDUINO_AVR_MEGA2560
#error Wrong board. Please choose “Arduino/Genuino Mega or Mega 2560”
#endif

#include <Servo.h>

Servo servos[18];
const int servosPins[] = { 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39};

void setup() {
for (int i = 0; i < 18; i++) {
servos[i].attach(servosPins[i]);
servos[i].writeMicroseconds(1500);
}
}

void loop() {
// put your main code here, to run repeatedly:
}


I2C scanner

This sketch is very useful for reading out the 7-bit addresses of the individual devices.


// ————————————–
// i2c_scanner
//
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26 2013
// V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
// by Arduino.cc user Krodal.
// Changes by louarnold removed.
// Scanning addresses changed from 0…127 to 1…119,
// according to the i2c scanner by Nick Gammon
// https://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
// As version 4, but address scans now to 127.
// A sensor seems to use address 120.
// Version 6, November 27, 2015.
// Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>

void setup()
{
Wire.begin();

Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println(“\nI2C Scanner”);
}

void loop()
{
byte error, address;
int nDevices;

Serial.println(“Scanning…”);

nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();

if (error == 0)
{
Serial.print(“I2C device found at address 0x”);
if (address<16)
Serial.print(“0″);
Serial.print(address,HEX);
Serial.println(” !”);

nDevices++;
}
else if (error==4)
{
Serial.print(“Unknown error at address 0x”);
if (address<16)
Serial.print(“0”);
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println(“No I2C devices found\n”);
else
Serial.println(“done\n”);

delay(5000); // wait 5 seconds for next scan
}