Go Down

Topic: need some major help with project... (Read 1 time) previous topic - next topic

gabe

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
  }
}

CrossRoads

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

add
Code: [Select]

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(){
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AWOL

The visual difference between this
Code: [Select]
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: [Select]
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.
"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.

gabe


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
  }
}

AWOL

#4
Jun 03, 2011, 09:39 am Last Edit: Jun 03, 2011, 09:43 am by AWOL Reason: 1
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: [Select]
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: [Select]
for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
The loop goes from 0 to 179, not 180.
"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.

liudr

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.

gabe

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
  }
}

AWOL

#7
Jun 04, 2011, 10:41 am Last Edit: Jun 04, 2011, 10:45 am by AWOL Reason: 1
Code: [Select]
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: [Select]
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: [Select]
// 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.
"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.

liudr

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.

PaulS

One final comment:
Code: [Select]
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?

AWOL

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.

8)
"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.

gabe

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);
    }
}

AWOL

You need to tell us what the problem is.

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

gabe

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

AWOL

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"?)
"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.

Go Up