Dummy question re neopixel

Hi There,
I wonder if someone could help me, ( coding newbie) I did 3D printer stargate for my kiddo, and have spare WS strip bwith Uno 3 and want to "brighten it a bit" that stargate, can someone tell me how to code for Uno r3 simple sketch that light up 9 neopixels (Chevrons) randomly one by one in red and loop it after 9 chevron done?
Thank you in advance for any help!

Regards
Peter

I would help you, but I (nor anyone else, usually) will just give you the code. The forum is to help users learn, not provide a free code writing service.

Please read the forum guidelines to get some ideas of what we should know to be able to help you.

First, what exact type of LED strip. NeoPixels (WS2912) powered by 5V? How is the strip powered?

Do you have a schematic or wiring diagram? Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Have you tried to write the code? Please post your best effort. Tell us what the code actually does and how that differs from what you want.

1 Like

Thank you, I'm sorry, I tried to adopt code from Working Stargate with Arduino Control by Carasibana - Thingiverse written for single diffused LED's and servo shield 2.3 to my WS2912 strip and adafruit servo shield v1 L293D Motor Driver Shield I have (absolute now ) after 2 weeks, and 100 sketches I give up and settle down just to do led's ( nema17 was acting like a shaker after my code alterations and leds was not in sync.
I cannot upload code , so will paste here:

/* 
Dialing Sequence program for 3D printed working Stargate
Written by Cara McNab ( Carasibana )
This sketch requires the following Libraries:
Neopixel Library,  available via https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library
Adafruit Motor Shield Library, avaliable via https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/install-software
*/
// Include the Motor shield and NeoPixel Libraries
#include <Wire.h>
//#include <Adafruit_MotorShield.h>
//#include "utility/Adafruit_PWMServoDriver.h"
#include <Adafruit_NeoPixel.h>
#include <AFMotor.h>
// Which pin on the Arduino is connected to the NeoPixels
#define PIN 6

// How many NeoPixels are attached to the Arduino
#define NUMPIXELS 9

// How many steps are required to advance the Symbol Ring by one symbol *needs tweaking
int STEPPERSYM = 31;

// Define the Pixel strand item "pixels"
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);

// Define the colours to use for the NeoPixels
uint32_t orange = pixels.Color(255,40,5);
uint32_t off = pixels.Color(0,0,0);

// Create the motor shield object with the default I2C address
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
//AF_Stepper myMotor(stepsPerRevolution, 2);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
//Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
const int stepsPerRevolution = 200;
AF_Stepper myMotor(stepsPerRevolution, 2);
// Define Stargate address to be dialed as array
// These numbers are the references to the Gate symbols, including the point of origin (1)
// reference can be found here http://rdanderson.com/stargate/glyphs/glyphs.htm 

int SGAddress[] = {27,7,15,32,12,30,1};

// Define delay for Blinking
int BlinkDelay = 1500; 

void setup() {
 // AFMS.begin();  // create with the default frequency 1.6KHz
   
  myMotor.setSpeed(50);  // Set RPM of stepper motor 
  
  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {
  
  // make sure all neopixels are off
  for(int i=0;i<NUMPIXELS;i++){
    pixels.setPixelColor(i, off);
  }
  pixels.show(); // This sends the updated pixel color to the hardware.

  // Chevron One (( Assumes starting on Earth (1) ))
  int Chevron = 1;
  
  int numStep = (SGAddress[(Chevron-1)] - 1) * STEPPERSYM;
  myMotor.step(numStep, FORWARD, INTERLEAVE);
  pixels.setPixelColor(4, orange);
  pixels.show();
  delay(BlinkDelay);
  pixels.setPixelColor(4, off);
  pixels.setPixelColor(5, orange);
  pixels.show();
  
  // Chevron Two
  
  Chevron = 2 ;
  
  if (SGAddress[(Chevron-1)] > SGAddress[(Chevron-2)]){
   int numStep = ((SGAddress[(Chevron-2)] - 1) + (40 - SGAddress[(Chevron-1)])) * STEPPERSYM;
  }
  else{
    int numStep = (SGAddress[(Chevron-1)] - SGAddress[(Chevron-2)]) * STEPPERSYM;
  }
  myMotor.step(numStep, BACKWARD, INTERLEAVE);
  pixels.setPixelColor(4, orange);
  pixels.show();
  delay(BlinkDelay);
  pixels.setPixelColor(4, off);
  pixels.setPixelColor(6, orange);
  pixels.show();
  
  // Chevron Three * Needs Tweaking
  
  Chevron = 3 ;
  
  if (SGAddress[(Chevron-1)] > SGAddress[(Chevron-2)]){
   int numStep = ((SGAddress[(Chevron-2)] - 1) + (40 - SGAddress[(Chevron-1)])) * STEPPERSYM;
  }
  else{
    int numStep = (SGAddress[(Chevron-1)] - SGAddress[(Chevron-2)]) * STEPPERSYM;
  }
  myMotor.step(numStep, FORWARD, INTERLEAVE);
  pixels.setPixelColor(4, orange);
  pixels.show();
  delay(BlinkDelay);
  pixels.setPixelColor(4, off);
  pixels.setPixelColor(7, orange);
  pixels.show();
  
  // Chevron Four
  
  Chevron = 4 ;
  
  if (SGAddress[(Chevron-1)] > SGAddress[(Chevron-2)]){
   int numStep = ((SGAddress[(Chevron-2)] - 1) + (40 - SGAddress[(Chevron-1)])) * STEPPERSYM;
  }
  else{
    int numStep = (SGAddress[(Chevron-1)] - SGAddress[(Chevron-2)]) * STEPPERSYM;
  }
  myMotor.step(numStep, BACKWARD, INTERLEAVE);
  pixels.setPixelColor(4, orange);
  pixels.show();
  delay(BlinkDelay);
  pixels.setPixelColor(4, off);
  pixels.setPixelColor(1, orange);
  pixels.show();
  
  // Chevron Five * Needs Tweaking
  
  Chevron = 5 ;
  
  if (SGAddress[(Chevron-1)] > SGAddress[(Chevron-2)]){
   int numStep = ((SGAddress[(Chevron-2)] - 1) + (40 - SGAddress[(Chevron-1)])) * STEPPERSYM;
  }
  else{
    int numStep = (SGAddress[(Chevron-1)] - SGAddress[(Chevron-2)]) * STEPPERSYM;
  }
  myMotor.step(numStep, FORWARD, INTERLEAVE);
  pixels.setPixelColor(4, orange);
  pixels.show();
  delay(BlinkDelay);
  pixels.setPixelColor(4, off);
  pixels.setPixelColor(2, orange);
  pixels.show();
  
  // Chevron Six
  
  Chevron = 6 ;
  
  if (SGAddress[(Chevron-1)] > SGAddress[(Chevron-2)]){
   int numStep = ((SGAddress[(Chevron-2)] - 1) + (40 - SGAddress[(Chevron-1)])) * STEPPERSYM;
  }
  else{
    int numStep = (SGAddress[(Chevron-1)] - SGAddress[(Chevron-2)]) * STEPPERSYM;
  }
  myMotor.step(numStep, BACKWARD, INTERLEAVE);
  pixels.setPixelColor(4, orange);
  pixels.show();
  delay(BlinkDelay);
  pixels.setPixelColor(4, off);
  pixels.setPixelColor(3, orange);
  pixels.show();
  
 // Chevron Seven * Needs Tweaking
  
  Chevron = 7 ;
  
  if (SGAddress[(Chevron-1)] > SGAddress[(Chevron-2)]){
   int numStep = ((SGAddress[(Chevron-2)] - 1) + (40 - SGAddress[(Chevron-1)])) * STEPPERSYM;
  }
  else{
    int numStep = (SGAddress[(Chevron-1)] - SGAddress[(Chevron-2)]) * STEPPERSYM;
  }
  myMotor.step(numStep, FORWARD, INTERLEAVE);
  pixels.setPixelColor(4, orange);
  pixels.show();
  delay(BlinkDelay);
  pixels.setPixelColor(0, orange);
  pixels.setPixelColor(8, orange);
  pixels.show();
  
  // Restart Sequence
  delay(10000);

}

Stargate awesome!!
I wanna see, pics please..
does it spin and do the whole chevrons lock in place bit??

sorry.. ~q

Not yet, however it It does rotate and light up when chevrons lock if you have working code :slight_smile:

1 Like

sweet!!

got it in a sim now..
did not have the motor lib, so used a different one, a bit broke right now..
and time for dinner for me..
maybe someone else helps too..
else i'll be back.. :sunglasses:

UnoStargateController

~q

Wow , That sim is cool! Did know that it exist:)

