Please Help error expected declaration before '}' token and units used in servo

Hi all,

I'm new to Arduino and coding but looking forward to the projects this can do.

I'm building an egg turner for an incubator and luckily found this code on the internet as part of a huge sketch for controlling all of the incubator.

I have cut and pasted only the servo part and removed everything i thought that didn't relate to that.

I have fixed some obvious errors i made but the last one is a bit tricky.

So could someone please help me out or point me in the place to find it.

My sketch will not compile and gives an error ......expected declaration before '}' token at the very last curly bracket........looking at the code i noticed just above .... // Pause for 2 seconds
delay(2000); there was another double curly which looked wrong ....so removed one and it compiled!........

Does this sound correct?

secondly the positioning numbers used don't seem to match the comments .....i thought you simply use degrees or is there another system of units???? example ..

...myservo.write(70); //put the servo at intitial position of 16 degrees

Any help would be appreciated :slight_smile:

// Servo version of

#include <Servo.h>
#include <Wire.h>
Servo myservo;                                // create servo object to control a servo

// Servo version of
int pos = 0;                                  // variable to store the servo position
int istate = 0;

int END = 0;
unsigned long previousMillis = 0;
const long interval = 3600000UL; //timer for roll eggs 1HOUR
void setup()
{
Serial.begin(9600);
 myservo.attach(9); // servo control is set to pin 9 (usually yellow wire is control, black goes to ground red goes to +5V)
 myservo.write(70); //put the servo at intitial position of 16 degrees
 myservo.detach();
}
void loop() {
 
  // this section is to roll the eggs   
unsigned long currentMillis = millis();

int tleft = ((currentMillis - previousMillis)/ 1000) /60;

if (currentMillis - previousMillis >= interval)
{
 previousMillis = currentMillis;

if(istate == 0 && END == 0){  
 //myservo.attach(9);
  for (pos = 80; pos <= 155; pos += 1) { // goes from 16 degrees to 80 degrees  in steps of 1 degree
    myservo.attach(9);
    myservo.write(pos);
    istate=1;
    delay(50);
  }
  myservo.detach();
}
else if( istate == 1 && END == 0)
{ 
// myservo.attach(9);        
  for (pos = 155 ; pos >=80; pos -= 1) { // goes from 80 degrees to 0 degrees
   myservo.attach(9);
    myservo.write(pos); 
    istate = 0;
    delay(50);                      // slow the servo down a bit to turn the eggs        
  }
  myservo.detach();
}

}
 
      // Pause for 2 seconds
  delay(2000);
 }
}

It means that you don't have your curly braces correctly balanced. You have an extra closing brace with no opening brace. When you write code in the IDE, do control (or command on a mac) T to format the code. Then if you have made such an error it will show up in how everything lines up. In this case there is an extra } at the end.

Why are you detaching the servo? Next time you attach, it will jump to the default position (90 degrees), probably not good for the egg. Why are you calling 70 degrees 16?

Hi

not sure why he detached the servo the code is not mine i copied it from someone else when i do a test run i will keep an eye out for that odd movement

As for the 70 not like 16 that is my question....cant make heads or tails of that further down the code he used 155 and i cant relate that to anything in his comments

cheers

Is the container that holds the egg(s) attached directly to the servo shaft? In other words, if the servo turns 90 degrees, does the container turn 90 degrees? Describe exactly how you want the container to turn and when, better yet, see if you can find a short video of an egg turner like you want to make and post a link to it.

You'd probably be much better off writing a sketch from scratch than attempting to hack up slabs of code that you don't understand. I mean, what do you want? Something that moves a servo back and forth every few seconds? That's really not difficult to do.