a function-definition is not allowed here before "{" token

i need help for my project code
Please help me
Thanks

testing2.ino (4.58 KB)

Hi marsmen,

please post your your code as a code-section. Easiest way is

  • to press Ctrl-T in the IDE to auto-format your code.
  • then do a right-click with the mouse and choose "copy for forum"
  • change to your browser and paste the clipboard by pressing Ctrl-V

best regards Stefan

Why have you got the definition of a class inside the loop() function ?
Where does the class definition end ? Come to that, where does the loop() function end ?

Also

 pixy. init ();

looks very wrong too

How much experience do you have in coding?
I guess very little because you positioned the loop function inside setup
and everything else inside the loop-fucntion.

Your code looks like copy & pasted from somewhere.
defining a class is pretty advanced programming.

I recommend learning the basics.
to compare learning programming with a allday-situation

imagine you want to learn to ride a bike.
You have looked at somebody who is doing it
and think AH! I understand sit on the saddle put your hands on the handlebars
and kick your feet down and then it is moving.

You know from your own experience that's not all.

  • You have to learn how to put the pedal in a certain position to start,
  • you have to learn where is the brake,
  • you have to learn how to shift gears

you have to practice BALANCING on the bike with your OWN BODY

it is similar with learning to program

You have to learn some basics. You have to practice

Asking here in the forum in this style

"I have this code here but it does not do what it should" End of message

is like you fell off the bike and then go to a experienced biker
telling him "I was unable to ride the bike" can YOU drive my bike for 200 meters to get me going?"

makes no sense at all. It's the same thing with programming.

Though the difference is your "programming"-Bike has fivehundred wheels, fifty pedals, eighty gearshifting-levers
five handlebars etc.

For starting to move (=starting programming) it is enough to get explained use handlebar number three, pedals number 27 and wheels 1,2,3 and 4.

But that's a different exercise than doing a backflip in the halfpipe. You simple don't start freestyle BMX-ing with backflips in the halfpipe. You start with basic exercises. It's the same thing with learning programming. You start with small exercises.

best regards Stefan

you positioned the loop function inside setup

Really ?

Here is setup

void setup ()
{
  Serial.begin (9600);
  pinMode (LeftA, OUTPUT);
  pinMode (LeftB, OUTPUT);
  pinMode (RightA, OUTPUT);
  pinMode (RightB, OUTPUT);
  pixy. init ();
}

UKHeliBob:
Also

 pixy. init ();

looks very wrong too

A little odd maybe, but not not wrong.

UKHeliBob:
Really ?
Here is setup

void setup ()

{
Serial.begin (9600);
pinMode (LeftA, OUTPUT);
pinMode (LeftB, OUTPUT);
pinMode (RightA, OUTPUT);
pinMode (RightB, OUTPUT);
pixy. init ();
}

hm maybe I meMyself did a copy&paste-error. So if not I apologise

best regards Stefan

so my code done, then when i'm compile say error to compile to arduino uno

#include <SPI.h>
#include <Pixy.h>

#define X_CENTER 160L
#define RCS_MIN_POS 0L
#define RCS_MAX_POS 1000L
#define RCS_CENTER_POS ((RCS_MAX_POS-RCS_MIN_POS)/2)

Pixy pixy;

const int RightA = 3;
const int RightB = 4;
const int LeftA = 5;
const int LeftB = 6;
byte speed = 255 * 2 / 3;


const int trigPin = 10;
const int echoPin = 8;

void setup () {
  Serial.begin(9600);
  pinMode(LeftA, OUTPUT);
  pinMode(LeftB, OUTPUT);
  pinMode(RightA, OUTPUT);
  pinMode(RightB, OUTPUT);
  pixy.init();
}

class ServoLoop
{
  public:
    ServoLoop(int32_t proportionalGain, int32_t derivativeGain);

    void update(int32_t error);

    int32_t m_pos;
    int32_t m_prevError;
    int32_t m_proportionalGain;
    int32_t m_derivativeGain;

};
ServoLoop::ServoLoop(int32_t proportionalGain, int32_t derivativeGain)
{
  m_pos = RCS_CENTER_POS;
  m_proportionalGain = proportionalGain;
  m_derivativeGain = derivativeGain;
  m_prevError = 0x80000000L;
}
void ServoLoop::update(int32_t error)
{
  long int velocity;
  char buf[32];
  if (m_prevError != 0x80000000)
  {
    velocity = (error * m_proportionalGain +
                (error - m_prevError) * m_derivativeGain) >> 10;
    m_pos += velocity;
    if (m_pos > RCS_MAX_POS)
    {
      m_pos = RCS_MAX_POS;
    }
    else if (m_pos < RCS_MIN_POS)
    {
      m_pos = RCS_MIN_POS;
    }
  }
  m_prevError = error;
}
ServoLoop panLoop (500, 500);

void pixyfunc() {
  uint16_t blocks;
  blocks = pixy.getBlocks();

  if (blocks)
  {
    int trackedBlock = TrackBlock(blocks);
    DriveToObject(trackedBlock);
  }
  else
  {
    drive(0, 0);
    ScanForBlocks();
  }
}

int oldX, oldSignature;

