How can i combine these two codes?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ccmacho21
    New Member
    • Feb 2014
    • 1

    How can i combine these two codes?

    the first code is for a muscle sensor kit, i need the code to say if the value of i is > 275 then move to the right and if i is < 275 move to the left. the servo motor will be moving right or left depending on i. i need help combining the two code into what i want! THANK YOU

    servo motor code:

    Code:
    #include <Servo.h> 
     
    Servo myservo;  // create servo object to control a servo 
                    // a maximum of eight servo objects can be created 
     
    int pos = 0;    // variable to store the servo position 
     
    void setup() 
    { 
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
    } 
     
     
    void loop() 
    { 
      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(15);                       // waits 15ms for the servo to reach the position 
      } 
      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(15);                       // waits 15ms for the servo to reach the position 
      } 
    }


    Muscle sensor code:

    Code:
    // reads analog input from the five inputs from your arduino board 
    // and sends it out via serial
    
    // variables for input pins and
    int analogInput[6];
      
    // variable to store the value 
    int value[6]; 
     
    void setup()
    {
      // declaration of pin modes
      for(int i=0;i<6;i++)
      {
        analogInput[i] = i+1;
        value[i] = 0;     
        pinMode(analogInput[i], INPUT);    
      }
      
      // begin sending over serial port
      Serial.begin(9600);
    }
    
    void loop()
    {
      // read the value on analog input
      for(int i=0;i<6;i++)
      {
        value[i] = analogRead(analogInput[i]);
      }
    
      // print out value over the serial port
      for(int i=0;i<6;i++)
      {
        Serial.println(0 + i + 1); //prefix
        Serial.println(value[i]);
      }
      // wait for a bit to not overload the port
      delay(500);
    }
    Last edited by Banfa; Feb 4 '14, 01:11 PM. Reason: Added [code]...[/code] tags
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi ccmacho21 and welcome to bytes.com!

    What exactly is it you need help with? You have two sets of code, probably from different C files. (They should be in [code]...[/code] tags btw, that makes them much easier to read.) You have an idea of what they should do and what you want to do with them. But which i value are you referring to? There are several is just in the muscle sensor code. Also, can you change these files or do you have to use them as they are and build something around them?

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      The most obvious issue to having all this code in the same program is that they both defined the symbols setup and loop (as functions). You will need to give these functions unique names e.g. servoSetup and servoLoop, in order to include them into the same program.

      Comment

      Working...