Arduino kod

Elektronik- och mekanikrelaterad mjukvara/litteratur. (T.ex schema-CAD, simulering, böcker, manualer mm. OS-problem hör inte hit!)
Användarvisningsbild
GhostLT
Inlägg: 155
Blev medlem: 14 juli 2009, 18:16:22

Arduino kod

Inlägg av GhostLT »

Har lite problem

Har denna kod
Men behöver anpassa den så jag kan köra 5 st servon och potentiometers
helst så jag kan ställa ms på servon med
Arduino Duemilanove port a0-a5 d0-d13

Skulle vara bra om man kan ställa ms på eftersläpet på dom med.

Potentiometern ska vara styr del så när den rörs så ska servon kompensera


Men är dålig på att programmera dessa så lite hjälp skulle vara bra
Har arduino-0017 programvara

Kod: Markera allt


// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> 

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 

void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
}
Hittade denna med

Kod: Markera allt

Servo control from an analog input

The minimum (minPulse) and maxiumum (maxPuluse) values
will be different depending on your specific servo motor.
Ideally, it should be between 1 and 2 milliseconds, but in practice,
0.5 - 2.5 milliseconds works well for me.
Try different values to see what numbers are best for you.

This program uses the millis() function to keep track of when the servo was
last pulsed.  millis() produces an overflow error (i.e. generates a number
that's too big to fit in a long variable) after about 5 days. if you're
making a program that has to run for more than 5 days, you may need to
account for this.

by Tom Igoe
additions by Carlyn Maw
Created 28 Jan. 2006
Updated 7 Jun. 2006
*/

int servoPin = 2;     // Control pin for servo motor
int minPulse = 500;   // Minimum servo position
int maxPulse = 2500;  // Maximum servo position
int pulse = 0;        // Amount to pulse the servo

long lastPulse = 0;    // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses

int analogValue = 0;  // the value returned from the analog sensor
int analogPin = 0;    // the analog pin that the potentiometer's on

void setup() {
 pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
 pulse = minPulse;           // Set the motor position value to the minimum
 Serial.begin(9600);
}

void loop() {
 analogValue = analogRead(analogPin);      // read the analog input
 pulse = (analogValue * 19) / 10 + minPulse;    // convert the analog value
                                           // to a range between minPulse
                                           // and maxPulse.

 // pulse the servo again if rhe refresh time (20 ms) have passed:
 if (millis() - lastPulse >= refreshTime) {
   digitalWrite(servoPin, HIGH);   // Turn the motor on
   delayMicroseconds(pulse);       // Length of the pulse sets the motor position
   digitalWrite(servoPin, LOW);    // Turn the motor off
   lastPulse = millis();           // save the time of the last pulse
 }
} 
Senast redigerad av GhostLT 9 november 2009, 20:45:59, redigerad totalt 1 gång.
Användarvisningsbild
xraid
Inlägg: 1129
Blev medlem: 20 mars 2009, 04:12:14
Ort: Stockholm

Re: Arduino

Inlägg av xraid »

Jag förstår inte riktigt - du vill styra 5 servon med 5 potentiometrar ?

Rubriken borde spegla servo kod relaterat ...

i Arduino-17 IDE så finns ett exempel i meny : Open->Servo->Knob

Kod: Markera allt

// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> 

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
} 
enklare kan det väl knappast bli - gör två loopar som sätter portarna bara ... men jag kanske inte riktigt förstått vad ditt problem är ...
Användarvisningsbild
GhostLT
Inlägg: 155
Blev medlem: 14 juli 2009, 18:16:22

Re: Arduino kod

Inlägg av GhostLT »

Behöver


Försökte lite men det funkade inte som jag ville.
Testade lite men fick inte till det med 2 loopar

5 servon 5 potentiometers individuella

Så undrar om någon kan gör ett exempel med 2-3 servo loopar med den där koden
Användarvisningsbild
xraid
Inlägg: 1129
Blev medlem: 20 mars 2009, 04:12:14
Ort: Stockholm