int TrackBlock(int blockCount)
{
  int TrackedBlock = 0;
  long maxSize = 0;
  float objectheight;

  for (int i = 0; i < blockCount; i++)
  {
    if ((oldSignature == 0) || (pixy.blocks[i].signature == oldSignature))
    {
      long newSize = pixy.blocks[i].height * pixy.blocks[i].width;
      if (newSize > maxSize)
      {
        TrackedBlock = i;
        maxSize = newSize;
        objectheight = pixy.blocks[i].height;
      }
    }
  }
  calculatedistance(objectheight);
  int32_t panError = X_CENTER - pixy.blocks[TrackedBlock].x;
  panLoop.update(panError);
  pixy.setServos(panLoop.m_pos, panLoop.m_pos);
  oldX = pixy.blocks[TrackedBlock].x;
  oldSignature = pixy.blocks[TrackedBlock].signature;
  return TrackedBlock;
}

int32_t prev_followError = 0;

void DriveToObject(int trackedBlock)
{
  int32_t followError = RCS_CENTER_POS - panLoop.m_pos;
  int fowardSpeed;
  int Kd = 2 * fowardSpeed;
  long distance = 0;
  distance = ultrasonic();

  if (distance > 30)
  {
    fowardSpeed = speed;
  }
  else if (distance > 15 && distance <= 30)
  {
    fowardSpeed = 0;
    followError = 0;
  }
  else if (distance <= 15)
  {
    fowardSpeed = speed;
    followError = 0;
  }
  int32_t differential = (followError + (followError * fowardSpeed) +
                          Kd * (followError - prev_followError)) >> 8;

  int speedleft = fowardSpeed - differential / 2;
  int speedright = fowardSpeed + differential / 2;

  if (speedleft < 0)
  {
    speedleft = 0;
  }
  else if (speedleft > 255)
  {
    speedleft = 255;
  }
  else if (speedright > 0)
  {
    speedright = 0;
  }
  else if (speedright > 255)
  {
    speedright = 255;
  }

  if (distance > 15)
  {
    drive(speedright, speedleft);
  }
  else
  {
    reverse(speedleft, speedright);
  }
  prev_followError = followError;
}

int scanIncrement = (RCS_MAX_POS - RCS_MIN_POS) / 150;
uint32_t lastMove = 0;

void ScanForBlocks()
{
  if (millis() - lastMove > 20)
  {
    lastMove = millis();
    panLoop.m_pos += scanIncrement;
    if ((panLoop.m_pos >= RCS_MAX_POS) || (panLoop.m_pos <= RCS_MIN_POS))
    {
      scanIncrement = -scanIncrement;
    }
    pixy.setServos(panLoop.m_pos, panLoop.m_pos);
  }
}

long ultrasonic() {
  long duration, cm;

  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  cm = microsectocm(duration);
  Serial.println("Ultrasonic_distance:_");
  Serial.print(cm);
  Serial.print("cm");
  return cm;
}

long microsectocm(long microsec)
{
  return microsec / 29 / 2;
}

void calculatedistance(float objectheight)
{
  float d1 = 30;
  float d2 = 75;
  float h1 = 21;
  float h2 = 7;
  float dist = d1 + (d2 - d1) * ((objectheight - h1) / (h2 - h1));
  Serial.print("_Calculated_distance_from_Pixy:_");
  Serial.print(dist);
  Serial.print("_cm_");
}

void drive(int speedleft, int speedright)
{
  analogWrite(LeftA, 0);
  analogWrite(LeftB, speedleft);
  analogWrite(RightA, 0);
  analogWrite(RightB, speedright);
}

void reverse(int speedleft, int speedright)
{
  analogWrite(LeftA, speedleft);
  analogWrite(LeftB, 0);
  analogWrite(RightA, speedright);
  analogWrite(RightB, 0);
}

mycode.ino (5.02 KB)

Post text, not pictures.
Post code, in code tags.

Can you also post the error message?

If possible, go to your Arduino IDE, then navigate to File>Preferences and find Compiler Warning settings. Change it to All. Then try to compile your code again, and post the ENTIRE error message in code tags.

Edit: I tried compiling myself, but I don't have your Pixy library, so...

this is the error message
Arduino: 1.8.14 Hourly Build 2020/10/09 12:33 (Windows 10), Board: "Arduino Uno"

C:\Users\AppData\Local\Temp\cc0iShmV.ltrans0.ltrans.o: In function `main':

C:\Users\Downloads\arduino-nightly-windows\hardware\arduino\avr\cores\arduino/main.cpp:46: undefined reference to `loop'

collect2.exe: error: ld returned 1 exit status

exit status 1

Error compiling for board Arduino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Now it seems that you do not have a loop() function in your sketch

POST YOUR SKETCH !

here my sketch

mycode.ino (5.02 KB)

Post, not attach.

marsdefacto:
here my sketch

As suspected in #11, no loop() in there.

i have found my probelm, it on my void pixyfunc, i change to loop it worked, thank for your advice and suggest me to learn more, yeah im still new

recently I followoed a post that was moved to the spanish subforum.
The moderator of the spanish subforum has a very consequent line about users that don't re-edit posts to post sketches as a code-section.

He just closes the thread writing "read the forum-rules re-edit your post and your thread gets opened again".
I begin to like this idea .....

best regards Stefan

StefanL38:
He just closes the thread writing "read the forum-rules re-edit your post and your thread gets opened again".
I begin to like this idea .....

A huge +1

Good LORD! You guys are more into the rules than the customers!

-jim lee

Cause it's irritating! I can't even help anyone!! >:(

Someone will be like I got an error in my sketch...They won't post their code. We'll tell them to post in code tags and they'll attach it...We'll they them to Ctrl + V they'll just post and not in code tags...Finally on their 4th attempt they'll post their code properly!!

How do I help people like this? That's why I support the thing Stefan noticed...!