Help with Coding the servos

Hi there cool people :slight_smile:

I am very new to this so I have a favour to ask :slight_smile:
I have this idea for my project that works with Neopixel lights and Servos.
So far I coded the Neopixels lights that works with what I need. But the servos I have trouble coding them to work, I need 2 of them turns together 90° to the left and 90° to the right with push of the same button, the ons with the pin nr 10 & 11, both are Micro Servos 9g FS90. And the one with pin nr 9 to 90° also with a push of a button and to 0° with pushing it again, FS5106R (this I might have to change to only 180° servo). The last one I need it to turn a full circle also with also push of a button then to 0° with pushing it again, Continues servo FS90R.

I hope theres some one can help me with the coding I would really appreciate it :slight_smile:

I attached the coding for the Neopixel and the sketch of what I have in my mind. Let me know if the sketch is wrong and how to fix it or i should change anything :slight_smile:

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      60
#define TRACE_LENGTH   5

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 25; // delay for half a second

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

  int i = 0;
  while(true) {

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(255,40,0)); // Orange color.
    pixels.setPixelColor(i - TRACE_LENGTH, pixels.Color(255,0,0)); // Bright red color.

    //pixels.setPixelColor(i, pixels.Color(0,90,255)); // Light blue color.
    //pixels.setPixelColor(i - TRACE_LENGTH, pixels.Color(0,0,255)); // Bright blue color.

    //pixels.setPixelColor(i, pixels.Color(0,150,50)); // Light blue color.
    //pixels.setPixelColor(i - TRACE_LENGTH, pixels.Color(0,255,0)); // Bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).
    i = (i + 1) % (NUMPIXELS + TRACE_LENGTH);
  }
}

The last one I need it to turn a full circle also with also push of a button then to 0° with pushing it again, Continues servo FS90R.

You may need to rethink your project. You can NOT control the position of a continuous rotation no-longer-a-servo. You can only control it's speed.

You also need to explain why you think you need an infinite loop in an infinite loop().

Thank you for replying :slight_smile:

So you think its better if i change it to a DC motor? would that work?

And i dont understand what you mean by infinite loop!!??

The loop() function is run repeatedly, hence its name. Why do you need the while(true) loop within it ?

 while(true) {

infinite loop (no break)

Boomerango:
The last one I need it to turn a full circle also with also push of a button then to 0° with pushing it again, Continues servo FS90R.

If you mean "rotate 360 degrees in one direction when the button is pressed and rotate 360 degrees back when the button is pressed again" you would need a regular servo and either gears or pulleys to multiply a 180 degree motion by 2 to get a 360 degree motion.

A "continuous rotation servo" has no position feedback so it can't be directed to any particular position. Think of it as a geared DC motor with a built-in speed controller.

Similarly, a DC motor has no position feedback or speed controller.

If you really want 360 degrees or more controlled movement then another possibility is a sail winch servo. These have full positional control over usually 3 complete turns or more.

Steve

Yes but the thing is i need it to be not that big of a servo or motor.
I found two ideas on youtube.
This one is using 180° servo. But the idea of 2 push buttons is what i have in mind.

And this one is using a continuas rotating servo, also what i have in mind but he uses one button.

So the first one with 2 buttons L&R rotation and the second one with continuas rotation.
I tried them both they work, but i dont know how to let it stop when i lift my finger from the button.

Any ideas are welcome :slight_smile:

I am really weak on programming as you can see, trying to figure it out XD

Boomerango:
And this one is using a continuas rotating servo, also what i have in mind but he uses one button.

https://www.youtube.com/watch?v=TrLRSGp8HJw&frags=pl%2Cwn

That example has little to do with the programming and everything to do with wiring the button as an on/off switch for the motor. When the button is not pressed the motor is not moving because it has no power. When the button is pressed the motor spins in some direction at some speed that depends entirely on where the Sweep example is in its loops. The Sweep example is not intended to control a 'continuous rotation' servo. If it were it would be written:

#include <Servo.h>


Servo myMotor;  // create servo object to control a 'continuous rotation' servo
// A.K.A.  A gearmotor with a speed controller built in


int speed = 0;    // variable to store the servo speedition


const int FullSpeedReverse = 0;
const int FullSpeedForward = 180;


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


void loop()
{
  // tell the motor to spin full speed backward, slow down to a stop, and 
  // then speed up to full speed forward
  for (speed = FullSpeedReverse; speed <= FullSpeedForward; speed += 1)
  {
    myMotor.write(speed);
    delay(15);
  }


  // tell the motor to spin full speed forward, slow down to a stop, and 
  // then speed up to full speed backward
  for (speed = FullSpeedForward; speed >= FullSpeedReverse; speed -= 1)
  {
    myMotor.write(speed);
    delay(15);
  }
}