Offline
Newbie
Karma: 0
Posts: 5
|
 |
« on: June 03, 2011, 12:47:28 am » |
can anybody tell me if this code is done properly? needs 1 sensor to activate 3 servo's to turn 180 degress when light is detected then back to 0 degrees when no light is detected. If this is not correct, could you please help me fix the errors to make this project work.
#include <Servo.h> Servo myservo; Servo myservo1; Servo myservo2; // create servo object to control a servo int pos = 0; // variable to store the servo position
void setup() { myservo.attach(9); myservo1.attach(10); myservo2.attach(11); // attaches the servo on pin 9 to the servo object // syntaxf 21c
int sensorPin = 3; //pin that the input fron LED int val = low; if (sensorPin == high) // if signal is high on 3 then run following void setup () { for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(10); myservo1.write(pos); delay(10); myservo2.write(pos); delay(10); // waits 15ms for the servo to reach the position }
if (sensorPin == low) void setup () { for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(10); myservo1.write(pos); delay(10); myservo2.write(pos); delay(10); // waits 15ms for the servo to reach the position } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 242
Posts: 16471
Available for Design & Build services
|
 |
« Reply #1 on: June 03, 2011, 01:25:11 am » |
Format-wise, take out the 2nd & 3rd void setup() add pinMode (sensorPin, INPUT); // make sure the sensor can actually drive Lo & High // might have to change to an analogRead on an analog pin and make decisions based on value
void loop(){ // now read the sensor, change the if statement to: if (digitalRead(sensorPin)== HIGH){
after it says int val = low. add } at the end to close out void loop(){
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19011
I don't think you connected the grounds, Dave.
|
 |
« Reply #2 on: June 03, 2011, 01:41:16 am » |
The visual difference between this for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(10); myservo1.write(pos); delay(10); myservo2.write(pos); delay(10); // waits 15ms for the servo to reach the position }
And this: for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 179 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' myservo1.write(pos); myservo2.write(pos); delay(10); // waits at least 10ms for the servo to reach the position } will be minimal. Did you realise that you don't need Arduino hardware to run the Arduino IDE? You could have got your sketch to at least compile.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #3 on: June 03, 2011, 02:32:15 am » |
Does this look correct? Im new to this program..it may help me alot if i could see the whole layout with the fixtures included. #include <Servo.h> Servo myservo; Servo myservo1; Servo myservo2; // create servo object to control a servo int pos = 0; // variable to store the servo position
void setup() { myservo.attach(9); myservo1.attach(10); myservo2.attach(11); // attaches the servo on pin 9 to the servo object
pinMode (sensorPin, INPUT); Void loop () { if (digitalRead(sensorPin)== HIGH){// if signal is high on 3 then run following int val = low; } { for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' myservo1.write(pos); myservo2.write(pos); delay(10); // waits 10ms for the servo to reach the position }
if (digitalRead(sensorPin)== low) { for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' myservo1.write(pos); myservo2.write(pos); delay(10); // waits 10ms for the servo to reach the position } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19011
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: June 03, 2011, 02:39:51 am » |
Install the Arduino IDE, open it, paste your code into it, and click on "verify". Work through the error messages. Humans are not good compilers. Remember that C is case-sensitive. if (digitalRead(sensorPin)== HIGH){// if signal is high on 3 then run following int val = low; } Three things here: You haven't defined "sensorPin", "low" is spelled "LOW", and "val" is a new variable that you have defined assigned a value to and are going to destroy in the next few nanoseconds. Lose the ""int" off the front. for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees The loop goes from 0 to 179, not 180.
|
|
|
|
« Last Edit: June 03, 2011, 02:43:55 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5915
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #5 on: June 03, 2011, 10:39:09 pm » |
gabe,
You should try something that fits your programming skills. I suggest the tutorials. You're either trying to program with brute force/hodgepodge or end up relying on others to spoon feed you with code, neither of which works for you. There are lots of mistakes in your original code that beginners can pick out after a whole day of learning.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #6 on: June 03, 2011, 11:13:29 pm » |
okay im trying my best to figure this out, and im making changes as a beginner.. and much help is needed.
#include <Servo.h> Servo myservo; Servo myservo1; Servo myservo2; // create servo object to control a servo int pos = 0; // variable to store the servo position
void setup() { myservo.attach(9); myservo1.attach(10); myservo2.attach(11); // attaches the servo on pin 9 to the servo object // syntaxf 21c
pinMode (sensorPin = INPUT); //pin that the input fron LED val = LOW; } if (digitalRead(sensorPin) == HIGH) // if signal is high on 3 then run following } for(pos = 0; pos < 179; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' myservo1.write(pos); myservo2.write(pos); delay(10); // waits 10ms for the servo to reach the position }
if (sensorPin == LOW) { for(pos = 179; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' myservo1.write(pos); myservo2.write(pos); delay(10); // waits 10ms for the servo to reach the position } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19011
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: June 04, 2011, 03:41:56 am » |
pinMode (sensorPin = INPUT); //pin that the input fron LED val = LOW;
Why aren't you at least trying to compile this, or at least reading the reference material provided? http://arduino.cc/en/Reference/PinMode"val" hasn't been defined, so assigning the value LOW to it is impossible. myservo2.attach(11); // attaches the servo on pin 9 to the servo object Clearly, it does no such thing. Any clues as to what this comment // syntaxf 21c means? What is it you are trying to achieve here? If you compiled this stuff, the compiler would give you errors, you would learn the meaning of those errors and move on and correct the code. This is wasting time.
|
|
|
|
« Last Edit: June 04, 2011, 03:45:26 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5915
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #8 on: June 04, 2011, 07:25:50 am » |
I don't know what the OP is trying to achieve by ignoring AWOL's suggestion to simply paste the code in Arduino IDE and hit compile. It's not helping. Please respect people's comment if you want to get help.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #9 on: June 04, 2011, 10:42:06 am » |
One final comment: Servo myservo; Servo myservo1; Servo myservo2; // create servo object to control a servo
If you were counting objects, would you count 1, 2, 3, or nothing, 1, 2?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19011
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: June 04, 2011, 04:12:53 pm » |
If you were counting objects, would you count 1, 2, 3, or nothing, 1, 2? Well, I've been a programmer for quite some time, so I'd count zero, one, two, obviously. 
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #11 on: June 14, 2011, 01:48:51 am » |
ok...im still playing with this code...and applying the feedback im receiving yet im still confused...so im tryin once again...this is probably wrong but need me some correction here...help a buddy out..
#include <Servo.h>
Servo myservo; Servo myservo1; Servo myservo2; //create servo object to control a servo int pos = 0 ; // variable to store the servo object // these constants won't change: const int sensorMin = 0; // sensor minimum, discovered through experiment const int sensorMax = 600; // sensor maximum, discovered through experiment
void setup() { // initialize serial communication: Serial.begin(9600); myservo.attach(9); //attaches the servo to pin 9 myservo1.attach(10); // attaches the servo to pin 10 myservo2.attach(11); // attaches the servo to pin 11 }
void loop() { // read the sensor: int sensorReading = analogRead(A0); // map the sensor range to a range of four options: int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// do something different depending on the // range value: switch (range) { case 0: // your hand is on the sensor Serial.println("dark"); break; case 1: // your hand is close to the sensor Serial.println("dim"); break; case 2: // your hand is a few inches from the sensor Serial.println("medium"); break; case 3: // your hand is nowhere near the sensor Serial.println("bright"); break; for(pos = 0; pos < 179; pos +=1) //goes from 0 to 179 degree { myservo.write(pos); myservo1.write(pos); myservo2.write(pos); delay(10); //wait 10ms for the servo to move } for (pos = 179; pos>=1; pos-=1) { myservo.write(pos); myservo1.write(pos); myservo2.write(pos); delay(10); } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19011
I don't think you connected the grounds, Dave.
|
 |
« Reply #12 on: June 14, 2011, 01:51:14 am » |
You need to tell us what the problem is.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #13 on: June 14, 2011, 02:02:40 am » |
i am compiling this and towards the end: i am receiving expected '}' at end of input in function 'void loop () ': error: expected ' } ' at end of input
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19011
I don't think you connected the grounds, Dave.
|
 |
« Reply #14 on: June 14, 2011, 02:04:18 am » |
So, you need to match up your opening braces "{" with your closing braces "}". That's what the compiler is telling you.
(Hint: What closes your "switch"?)
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
|