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
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.
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
#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:
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
And that is exactly what @CtrlAltElite was indicating. This is what I get when I compile (IDE 1.8.5)
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
crispinonick:
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.
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
}
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.
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!
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.
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.