Go Down

Topic: Update causing errors? (Read 607 times) previous topic - next topic

crispinonick

I have been running this code on my arduino uno and 3 different nanos for weeks with no issues. I just hooked them up to add a servo to find my code now has errors what has changed?
this is what i have been using with no issues.
void setup() {
  // put your setup code here, to run once:

}

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

/************************************************/
#include <Servo.h>
/************************************************/
Servo myservo;//create servo object to control a servo
/************************************************/
Servo myservo.attach(9);//attachs the servo on pin 9 to servo object
  myservo.write(0);//back to 0 degrees
  delay(3000);//wait for a second

/*************************************************/

  Servo myservo.write(160);//goes to 160 degrees
  delay(1000);//wait for a second
  myservo.write(0);//goes to 0 degrees
  delay(6000);//wait for a second.99


/**************************************************/

CtrlAltElite

#1
Feb 25, 2018, 01:12 am Last Edit: Feb 25, 2018, 01:12 am by CtrlAltElite
The code you posted cannot possibly compile.
Please don't waste our time.
Has there been an outbreak of the stupid virus, and I didn't get the memo?

CtrlAltElite

Quote
Well your wrong
That would be "you're wrong".
Except that I'm not, any idiot (except you) can see that that code can't compile.
If it can't compile, it can't run.
If it can't run, it can't work.

Over to you.
Has there been an outbreak of the stupid virus, and I didn't get the memo?

crispinonick

Your ridicule is less than helpful. I am seeking help. What you see here is what I wrote based off of some examples I have reviewed. Like it or not It did work it and I was able to upload my micro controllers. But now its telling me I have errors. If i need to start over Ill go that route but if something has changed and i can fix what i have, ill do that. you clearly see a major flaw

pert

#4
Feb 25, 2018, 02:17 am Last Edit: Feb 25, 2018, 04:17 am by pert
Try this:
Code: [Select]
#include <Servo.h>

Servo myservo;  //create servo object to control a servo

void setup() {
  myservo.attach(9);  //attaches the servo on pin 9 to servo object
}

void loop() {
  myservo.write(0);  //back to 0 degrees
  delay(3000);  //wait for a second
  Servo myservo.write(160);  //goes to 160 degrees
  delay(1000);  //wait for a second
  myservo.write(0);  //goes to 0 degrees
  delay(6000);  //wait for a second.99
}


You really need to take some time to actually understand what you're doing instead of just slapping down some code in random places and hoping it will work. Please take some time to thoroughly read this:
https://www.arduino.cc/reference/en/language/structure/sketch/setup/
And this:
https://www.arduino.cc/reference/en/language/structure/sketch/loop/

sterretje

Your ridicule is less than helpful. I am seeking help. What you see here is what I wrote based off of some examples I have reviewed. Like it or not It did work it and I was able to upload my micro controllers. But now its telling me I have errors. If i need to start over Ill go that route but if something has changed and i can fix what i have, ill do that. you clearly see a major flaw
And that is exactly what @CtrlAltElite was indicating. This is what I get when I compile (IDE 1.8.5)
Code: [Select]
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_149533\sketch_feb25a.ino: In function 'void loop()':

sketch_feb25a:14: error: expected initializer before '.' token

   Servo myservo.attach(9);//attachs the servo on pin 9 to servo object

                ^

sketch_feb25a:20: error: expected initializer before '.' token

   Servo myservo.write(160);//goes to 160 degrees

                ^

sketch_feb25a:23: error: expected '}' at end of input

   delay(6000);//wait for a second.99

              ^

Using library Servo at version 1.1.2 in folder: C:\Program Files (x86)\Arduino\libraries\Servo
exit status 1
expected initializer before '.' token



Well your wrong because it did work and still does on the 2 nanos i have not updated yet so something has changed and you waste your own time if you cant help move along
Yes, your code has changed.


To help further
1)
You're missing a } at the end of loop() for starters.
2)
Have you ever seen an example where #include is inside loop()? Or that contains Servo myservo.attach(9);? If so, you have been looking at the wrong examples. Look at the examples that come with the IDE and base your code on that.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

CtrlAltElite

Try this:
Code: [Select]
#include <Servo.h>

Servo myservo;  //create servo object to control a servo

void setup() {
  myservo.attach(9);  //attaches the servo on pin 9 to servo object
}

void loop() {
  myservo.write(0);  //back to 0 degrees
  delay(3000);  //wait for a second
  Servo myservo.write(160);  //goes to 160 degrees  NO, NO, NO!!
  delay(1000);  //wait for a second
  myservo.write(0);  //goes to 0 degrees
  delay(6000);  //wait for a second.99
}



Has there been an outbreak of the stupid virus, and I didn't get the memo?

lastchancename

Wait till OP discovers blocking code...
Sparks will fly !
Experienced responders have a nose for laziness, (they were beginners once)... expecting the poster to contribute to the learning experience.

crispinonick

Thank you everyone for the tips and links I will be going through them all today to see what happened to my code. I am very new to this and I am working out of a beginners programming book.

lastchancename

#9
Feb 25, 2018, 09:40 pm Last Edit: Feb 27, 2018, 06:51 am by lastchancename
The battle is always at the beginning!
It will get easier as you become more familiar with the structure and syntax.
The problem with programming is that it has to be flexible - because no two programs or solutions are ever the same!
Experienced responders have a nose for laziness, (they were beginners once)... expecting the poster to contribute to the learning experience.

crispinonick

So the struggle is real... I cannot find what changed in my code or how to correct it I somehow had it running a full 160degree sweep then immediately back to 0 delay for 6 seconds(about) in a loop. Hours later and I cannot seem to write a code without errors. I even copied a few examples I found exactly (they didn't do the same thing I need but just wanted to see something working one was from Arduino and 2 off instructables and even those show errors. This seems unlikely from 2 trusted sources. Anyone have a link to a known working code I can try. Anything involving a single servo.

sterretje

If you can't compile an IDE servo example, post that code that you used and the error message that you get. You might also have deeper laying problems and might want to test compiling a simple blink.

Note: not all instructables are reliable.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up