Re: Arduino kod

Inlägg av xraid »

i servo biblioteket finns dessa Extra metoder nu : (http://www.arduino.cc/playground/ComponentLib/Servo)

Extra Methods
-refresh()
You must call this at least once every 50ms to keep the servos updated. You can call it as often as you like, it won't fire more than once every 20ms. When it does fire, it will take from .5 to 2.5 milliseconds to complete, but won't disable interrupts.

-setMinimumPulse(uint16_t)
set the duration of the 0 degree pulse in microseconds. (default minimum value is 544 microseconds)

-setMaximumPulse(uint16_t)
set the duration of the 180 degree pulse in microseconds. (default maximum pluse value is 2400 microsconds)

Kod: Markera allt

#include <SoftwareServo.h> 

SoftwareServo myservo;  // create servo object to control a servo 

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 

void setup() 
{ 
  myservo.attach(2);  // attaches the servo on pin 2 to the servo object 
} 

void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 

  SoftwareServo::refresh();
Användarvisningsbild
xraid
Inlägg: 1129
Blev medlem: 20 mars 2009, 04:12:14
Ort: Stockholm

Re: Arduino kod

Inlägg av xraid »

i servo biblioteket finns dessa Extra metoder nu : (http://www.arduino.cc/playground/ComponentLib/Servo)

Extra Methods
-refresh()
You must call this at least once every 50ms to keep the servos updated. You can call it as often as you like, it won't fire more than once every 20ms. When it does fire, it will take from .5 to 2.5 milliseconds to complete, but won't disable interrupts.

-setMinimumPulse(uint16_t)
set the duration of the 0 degree pulse in microseconds. (default minimum value is 544 microseconds)

-setMaximumPulse(uint16_t)
set the duration of the 180 degree pulse in microseconds. (default maximum pluse value is 2400 microsconds)

Kod: Markera allt

#include <SoftwareServo.h> 

SoftwareServo myservo;  // create servo object to control a servo 

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 

void setup() 
{ 
  myservo.attach(2);  // attaches the servo on pin 2 to the servo object 
} 

void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 

  SoftwareServo::refresh();
}
Användarvisningsbild
xraid
Inlägg: 1129
Blev medlem: 20 mars 2009, 04:12:14
Ort: Stockholm

Re: Arduino kod

Inlägg av xraid »

nåt sånt här kanske . lek med setMinimumPulse / setMaximumPulse

ändra forloopens i(ndex) till portarna du vill använda = for(int i = 7; i < 11; i++) {

Kod: Markera allt

SoftwareServo myservos[4];  // create servo objects 

int potpin[4] = 1,2,3,4,5;

int val; 

void setup() 
{ 
  for(int i = 0;  i < 4; i++) {
    myservos[i].attach(i);  // attaches the servos on pin "i" to the servo objects

    myservos[i].setMinimumPulse(700)
    //set the duration of the 0 degree pulse in microseconds. (default minimum value is 544 microseconds) 

    myservos[i].setMaximumPulse(1400)
    //set the duration of the 180 degree pulse in microseconds. (default maximum pluse value is 2400 microsconds)
  }
}

void loop() 
{ 
  for(int i = 0;  i < 4; i++) {
    val = analogRead(potpin[i]);            
    val = map(val, 0, 1023, 0, 179);     
    myservos[i].write(val);                     
    delay(15);                                      
  }
   SoftwareServo::refresh();
}
Användarvisningsbild
xraid
Inlägg: 1129
Blev medlem: 20 mars 2009, 04:12:14
Ort: Stockholm

Re: Arduino kod

Inlägg av xraid »

oj det ekar härinne ;-)

det finns ett prob med

Kod: Markera allt

for(int i = 0;  i < 4; i++) {
    myservos[i].attach(i);  // attaches the servos on pin "i" to the servo objects
men det löser du ...
Skriv svar