Pages: [1] 2   Go Down
Author Topic: need some major help with project...  (Read 1220 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 446
Posts: 23872
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Format-wise, take out the 2nd & 3rd
void setup()

add
Code:
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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The visual difference between this
Code:
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:
Code:
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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 Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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.

Code:
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6944
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.

Code:
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
Code:
// 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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6944
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 553
Posts: 46299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One final comment:
Code:
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 Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

 smiley-cool
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1] 2   Go Up
Jump to: