Servo doesn't want to move and back when I combine 2 files in 1

I have a code, the neopixels are in order and when I push the button, the servo should move. But there is a problem when I combine the 2 codes in 1 file, the servo doesn't want to work properly. Can someone help me please? video servo not working properly while neopixel is attached - YouTube

#include <Adafruit_NeoPixel.h> // importeren vanuit bibliotheek

Adafruit_NeoPixel tira = Adafruit_NeoPixel(8, 5, NEO_GRB + NEO_KHZ800); // random naam geven; hier "tira"

#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
int button = 3; // The button will be on Pin 7

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(pos, OUTPUT);
pinMode(button, INPUT);
digitalWrite (button, LOW);
tira.begin(); // inicializacion de la tira
tira.show(); // muestra datos en pixel

if (digitalRead(button) == LOW)

for(pos = 0; pos < 90; pos += 90) // goes from 0 degrees to 90 degrees
{ // in steps of degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
// waits 1s for the servo to reach the position
}
if (digitalRead(button) == HIGH)

for(pos = 90; pos>=90; pos-=90) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 50ms for the servo to reach the position
}
myservo.detach();
}
void loop()
{

tira.setBrightness(10);
for(int i = 0; i < 8; i++){ //
tira.setPixelColor(i, 0, 255, 0);
tira.show();
delay(500);
tira.setPixelColor(i, 0, 0, 0); /
tira.show();
}
}

Please edit your post to add code tags, as described in How to get the best out of the forum.

Remove this line, it does nothing for you and may interfere with Serial output.

pinMode(pos, OUTPUT);

Some Arduino libraries conflict and can not be used together. I believe that the neopixel and servo libraries are among them. You may find this post helpful: The Issue | Using NeoPixels and Servos Together | Adafruit Learning System

1 Like

All of your servo motion is in setup() so once setup is done and loop() starts repeating the servo will never move again.

Also, the Arduino can't run your sketch while it is in a delay() so you are going to have to redesign the two sketches to make them look like they are happening simultaneously.

Note: This 'for loop' only has one step:

    for (pos = 0; pos < 90; pos += 90) // goes from 0 degrees to 90 degrees
    {
      // in steps of degree
      myservo.write(pos); // tell servo to go to position in variable 'pos'
      // waits 1s for the servo to reach the position
    }

It is entirely equivalent to:

    pos = 0;
    myservo.write(pos); // tell servo to go to position in variable 'pos'

The other 'for loop' is similarly identical to:

    pos = 90;
    myservo.write(pos); // tell servo to go to position in variable 'pos'
1 Like

Hi johnwasser,
What should I change to redesign the code so that it goes simultaneously? I'm a newbie at arduino and I'm really dissapointed that I can't get what I initially want.

This is the code without the neopixel led strip and it works perfectly, when you push the button, the servo moves and comes back:

#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
int button = 3; // The button will be on Pin 7

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(pos, OUTPUT);
pinMode(button, INPUT);
digitalWrite (button, LOW);
}

void loop()
{

if (digitalRead(button) == LOW)

for (pos = 0; pos < 90; pos += 90) // goes from 0 degrees to 90 degrees
{ // in steps of degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
// waits 1s for the servo to reach the position
}
if (digitalRead(button) == HIGH)

for(pos = 90; pos>=90; pos-=90) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 50ms for the servo to reach the position
}

}

The next code is of the neopixel and it also works perfectly alone:

#include <Adafruit_NeoPixel.h> // importeren vanuit bibliotheek

Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, 5, NEO_GRB + NEO_KHZ800);

void setup(){
strip.begin();
strip.show();
}

void loop(){
strip.setBrightness(10);
for(int i = 0; i < 8; i++)
{
strip.setPixelColor(i, 0, 255, 0);
strip.show();
delay(500);
strip.setPixelColor(i, 0, 0, 0);
strip.show();
}
}

Hi jremington,

I tried like you said the library 'TicoServo'. Unfortunately, it's not working :confused:

include <Adafruit_NeoPixel.h>
#include <Adafruit_TiCoServo.h>
#define SERVO_PIN 9

Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, 6);
Adafruit_TiCoServo myservo;

int pos = 0; // variable to store the servo position
int button = 3; // The button will be on Pin 3

void setup(void) {
myservo.attach(9);
strip.begin();
pinMode(pos, OUTPUT);
pinMode(button, INPUT);
digitalWrite (button, LOW);

}

void loop(void) {
if (digitalRead(button) == LOW)

for(pos = 0; pos < 90; pos += 90) // goes from 0 degrees to 90 degrees
{ // in steps of degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
// waits 1s for the servo to reach the position
}
if (digitalRead(button) == HIGH)

for(pos = 90; pos>=90; pos-=90) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 50ms for the servo to reach the position
}

strip.setBrightness(10); // Helderheid
for(int i = 0; i < 8; i++){ //
strip.setPixelColor(i, 0, 255, 0); // elke pixel in kleur (positie led, R, G, B)
strip.show(); // geeft gegevens weer in pixels
delay(500); // korte vertraging van een halve seconde (500ms)
strip.setPixelColor(i, 0, 0, 0); // zet de pixel uit
strip.show(); // data naar pixel
}

}

With no working I meant; If I push the button, there is no response from the servo. When I push for a very long time, it rotates from 0 to 90 degrees. Normally the servo should rotate from 0 to 90 degrees and back from 90 to 0 degrees with 1 push. Now because of the addition of neopixel, it's not working properly. Even though I changed it to the Ticoservo library, the servo is not working properly

The loop() function in your working servo sketch can be simplified to:

void loop()
{
  if (digitalRead(button) == LOW)
    myservo.write(0);
  else
    myservo.write(90);
}

The reason your button is unresponsive when you combine it with the LED sketch is that the LED sketch takes 4 seconds to run a cycle (delay(500) eight times). A way to work around that problem is to replace "delay(500);" with a loop that does nothing but check the button repeatedly for 500 milliseconds:

#include <Servo.h>
#include <Adafruit_NeoPixel.h> // importeren vanuit bibliotheek

Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, 5, NEO_GRB + NEO_KHZ800);

Servo myservo;

const int ButtonPin = 3;
const int ServoPin = 9;

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

  pinMode(ButtonPin, INPUT);  // Requires external pull-up or pull-down resistor!

  strip.begin();
  strip.show();
}

void checkButton()
{
  if (digitalRead(ButtonPin) == LOW)
    myservo.write(0);
  else
    myservo.write(90);
}

void loop()
{
  strip.setBrightness(10);
  for (int i = 0; i < 8; i++)
  {
    strip.setPixelColor(i, 0, 255, 0);
    strip.show();
    unsigned long start = millis();
    while (millis() - start <= 500)
      checkButton();
    strip.setPixelColor(i, 0, 0, 0);
    strip.show();
  }
}
1 Like

Thank you so much!