Go Down

Topic: Combining stepper, ledstrip & audio (Read 2052 times) previous topic - next topic

StudioBloemendal

Hello,

I am working on an art project and I want to combine a stepper (with limit switch) , led strip & audio file.

The stepper starts an moves toward a limit switch, after touching it will start a loop from 5 to 1000. Some // are in Dutch sorry!
Code: [Select]
/*  Motor Homing code using AccelStepper and the Serial Monitor
 
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/
#define motorPin1  8                // IN1 pin op de ULN2003A driver
#define motorPin2  9                // IN2 pin op de ULN2003A driver
#define motorPin3  10               // IN3 pin op de ULN2003A driver
#define motorPin4  11               // IN4 pin op de ULN2003A driver

int stepsPerRevolution = 64;        // stappen per omwenteling
float degreePerRevolution = 5.625;  // graden per omwenteling

// AccelStepper Setup
AccelStepper stepperX(AccelStepper::HALF4WIRE, motorPin1, motorPin3, motorPin2, motorPin4);


// Define the Pins used
#define home_switch 12 // Pin 9 connected to Home Switch (MicroSwitch)

// Stepper Travel Variables

int move_finished=1;  // Used to check if move is completed
long initial_homing=-1;  // Used to Home Stepper at startup


void setup() {
   Serial.begin(9600);  // Start the Serial monitor with speed of 9600 Bauds
   
   pinMode(home_switch, INPUT_PULLUP);
   
   delay(5);  // Wait for EasyDriver wake up

   //  Set Max Speed and Acceleration of each Steppers at startup for homing
  stepperX.setMaxSpeed(2000.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperX.setAcceleration(1000.0);  // Set Acceleration of Stepper
  stepperX.setSpeed(2000);            // stel de huidige snelheid in
 

// Start Homing procedure of Stepper Motor at startup

  Serial.print("Stepper is Homing . . . . . . . . . . . ");

  while (digitalRead(home_switch)) {  // Make the Stepper move CCW until the switch is activated   
    stepperX.moveTo(initial_homing);  // Set the position to move to
    initial_homing--;  // Decrease by 1 for next move if needed
    stepperX.run();  // Start moving the stepper
    delay(5);
}

  stepperX.setCurrentPosition(0);  // Set the current position as zero for now
  stepperX.setMaxSpeed(1000.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperX.setAcceleration(500.0);  // Set Acceleration of Stepper
  initial_homing=1;

  while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
    stepperX.moveTo(initial_homing); 
    stepperX.run();
    initial_homing++;
    delay(5);
  }
 
  stepperX.setCurrentPosition(0);
  Serial.println("Homing Completed TOT HIER");
  Serial.println("");
  stepperX.setMaxSpeed(1000.0);      // Set Max Speed of Stepper (Faster for regular movements)
  stepperX.setAcceleration(1000.0);  // Set Acceleration of Stepper
}
// Graden van bewegingen dus eerst naar 30 graden, dan naar -30 graden dan naar 60 graden etc.
int steps[] = { 1000, 5, 1000, 5, 1000, 5, 1000, 5, };

// Het totaal aantal bewegingen in steps[]
int stepsCount = 5;

// Houdt bij welke positie van steps[] we gaan uitvoeren
int stepsIndex = 0;

void loop() {

  // Als de stepper niet beweegt en geen afstand meer hoeft af te leggen
  if (!stepperX.isRunning() && stepperX.distanceToGo() == 0) {

    // Beweeg de stepper naar het aantal graden op de positie stepIndex van steps[]
    stepperX.moveTo(degToSteps(steps[stepsIndex]));

    // Verhoog de index als de beweging klaar is
    stepsIndex++;

    // Als we alle posities in steps[] gehad hebben
    if (stepsIndex > stepsCount) {

      // Begin overnieuw bij 0
      stepsIndex = 0;
    }
  }

  stepperX.run();

}

/*
 * Rekent het aantal graden om naar het aantal stappen
 * In ons voorbeeld van 45 graden: 64 / 5,625 = 1024 stappen
 */
float degToSteps(float deg) {
  return (stepsPerRevolution / degreePerRevolution) * deg;
}


I want to combine the movement of the stepper with the fade in (50 to 250) and fade out (250 to 50) of a led strip. The led strip should be on 50 when the stepper is on 5 and on 250 when the stepper is on 1000. And I also want to start an audio file at the beginning of every loop! I know that I could remove te delay to combine the scripts and replace them with Milles but don't know how to do this. And I also don't know how to include the audio! Please help

Code: [Select]
#include "FastLED.h"
#define NUM_LEDS 208
CRGB leds[NUM_LEDS];
#define PIN 6

void setup()
{
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}

void loop()

{
  FadeInOut(0xff, 0xff, 0xff);
}

void FadeInOut(byte red, byte green, byte blue){
  float r, g, b;
     
  for(int k = 50; k < 250; k=k+1) {
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll(r,g,b);
    showStrip();
    delay(60);
  }
     
  for(int k = 250; k >= 50; k=k-2) {
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll(r,g,b);
    showStrip();
    delay(60);
  }
}

void showStrip() {
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}




Thank you for reading this!

groundFungus


Go Up