Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Having some issues ...
 
Share:
Notifications
Clear all

[Solved] Having some issues with my coding

2 Posts
2 Users
1 Reactions
1,022 Views
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

I have never worked with code or anything like it until about a month and a half ago. I am writing this code to be triggered by inputs to turn specific light sequences on. Any help in what I have wrong would be greatly appreciated! I am going to copy what I have here and hopefully someone can point out what I have wrong and help me fix it.

#include "FastLED.h"
#include <Adafruit_NeoPixel.h>
#define NUM_LEDS 100
#define DATA_PIN0 11
#define DATA_PIN1 10
#define DATA_PIN2 9
#define DATA_PIN3 13
#define CHIPSET WS2811
#define BRIGHTNESS 200
#define ON 1
#define OFF 0
CRGB leds[NUM_LEDS];


void setup() {
  FastLED.addLeds<CHIPSET, DATA_PIN3>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();
  FastLED.show();
}


void switchInput(int inputOne, int inputTwo, int inputThree) {
  if (digitalRead(inputOne) == ON && digitalRead(inputTwo) == OFF && digitalRead(inputThree) == OFF) {
        for (int i = 0; i < NUM_LEDS; i++)
        leds[i] = CRGB::Red;
        FastLED.show();
  }
  if (digitalRead(inputOne) == OFF && digitalRead(inputTwo) == ON && digitalRead(inputThree) == OFF) {
        for (int i = 0; i < NUM_LEDS; i++)
        leds[i] = CRGB::Green;
        FastLED.show();
    }
  if (digitalRead(inputOne) == OFF && digitalRead(inputTwo) == OFF && digitalRead(inputThree) == ON) {
   void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
   int Position=0;
 
   for(int j=0; j<NUM_LEDS*2; j++)
   {
      Position++; // = 0; //Position + Rate;
      for(int i=0; i<NUM_LEDS; i++) {
        // sine wave, 3 offset waves make a rainbow!
        //float level = sin(i+Position) * 127 + 128;
        //setPixel(i,level,0,0);
        //float level = sin(i+Position) * 127 + 128;
        setPixel(i,((sin(i+Position) * 127 + 128)/255)*red,
                   ((sin(i+Position) * 127 + 128)/255)*green,
                   ((sin(i+Position) * 127 + 128)/255)*blue);
      }
     
      showStrip();
      delay(WaveDelay);
  }
}    
    void showStrip() {
     #ifdef ADAFRUIT_NEOPIXEL_H
     // NeoPixel
     strip.show();
     #endif
     #ifndef ADAFRUIT_NEOPIXEL_H
     // FastLED
     FastLED.show();
     #endif
     }


    void setPixel(int Pixel, byte red, byte green, byte blue) {
     #ifdef ADAFRUIT_NEOPIXEL_H
     // NeoPixel
     strip.setPixelColor(Pixel, strip.Color(red, green, blue));
     #endif
     #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();
     }
  }
}

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2796
 

Let me see if I can help ... so first of all you'll need to decide what library you're going to be using - FastLED of NeoPixel.
In your code you use both which will most likely cause conflicts and issues.

Of these 2 lines, remove one of them (I'd keep the FastLED library - better, more mature and faster, and some of your code seems to be using FastLED stuff):

#include "FastLED.h"
#include <Adafruit_NeoPixel.h>

Next problem is that every (standard) Arduino code has two major functions (see also my beginners course where you can find some of the basics).

One to initialize things:

void setup (
...
}

and one loop dat keeps repeating

void loop (
...
}

That last one seem to be missing so nothing will really happen with your code.
In that loop you'll probably want to call your "switchInput" function, so the Arduino keeps looking at the switches.

Probably something like this (assuming PIN0, PIN1 and PIN2 are switches):

void loop {
  switchInput(PIN_SWITCH1, PIN_SWITCH2, PIN_SWITCH3);
}

Ask you can see, I renamed the defined pins to a more meaning full and less confusing name - good habit to learn over time as it makes your code easier to read and fix.

Next you have a function (RunningLights) defined in a function which is not a good thing to do.
Taking this function definition out of the other function, comes with an issues since it asks for red, green and blue values, which are nowhere found (replaced them with "??").

Since we're using FastLED, the "conversion" function (between FastLED and NeoPixel - eg. ShowStrip) are no longer needed and you can use the FastLED counter part.

 

Some basic cleanup makes it look like this but more needs to be done to make this more useful - hope it helps 😊 

Note: untested, no warranty so you computer may explode 😉 -- and it will fail when you try to compile it because of the missing parameter values of "RunningLights". So it still needs work!

#include "FastLED.h"

# LEDs
#define NUM_LEDS 100
#define PIN_LEDS 13
#define CHIPSET WS2811
#define BRIGHTNESS 200
CRGB leds[NUM_LEDS];

# switches
#define PIN_SWITCH1 11
#define PIN_SWITCH2 10
#define PIN_SWITCH3 9
#define ON 1
#define OFF 0


void setup() {
  FastLED.addLeds<CHIPSET, PIN_LEDS>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();
  FastLED.show();
}

void loop {
  switchInput(PIN_SWITCH1, PIN_SWITCH2, PIN_SWITCH3);
}

void switchInput(int inputOne, int inputTwo, int inputThree) {
  if (digitalRead(inputOne) == ON && digitalRead(inputTwo) == OFF && digitalRead(inputThree) == OFF) {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB::Red;
    FastLED.show();
  }
  
  if (digitalRead(inputOne) == OFF && digitalRead(inputTwo) == ON && digitalRead(inputThree) == OFF) {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB::Green;
    FastLED.show();
  }
  
  if (digitalRead(inputOne) == OFF && digitalRead(inputTwo) == OFF && digitalRead(inputThree) == ON) {
    RunningLights( ??, ??, ?? ); 
  }
}    

void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
  int Position=0;

  for(int j=0; j<NUM_LEDS*2; j++)
  {
    Position++; // = 0; //Position + Rate;
    for(int i=0; i<NUM_LEDS; i++) {
      // sine wave, 3 offset waves make a rainbow!
      //float level = sin(i+Position) * 127 + 128;
      //setPixel(i,level,0,0);
      //float level = sin(i+Position) * 127 + 128;
      setPixel(i,((sin(i+Position) * 127 + 128)/255)*red,
                 ((sin(i+Position) * 127 + 128)/255)*green,
                 ((sin(i+Position) * 127 + 128)/255)*blue);
    }
 
    FastLED.show();
    delay(WaveDelay);
}


void setPixel(int Pixel, byte red, byte green, byte blue) {
  leds[Pixel].r = red;
  leds[Pixel].g = green;
  leds[Pixel].b = blue;
}

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

   
baker7001 reacted
ReplyQuote
Share: