else if problem

Hi, I am having a problem with the else if part in my code, could someone please look at it?

  if(voice=="paw"){
    ball1.writeMicroseconds(1000);
  }
else if(ball5) == int pulse4){
  ball1.writeMicroseconds(pulse0);
{
else if(ball1 == }int pulse0){
  ball7.writeMicroseconds(pulse6);
{
else if(ball7 == }int pulse6){
  ball6.writeMicroseconds(pulse5);
{
else if(ball6 == }int pulse5){
  ball2.writeMicroseconds(pulse1);
{
else if(ball2 == }int pulse1){
  ball8.writeMicroseconds(pulse7);
{
else if(ball8 == }int pulse7){
  ball4.writeMicroseconds(pulse3);
}

This is not the whole code, it is just part of it. I am fixing all the errors my compiler is showing, but I can't get past this one. The error message is could not convert 'ball5' from 'Servo' to 'bool'. That first happened when that part (Line 4) was the first "if" statement, so I thought of putting a voice command there to make the voice command the first "if", but it did not work. Now it is just showing the same error that it showed before the voice command.

else if(ball1 == }int pulse0){ Does that even compile?

No, it is telling me to change it to change it from this:

else if(ball5 == int pulse4){

To this:

else if(ball5) == int pulse4){

But then it gives me this error message:
could not convert 'ball5' from 'Servo' to 'bool'

Maybe you should explain what it is you're trying to do.

I doubt it is telling you that.
How about

else if(ball5 == pulse4){

which compares the value of ball5 with the value of the existing pulse4 variable instead of declaring a new variable named pulse4 with an an unknown value

That is what I wrote, but it is telling me I need a primary-expression before the "=", So I fixed it like this:

else if(ball5) == int pulse4){

Then it worked, but it gave me that error.

TheMemberFormerlyKnownAsAWOL:
Maybe you should explain what it is you're trying to do.

I am trying to write else if statements that tell the next servo to move when one reaches its position...

But you must see that a Servo object is never going to equal a pulse length?

The concept of "next" looks to me to be pretty random (six to two, two to eight,eight to four...)
Have you considered an array?

I am making a robotic dog, so the order of how the statements are written are in the order of how the legs of my robotic dog will walk.
Will the array tell the next servo to move once the previous servo has reached it's position? Some parts of my code are like this:

void moveServos(int pulse0, int pulse1, int pulse2, int pulse3, int pulse4, int pulse5, int pulse6, int pulse7);

Is that what you mean?

Some parts of my answer may look like "yes", but keyhole debugging is exceptionally difficult.

I am just 14 years old, so I am still trying to figure out how this all works. And I appreciate any assistance on my project. :slight_smile:

Your age is pretty irrelevant here, what is relevant is you're keeping your cards awfully close to your chest.

If you want help, you have to help us to help you.

Ok, thank you. I would like to make the following servo move only when the first servo has reached it's position, but I am not completely sure how to do that...

Yes, we get that.

What we don't get is any hint that you're going to tell us how we can know that the first servo has reached its position.

This is the whole code:

#include <Servo.h>
#include <math.h>
#define MAX_GAMMA 50

#define MAX_PULSE 2500
#define MIN_PULSE 560

Servo ball1;
Servo ball2;
Servo ball3;
Servo ball4;
Servo ball5;
Servo ball6;
Servo ball7;
Servo ball8;

unsigned long previousMillis = 0;
const long interval = 20;
unsigned long loopTime;
unsigned long previousLooptime;
double t;

struct angles {
  double tetta;
  double alpha;
  double gamma;
};
struct angles anglesFR;
struct angles anglesFL;
struct angles anglesBR;
struct angles anglesBL;

const byte numChars = 32;
char receivedChars[numChars];
int spaceCounter = 0;

boolean newData = false;

int pulse0, pulse1, pulse2, pulse3, pulse4, pulse5, pulse6, pulse7;

char a;
int pulse;
String voice;
void setup(){

  pinMode(9,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(2,OUTPUT);
  
  ball1.attach(2);
  ball2.attach(3);
  ball3.attach(4);
  ball5.attach(5);
  ball6.attach(6);
  ball7.attach(7);
  ball8.attach(9);
  
  Serial.begin(9600);
}
void loop(){
  while(Serial.available()>0)
  {
    delay(10);
    char c=Serial.read();
    if(c=='#')
    {
      break;
    }
    voice+=c;
    }
{
  unsigned long currentMillis = millis();{
    if (currentMillis - previousMillis >= interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;
      t = float(currentMillis) / 1000;
      /////////cuenta el tiempo que tarda el bucle en ejecutarse
      loopTime = currentMillis - previousLooptime;
      Serial.print("<");
      Serial.print(loopTime);
      Serial.print("#");
      previousLooptime = currentMillis;

}

void moveServos(int pulse0, int pulse1, int pulse2, int pulse3, int pulse4, int pulse5, int pulse6, int pulse7);
{
  ball5.writeMicroseconds(pulse4);
  ball1.writeMicroseconds(pulse0);
  
  ball7.writeMicroseconds(pulse6);
  ball3.writeMicroseconds(pulse2);
  
  ball6.writeMicroseconds(pulse5);
  ball2.writeMicroseconds(pulse1);
  
  ball8.writeMicroseconds(pulse7);
  ball4.writeMicroseconds(pulse3);
}
  if(voice=="paw"){
    ball1.writeMicroseconds(1000);
  }
else if(ball5) == int pulse4){
  ball1.writeMicroseconds(pulse0);
{
else if(ball1 == }int pulse0){
  ball7.writeMicroseconds(pulse6);
{
else if(ball7 == }int pulse6){
  ball6.writeMicroseconds(pulse5);
{
else if(ball6 == }int pulse5){
  ball2.writeMicroseconds(pulse1);
{
else if(ball2 == }int pulse1){
  ball8.writeMicroseconds(pulse7);
{
else if(ball8 == }int pulse7){
  ball4.writeMicroseconds(pulse3);
}

It is not my own code, I am just modifying it to work with my robotic dog.

Auto format your code in the IDE

Does every function definition start on the left margin ?
Answer, no. This usually means missing or misplaced { and/or } in the code

For instance, where does the loop() function end ?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.