1 Like

Yes, it's nice, a handy tool..
won't tell us about real hardware issues though..
so, please, please answer all of @groundFungus questions..
they're very important..
all the code in world won't help if something in the hardware is off..
instead of raising a wormhole it'll just go puff the magic dragon..

i also recommend a title change, something like Stargate controller help..
there's lots of geeks here.. :slight_smile:
be back..

~q

1 Like

It seems that you have a project on a web site that you want to recreate, but with different hardware. Is that right? How different is the hardware?

Have you written code to just test each piece so that you know that the individual pieces work and you know how to use them?

Correct, Different is just Servo shield used by creator, I have adafruit v1 and website sketch has v.2.3 that uses newer library. I could not get V2.3 ( reasonable priced) and had one . I have tested strip itself with default adafruit test sketch from the library ( since there is only 9 LEDS I powered by USB 1A ) after I assembled and glued model to check if all wiring is fine. Strip worked fine, so then I tested servo shield with Nema 17 motor with example sketch from library v1, worked fine too, However my attempt to use web code and recode web sketch with shield 2.3 to version 1.0 completely failed. I'm not at that level to rewrite to different libraries.

so I give up ajusting sketch, googling another similar sketch did not turn so good so I decided at least to make it lightup randomly all chevrons like on original stargate did. Did not want to disappoint completely my daughter with my poor coding skills :slight_smile:

/*
  Dialing Sequence program for 3D printed working Stargate
  Written by Cara McNab ( Carasibana )
  This sketch requires the following Libraries:
  Neopixel Library,  available via https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library
  Adafruit Motor Shield Library, avaliable via https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/install-software
*/
// Include the Motor shield and NeoPixel Libraries
#include <Wire.h>
//#include <Adafruit_MotorShield.h>
//#include "utility/Adafruit_PWMServoDriver.h"
#include <Adafruit_NeoPixel.h>
//#include <AFMotor.h>
#include <AccelStepper.h>
// Which pin on the Arduino is connected to the NeoPixels
#define PIN 6

// How many NeoPixels are attached to the Arduino
#define NUMPIXELS 9

// How many steps are required to advance the Symbol Ring by one symbol *needs tweaking
int STEPPERSYM = 31;

// Define the Pixel strand item "pixels"
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);

// Define the colours to use for the NeoPixels
uint32_t orange = pixels.Color(255, 40, 5);
uint32_t off = pixels.Color(0, 0, 0);

// Create the motor shield object with the default I2C address
// Adafruit_MotorShield AFMS = Adafruit_MotorShield();
//AF_Stepper myMotor(stepsPerRevolution, 2);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
//Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
const int stepsPerRevolution = 200;

AccelStepper myMotor(1, 3, 2);

//AF_Stepper myMotor(stepsPerRevolution, 2);
// Define Stargate address to be dialed as array
// These numbers are the references to the Gate symbols, including the point of origin (1)
// reference can be found here http://rdanderson.com/stargate/glyphs/glyphs.htm

int SGAddress[] = {27, 7, 15, 32, 12, 30, 1};
byte ChevronPixels[] ={4,5,6,7,1,2,3,8};
// Define delay for Blinking
int BlinkDelay = 1500;

byte stateGate = 0;
unsigned long lastMilli = 0;
unsigned int  intervalWait = 1500;
int Chevron = 1;


