Blink Without Delay - Adafruit LED and Servo

Hi!

I just cant make this code to work, what am I doing wrong? I whant the serve to run at the same time as the LED-code runs.

BR Brandon Lane

#include <Servo.h>

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

#define PIN 2
#define NUMPIXELS 1

Adafruit_NeoPixel led_strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
Servo servo;

int val;

unsigned long previousMillis_1 = 0; // will store last time LED was updated
unsigned long previousMillis_2 = 0;

// constants won’t change :
const long interval_1 = 1000; // interval at which to blink (milliseconds)
const long interval_2 = 500;

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

void loop() {
led_servo_crystal();
}

uint32_t led_servo_crystal() {

uint16_t i, j;
unsigned long currentMillis_2 = millis();
unsigned long currentMillis_3 = millis();

unsigned long currentMillis_1 = millis();

if (currentMillis_1 - previousMillis_1 >= interval_1) {

previousMillis_1 = currentMillis_1;

// tryck in koden här
val = random(65,100);
servo.write(val);

}

if (currentMillis_2 - previousMillis_2 >= interval_2) {

previousMillis_2 = currentMillis_2;

for(j=0; j<256; j++) {
for(i=0; i<led_strip.numPixels(); i++) {
led_strip.setPixelColor(i, Wheel((i+j) & 255));
}
led_strip.show();

}
}
}

uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return led_strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return led_strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return led_strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

WHat happens when you run the program ?
What should happen when you run the program ?

#define NUMPIXELS      1How many LEDs are there in your strip ?

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]`` [color=blue]// your code is here[/color] ``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool. I recommend you to use the standard IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason just make for more scrolling when we’re trying to read your code.

UKHeliBob:
WHat happens when you run the program ?
What should happen when you run the program ?

#define NUMPIXELS      1

How many LEDs are there in your strip ?

There is only 1 LED.

The code for the led works when I only run it by it self and use delay()

for(j=0; j<256; j++) {
      for(i=0; i<led_strip.numPixels(); i++) {
        led_strip.setPixelColor(i, Wheel((i+j) & 255));
      }

      led_strip.show();
      delay(500);
}

But if I use it in combination with “Blink without delay” it doesent.

when i run the program the servo works, but not the led…

I whant the led to do its thing and the servo to do its.

How long does it take to make 256 calls to led_strip.show()

for(j=0; j<256; j++) {
      for(i=0; i<led_strip.numPixels(); i++) {
        led_strip.setPixelColor(i, Wheel((i+j) & 255));
      }
      led_strip.show();

I suggest you replace the FOR with IF and a counter and allow loop() to do the iteration.

The demo Several Things at a Time is an extended example of BWoD. It may help with understanding the technique.

…R

brandon_lane:
I whant the led to do its thing and the servo to do its.

Adafruit has a tutorial on how to do this.