servo shield/Rf reciever/Rf remote coding help

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
sassmaster
 
Posts: 7
Joined: Tue May 29, 2018 11:01 am

servo shield/Rf reciever/Rf remote coding help

Post by sassmaster »

This is my trial by fire sorta speak, so my first project is to control 4 servos which i have hooked up to the shield and to control them with ada 4-button remote with the momentary receiver. The servos will basically fully open with one button and close with another, I have tested the board with the pmw servo library and everything is all good same with the Rf receiver/remote. I just don't understand how to correctly put everything together and work properly.

~Thanks for reading!!!

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: servo shield/Rf reciever/Rf remote coding help

Post by Franklin97355 »

Post your code as you have it and explain what is not working. Please use code tags when posting code or logs to the forums. It preserves formatting and makes it easier for everyone to read the code. Click the code button above the reply box and past your code between the tags created.

User avatar
sassmaster
 
Posts: 7
Joined: Tue May 29, 2018 11:01 am

Re: servo shield/Rf reciever/Rf remote coding help

Post by sassmaster »

Code: Select all

/*************************************************** 
  This is an example for our Adafruit 16-channel PWM & Servo driver
  Servo test - this will drive 8 servos, one after the other on the
  first 8 pins of the PCA9685

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/products/815
  
  These drivers use I2C to communicate, 2 pins are required to  
  interface.

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// you can also call it with a different address and I2C interface
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(&Wire, 0x40);

// Depending on your servo make, the pulse width min and max may vary, you 
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
#define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)

// our servo # counter
uint8_t servonum = 0;
int pos = 0;    // variable to store the servo position
void setup() {
  Serial.begin(9600);
  Serial.println("8 channel Servo test!");

  pwm.begin();
  
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

  delay(10);
}

// you can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
void setServoPulse(uint8_t n, double pulse) {
  double pulselength;
  
  pulselength = 1000000;   // 1,000,000 us per second
  pulselength /= 60;   // 60 Hz
  Serial.print(pulselength); Serial.println(" us per period"); 
  pulselength /= 4096;  // 12 bits of resolution
  Serial.print(pulselength); Serial.println(" us per bit"); 
  pulse *= 1000000;  // convert to us
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
}

void loop() {
  // Sweep the servos
  for (uint16_t pulselen = 0; pulselen < SERVOMAX-SERVOMIN; pulselen++) {
    for (int servonum = 0; servonum<8; servonum++)
        pwm.setPWM(servonum, 0, SERVOMIN+pulselen);
    for (int servonum = 8; servonum<16; servonum++)
        pwm.setPWM(servonum, 0, SERVOMAX-pulselen);
 }
  delay(150);
}
franklin97355 wrote:Post your code as you have it and explain what is not working. Please use code tags when posting code or logs to the forums. It preserves formatting and makes it easier for everyone to read the code. Click the code button above the reply box and past your code between the tags created.
I got the servos working together but need them to move at the same speed as they are going at the end of the loop. For the remote i only used the digtal button example that came with to test it, i dont have code for it to work for the servos

User avatar
sassmaster
 
Posts: 7
Joined: Tue May 29, 2018 11:01 am

Re: servo shield/Rf reciever/Rf remote coding help

Post by sassmaster »

So i managed to work out one servo being controlled with the rf remote/receiver without the shield being attached. Now i'm getting a problem of a long delay after i press the button, sometimes it is very quick and other time it is long. Here is the code im using

Code: Select all

#include <Servo.h>


int  A = 8; // button A input
int  B = 7; // button D input

int  servoPin = 9;

int angle = 1;
int change = 85; // this value determines how much the angle changes each time through the loop

Servo myservo;

void setup(){

 pinMode(A, INPUT);  // initialize pins
 pinMode(B, INPUT);

 
 digitalWrite(A, LOW); // set internal pull up resistors
 digitalWrite(B, LOW);
 
 myservo.attach(servoPin);
}

void loop(){

 if( digitalRead(A) == HIGH) { 
   // here if increment switch pressed
   angle = angle + change;
 }
 if( digitalRead(B) == HIGH) {
   // here if decrement switch pressed
   angle = angle - change;
 }  
 
   
 angle = constrain(angle, 5, 170); // limit value of angle 
 myservo.write(angle);
 delay(20); 
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”