void setup() {
  // AFMS.begin();  // create with the default frequency 1.6KHz
  Serial.begin(115200);
  Serial.println("Ready");

  myMotor.setMaxSpeed(800);  // Set RPM of stepper motor
  myMotor.setAcceleration(200); // Set acceleration value for the stepper
  myMotor.setCurrentPosition(0); // Set the current position to 0 steps
  

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

void loop() {

  myMotor.run();



  switch (stateGate) {
    case 0: {
        // make sure all neopixels are off
        for (int i = 0; i < NUMPIXELS; i++) {
          pixels.setPixelColor(i, off);
        }
        pixels.show(); // This sends the updated pixel color to the hardware.
        stateGate = 1;
      }
      break;
    case 1: {
        int numStep = (SGAddress[(Chevron - 1)] - 1) * STEPPERSYM;
        Serial.print("State1 Chevron: ");
        Serial.print(Chevron); Serial.print(" steps: ");
        Serial.println(numStep);
        myMotor.runToNewPosition(numStep);
        //myMotor.step(numStep, FORWARD, INTERLEAVE);
        pixels.setPixelColor(4, orange);
        pixels.show();
        stateGate = 2;
      }
      break;
    case 2: if (millis() - lastMilli >= intervalWait) {
        lastMilli = millis();
        pixels.setPixelColor(4, off);
        pixels.setPixelColor(5, orange);
        pixels.show();
        stateGate = 3;
      }
      break;
    case 3: {
        Chevron++ ;
        int numStep = 0;
       if (Chevron < 8){
        if (SGAddress[(Chevron - 1)] > SGAddress[(Chevron - 2)]) {
          numStep = ((SGAddress[(Chevron - 2)] - 1) + (40 - SGAddress[(Chevron - 1)])) * STEPPERSYM;
        }
        else {
          numStep = (SGAddress[(Chevron - 1)] - SGAddress[(Chevron - 2)]) * STEPPERSYM;
        }
        //myMotor.step(numStep, BACKWARD, INTERLEAVE);
        Serial.print("State3 Chevron: ");
        Serial.print(Chevron); Serial.print(" steps: ");
        Serial.println(numStep);
        myMotor.runToNewPosition(numStep);
        pixels.setPixelColor(4, orange);
        pixels.show();
        stateGate = 4;
       } else stateGate = 5;
      }
      break;
      case 4:if (millis() - lastMilli >= intervalWait) {
        lastMilli = millis();
        pixels.setPixelColor(4, off);
        pixels.setPixelColor(ChevronPixels[Chevron-1], orange);
        pixels.show();
        stateGate = 3;
      }
      break;
      case 5: break;
  }

  /*

    // Chevron One (( Assumes starting on Earth (1) ))
    int Chevron = 1;

    int numStep = (SGAddress[(Chevron - 1)] - 1) * STEPPERSYM;
    myMotor.moveTo(numStep);
    //myMotor.step(numStep, FORWARD, INTERLEAVE);
    pixels.setPixelColor(4, orange);
    pixels.show();
    delay(BlinkDelay);
    pixels.setPixelColor(4, off);
    pixels.setPixelColor(5, orange);
    pixels.show();

    // Chevron Two

    Chevron = 2 ;

    if (SGAddress[(Chevron - 1)] > SGAddress[(Chevron - 2)]) {
      int numStep = ((SGAddress[(Chevron - 2)] - 1) + (40 - SGAddress[(Chevron - 1)])) * STEPPERSYM;
    }
    else {
      int numStep = (SGAddress[(Chevron - 1)] - SGAddress[(Chevron - 2)]) * STEPPERSYM;
    }
    //myMotor.step(numStep, BACKWARD, INTERLEAVE);
    myMotor.moveTo(numStep);
    pixels.setPixelColor(4, orange);
    pixels.show();
    delay(BlinkDelay);
    pixels.setPixelColor(4, off);
    pixels.setPixelColor(6, orange);
    pixels.show();

    // Chevron Three * Needs Tweaking

    Chevron = 3 ;

    if (SGAddress[(Chevron - 1)] > SGAddress[(Chevron - 2)]) {
      int numStep = ((SGAddress[(Chevron - 2)] - 1) + (40 - SGAddress[(Chevron - 1)])) * STEPPERSYM;
    }
    else {
      int numStep = (SGAddress[(Chevron - 1)] - SGAddress[(Chevron - 2)]) * STEPPERSYM;
    }
    //myMotor.step(numStep, FORWARD, INTERLEAVE);
    myMotor.moveTo(numStep);
    pixels.setPixelColor(4, orange);
    pixels.show();
    delay(BlinkDelay);
    pixels.setPixelColor(4, off);
    pixels.setPixelColor(7, orange);
    pixels.show();

    // Chevron Four

    Chevron = 4 ;

    if (SGAddress[(Chevron - 1)] > SGAddress[(Chevron - 2)]) {
      int numStep = ((SGAddress[(Chevron - 2)] - 1) + (40 - SGAddress[(Chevron - 1)])) * STEPPERSYM;
    }
    else {
      int numStep = (SGAddress[(Chevron - 1)] - SGAddress[(Chevron - 2)]) * STEPPERSYM;
    }
    //  myMotor.step(numStep, BACKWARD, INTERLEAVE);
    myMotor.moveTo(numStep);
    pixels.setPixelColor(4, orange);
    pixels.show();
    delay(BlinkDelay);
    pixels.setPixelColor(4, off);
    pixels.setPixelColor(1, orange);
    pixels.show();

    // Chevron Five * Needs Tweaking

    Chevron = 5 ;

    if (SGAddress[(Chevron - 1)] > SGAddress[(Chevron - 2)]) {
      int numStep = ((SGAddress[(Chevron - 2)] - 1) + (40 - SGAddress[(Chevron - 1)])) * STEPPERSYM;
    }
    else {
      int numStep = (SGAddress[(Chevron - 1)] - SGAddress[(Chevron - 2)]) * STEPPERSYM;
    }
    // myMotor.step(numStep, FORWARD, INTERLEAVE);
    myMotor.moveTo(numStep);
    pixels.setPixelColor(4, orange);
    pixels.show();
    delay(BlinkDelay);
    pixels.setPixelColor(4, off);
    pixels.setPixelColor(2, orange);
    pixels.show();

    // Chevron Six

    Chevron = 6 ;

    if (SGAddress[(Chevron - 1)] > SGAddress[(Chevron - 2)]) {
      int numStep = ((SGAddress[(Chevron - 2)] - 1) + (40 - SGAddress[(Chevron - 1)])) * STEPPERSYM;
    }
    else {
      int numStep = (SGAddress[(Chevron - 1)] - SGAddress[(Chevron - 2)]) * STEPPERSYM;
    }
    //myMotor.step(numStep, BACKWARD, INTERLEAVE);
    myMotor.moveTo(numStep);
    pixels.setPixelColor(4, orange);
    pixels.show();
    delay(BlinkDelay);
    pixels.setPixelColor(4, off);
    pixels.setPixelColor(3, orange);
    pixels.show();

    // Chevron Seven * Needs Tweaking

    Chevron = 7 ;

    if (SGAddress[(Chevron - 1)] > SGAddress[(Chevron - 2)]) {
      int numStep = ((SGAddress[(Chevron - 2)] - 1) + (40 - SGAddress[(Chevron - 1)])) * STEPPERSYM;
    }
    else {
      int numStep = (SGAddress[(Chevron - 1)] - SGAddress[(Chevron - 2)]) * STEPPERSYM;
    }
    //myMotor.step(numStep, FORWARD, INTERLEAVE);
    myMotor.moveTo(numStep);
    pixels.setPixelColor(4, orange);
    pixels.show();
    delay(BlinkDelay);
    pixels.setPixelColor(0, orange);
    pixels.setPixelColor(8, orange);
    pixels.show();

    // Restart Sequence
    delay(10000);
  */

}

some work..
added a state machine..
removed all delays..
spinning and stopping..
still needs some tinkering, curious, if it will spin up for you..
the sim in the link posted earlier is updated..
let me know how goes..
~q

Thank you, will test it and let you know.
Peter

Is the right library to use AccelStepper: AccelStepper library for Arduino ? Does not spin at all as #2 , will rewire it as #1 motor tomorrow.

post this sketch that does turn the motor and I'll check into it..

~q

Yes, that;s the lib I used in the sim..
Not sure how the motor is connected..
~q

// AFMotor_ConstantSpeed.pde
// -*- mode: C++ -*-
//
// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
// Requires the AFMotor library
// (https://github.com/adafruit/Adafruit-Motor-Shield-library)
// Caution, does not work with Adafruit Motor Shield V2
// See https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library 
// for examples that work with Adafruit Motor Shield V2.

#include <AccelStepper.h>
#include <AFMotor.h>

AF_Stepper motor1(200, 2);


// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep() {  
  motor1.onestep(FORWARD, SINGLE);
}
void backwardstep() {  
  motor1.onestep(BACKWARD, SINGLE);
}

AccelStepper stepper(forwardstep, backwardstep); // use functions to step

void setup()
{  
   Serial.begin(9600);           // set up Serial library at 9600 bps
   Serial.println("Stepper test!");
  
   stepper.setMaxSpeed(50);	
   stepper.setSpeed(50);	
}

void loop()
{  
   stepper.runSpeed();
}

this spins the gate??

~q

updated code in the sim using this example sketch..
i found the AFMotor..
motor does not move in sim, expected..
does the gate spin now??

~q

Yes, I just change on example AF_Stepper motor1(200, 1); to AF_Stepper motor1(200, 2); since my nema is on #2 port