 # Wave tank: Troubleshooting the code

Hello!
I’m working on a wave tank, consisting of to servos and an accelerometer to measure the waves. With my very basic knowledge on Arduino coding, I’ve managed to make the following code:

``````#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

#include <Wire.h>
#include <ADXL345.h>

ADXL345 adxl; //variable adxl is an instance of the ADXL345 library

int x,y,z;
int rawX, rawY, rawZ;
float X, Y, Z;
float rollrad, pitchrad;
float rolldeg, pitchdeg;

void setup() {
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
adxl.powerOn();
}

void loop() {
for (pos = 0; pos <= 180; pos += 15) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 20) { // goes from 180 degrees to 0 degrees
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position
}
adxl.readAccel(&x, &y, &z); //read the accelerometer values and store them in variables  x,y,z
// Output (x,y,z) on horizontal plane should be approximately (0,0,255)
// the following 3 lines is for an offset
rawX=x-7;
rawY=y-6;
rawZ=z+10;

X = rawX/256.00; // used for angle calculations
Y = rawY/256.00; // used for angle calculations
Z = rawZ/256.00; // used for angle calculations

rollrad = atan(Y/sqrt(X*X+Z*Z));  // calculated angle in radians
pitchrad = atan(X/sqrt(Y*Y+Z*Z)); // calculated angle in radians

rolldeg = 180*(atan(Y/sqrt(X*X+Z*Z)))/PI; // calculated angle in degrees
pitchdeg = 180*(atan(X/sqrt(Y*Y+Z*Z)))/PI; // calculated angle in degrees
// print out values:
/*a
Serial.print("x: "); Serial.print(x);    // raw data without offset
Serial.print(" y: "); Serial.print(y);     // raw data without offset
Serial.print(" z: "); Serial.print(z);     // raw data without offset
Serial.print(" rawX = "); Serial.print(rawX); // raw data with offset
Serial.print(" rawY = "); Serial.print(rawY); // raw data with offset
Serial.print(" rawZ = "); Serial.print(rawZ); // raw data with offset
Serial.print(" X = "); Serial.print(X);    // raw data with offset and divided by 256
Serial.print(" Y = "); Serial.print(Y);    // raw data with offset and divided by 256
Serial.print(" Z = "); Serial.print(Z);    // raw data with offset and divided by 256
*/
Serial.print("\t Angle according to x axis (Roll(deg)) = "); Serial.print(rolldeg);      // calculated angle in degrees
Serial.print("\t Angle according to y axis (Pitch(deg)) = "); Serial.println(pitchdeg);  // calculated angle in degrees
// Serial.print(" Roll(rad) = "); Serial.print(rollrad);   // calculated angle in radians
// Serial.print(" Pitch(rad) = "); Serial.print(pitchrad); // calculated angle in radians
}
``````

I found it to have some problems, like when I finish plugging the accelerometer the servos seem to stop. Also, the speed could be regulated by the “pos += 15”, but increasing it seems to sacrifice the amplitude of the wave, the distance the servo arm travels. Can some kind being help me with this?

Post a wiring diagram (hand drawn, not Fritzing). If you are attempting to power the servo from the Arduino 5V, that is a problem.

The wiring seems to be ok... I've tested the components separately, and they both work correctly. If there's no problem with the code, then I shall investigate the wires or the breadboard.

luciomvl: I found it to have some problems, like when I finish plugging the accelerometer the servos seem to stop. Also, the speed could be regulated by the "pos += 15", but increasing it seems to sacrifice the amplitude of the wave, the distance the servo arm travels. Can some kind being help me with this?

I hope you are not plugging and unplugging components while they are powered!

If something is sacrificing amplitude, then there is not sufficient power to maintain the amplitude. Either the power supply is weak or the servo is not powerful enough.

Just guessing because you have given no information relating to the components, etc.

Paul

The servos are TowerPro SG90 and the accelerometer is an ADXL345.

Paul_KD7HB:
If something is sacrificing amplitude, then there is not sufficient power to maintain the amplitude. Either the power supply is weak or the servo is not powerful enough.

What I mean with amplitude is the distance the servo arm travels, so when made faster, it only travels a tiny portion of what it should.

Why have you not posted a wiring diagram?

95% of the servo problems reported on this forum are due to inadequate power supplies.

so when made faster, it only travels a tiny portion of what it should.

The srevo takes a finite time to move (it can only move so fast). A typical spec is 60 degrees in 200ms. If you do not give it time to move all the way, the moves will be short.

jremington: Why have you not posted a wiring diagram?

95% of the servo problems reported on this forum are due to inadequate power supplies.

Because I've tested the code without the accelerometer and it seems to work. It's not the wiring, I can assure you.

Thanks. I'll consider your problem as solved.

groundFungus:
The srevo takes a finite time to move (it can only move so fast). A typical spec is 60 degrees in 200ms. If you do not give it time to move all the way, the moves will be short.

Is there a manner to speed up the servo whilst maintaining the whole rotation?

The olny way, that I know, is to increase the supply voltage to the maximum allowed (and the supply is capable of supplying adequate current).