please help me correct this program

This is a discussion on please help me correct this program within the C Programming forums, part of the General Programming Boards category; Code: #include <servo.h> const int servo1 = 3; // first servo const int servo2 = 10; // second servo const ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    3

    Unhappy please help me correct this program

    Code:
    #include <servo.h>
     
    const int servo1 = 3;       // first servo
    const int servo2 = 10;       // second servo
    const int joyH = 4;        // Joystick Horizontal Axis
    const int joyV = 5;        // Joystick vertical Axis
     
    int servoVal;           // variable to read the value from the analog pin
     
    Servo myservo1;  // create servo object to control a servo
    Servo myservo2;  // create servo object to control a servo
     
     
     
    void setup() {
     
      // Servo
      myservo1.attach(servo1);  // attaches the servo
      myservo2.attach(servo2);  // attaches the servo
     
      // Inizialize Serial
      Serial.begin(9600);
    }
     
     
    void loop(){
     
        // Display Joystick values using the serial monitor
        outputJoystick();
     
        // Read the horizontal joystick value  (value between 0 and 1023)
        servoVal = analogRead(joyH);
        servoVal = map(servoVal, 0, 1023, 0, 180);     // scale it to use it with the servo (result  between 0 and 180)
     
        myservo2.write(servoVal);                               // sets the servo position according to the scaled value
     
        // Read the horizontal joystick value  (value between 0 and 1023)
        servoVal = analogRead(joyV);
        servoVal = map(servoVal, 0, 1023, 70, 180);     // scale it to use it with the servo (result between 70 and 180)
     
        myservo1.write(servoVal);                              // sets the servo position according to the scaled value
     
        delay(15);                                             // waits for the servo to get there
     
    }
     
     
    /**
    * Display joystick values
    */
    void outputJoystick(){
     
        Serial.print(analogRead(joyH));
        Serial.print ("---");
        Serial.print(analogRead(joyV));
        Serial.println ("----------------");
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Correct how?

    You don't say what is "wrong".
    You don't say what would be considered "right".

    You don't specify a main() to call it.
    You don't specify what kind of device you're talking to - other than it seems to imply a servo and a joystick.

    You don't document (or provide links to) what analogRead() and map() do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    3

    sorry

    sorry this program is for arduino uno its meant to control 2 servos via a thumbstick
    Code:
    #include <servo.h>  
    const int servo1 = 3;       // first servo
    const int servo2 = 10;       // second servo
    const int joyH = 4;        // Joystick Horizontal Axis
    const int joyV = 5;        // Joystick vertical Axis
      
    int servoVal;           // variable to read the value from the analog pin
    (its says servo does not name a type)  
    Servo myservo1;  // create servo object to control a servo
    Servo myservo2;  // create servo object to control a servo
      
      
      
    void setup() {
      
      // Servo
      myservo1.attach(servo1);  // attaches the servo
      myservo2.attach(servo2);  // attaches the servo
      
      // Inizialize Serial
      Serial.begin(9600);
    }
      
      
    void loop(){
      
        // Display Joystick values using the serial monitor
        outputJoystick();
      
        // Read the horizontal joystick value  (value between 0 and 1023)
        servoVal = analogRead(joyH);
        servoVal = map(servoVal, 0, 1023, 0, 180);     // scale it to use it with the servo (result  between 0 and 180)
      
        myservo2.write(servoVal);                               // sets the servo position according to the scaled value
      
        // Read the horizontal joystick value  (value between 0 and 1023)
        servoVal = analogRead(joyV);
        servoVal = map(servoVal, 0, 1023, 70, 180);     // scale it to use it with the servo (result between 70 and 180)
      
        myservo1.write(servoVal);                              // sets the servo position according to the scaled value
      
        delay(15);                                             // waits for the servo to get there
      
    }
      
      
    /**
    * Display joystick values
    */
    void outputJoystick(){
      
        Serial.print(analogRead(joyH));
        Serial.print ("---");
        Serial.print(analogRead(joyV));
        Serial.println ("----------------");
    }

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Let me repeat what Salem said:

    Correct how?

    You don't say what is "wrong".
    You don't say what would be considered "right".

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Arduino - Sweep

    TBH, you would probably be better off asking on Index - Arduino Forum, where they're likely to know if there is any particular trick involved.
    All we can do is read the manual (just like you), and maybe make a slightly better guess (but still a guess) as to what is wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    3
    i'll try that any ways thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. correct this program anyone please....
    By jothimani1991 in forum C Programming
    Replies: 3
    Last Post: 01-08-2013, 05:49 PM
  2. MD5 program not giving correct hashes.
    By Syscal in forum C Programming
    Replies: 4
    Last Post: 04-30-2012, 04:11 AM
  3. another little program: is this correct?
    By fsx in forum C Programming
    Replies: 7
    Last Post: 05-19-2009, 07:50 AM
  4. is this simple program correct? K&R 1-12
    By fsx in forum C Programming
    Replies: 14
    Last Post: 04-22-2009, 03:04 PM
  5. Is this program correct ?
    By broli86 in forum C Programming
    Replies: 9
    Last Post: 08-02-2008, 06:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21