Go Down

Topic: [Solved]LED Strip with arrays (Read 808 times) previous topic - next topic

komodo127

At the end I solved it and now I have everything working with the serial and the different sections.
dd if=/dev/brain of=/dev/head

PaulRB

Was all help from this forum useless to you? If not, you should post your working code here so that others can benefit from what you learned. Also we might have suggestions for improvements. But if the forum was of no help to you, then feel free to keep the code to yourself.

komodo127

#17
Jun 05, 2019, 04:12 am Last Edit: Jun 05, 2019, 04:14 am by komodo127
I had to log out yesterday, my company is very restrictive and does not allow for overtime.

Code: [Select]
#include <bitswap.h>
#include <chipsets.h>
#include <color.h>
#include <colorpalettes.h>
#include <colorutils.h>
#include <controller.h>
#include <cpp_compat.h>
#include <dmx.h>
#include <fastLED.h>
#include <fastled_config.h>
#include <fastled_delay.h>
#include <fastled_progmem.h>
#include <fastpin.h>
#include <fastspi.h>
#include <fastspi_bitbang.h>
#include <fastspi_dma.h>
#include <fastspi_nop.h>
#include <fastspi_ref.h>
#include <fastspi_types.h>
#include <hsv2rgb.h>
#include <led_sysdefs.h>
#include <lib8tion.h>
#include <noise.h>
#include <pixelset.h>
#include <pixeltypes.h>
#include <platforms.h>
#include <power_mgt.h>

#include <FastLED.h>
#define LED_PIN     4
#define NUM_LEDS    80
#define LED0 13          // Pin 13 is connected to the LED

char rxChar= 0;         // RXcHAR holds the received command.

CRGB leds[NUM_LEDS];
void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
 

 
//---------------- setup ---------------------------------------------

  Serial.begin(9600);   // Open serial port (9600 bauds).
  pinMode(LED0, OUTPUT); // Sets pin 13 as OUTPUT.
    Serial.flush();       // Clear receive buffer.
 }

//--------------- loop -----------------------------------------------
void loop(){
  if (Serial.available() >0){          // Check receive buffer.
    rxChar = Serial.read();            // Save character received.
    Serial.flush();                    // Clear receive buffer.
 
  switch (rxChar) {
   
    case '1':
      if (digitalRead(leds[0]) == LOW){        // If LED is Off:
  leds[0] = CRGB(0, 255, 0);
  leds[1] = CRGB(0, 255, 0);
  leds[2] = CRGB(0, 255, 0);
  leds[3] = CRGB(0, 255, 0);
  leds[4] = CRGB(0, 255, 0);
  leds[5] = CRGB(0, 255, 0);
  leds[6] = CRGB(0, 255, 0);
  leds[7] = CRGB(0, 255, 0);
  leds[8] = CRGB(0, 255, 0); 
  FastLED.show();
  delay(4000);
  }

  leds[0] = CRGB(255, 0, 0);
  leds[1] = CRGB(255, 0, 0);
  leds[2] = CRGB(255, 0, 0);
  leds[3] = CRGB(255, 0, 0);
  leds[4] = CRGB(255, 0, 0);
  leds[5] = CRGB(255, 0, 0);
  leds[6] = CRGB(255, 0, 0);
  leds[7] = CRGB(255, 0, 0);
  leds[8] = CRGB(255, 0, 0); 
  FastLED.show();
  delay(500);
  leds[0] = CRGB(0, 0, 0);
  leds[1] = CRGB(0, 0, 0);
  leds[2] = CRGB(0, 0, 0);
  leds[3] = CRGB(0, 0, 0);
  leds[4] = CRGB(0, 0, 0);
  leds[5] = CRGB(0, 0, 0);
  leds[6] = CRGB(0, 0, 0);
  leds[7] = CRGB(0, 0, 0);
  leds[8] = CRGB(0, 0, 0); 
  FastLED.show();
  delay(500);
  break;
 
case '2':
      if (digitalRead(leds[11]) == LOW){        // If LED is Off:
    leds[11] = CRGB(0, 255, 0);
    leds[12] = CRGB(0, 255, 0);
    leds[13] = CRGB(0, 255, 0);
    leds[14] = CRGB(0, 255, 0);
    FastLED.show();
    delay(4000);
 }
    leds[11] = CRGB(255, 0, 0);
    leds[12] = CRGB(255, 0, 0);
    leds[13] = CRGB(255, 0, 0);
    leds[14] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[11] = CRGB(0, 0, 0);
    leds[12] = CRGB(0, 0, 0);
    leds[13] = CRGB(0, 0, 0);
    leds[14] = CRGB(0, 0, 0);
    FastLED.show();
    delay(500);
        break;
    case '3':
      if (digitalRead(leds[19]) == LOW){        // If LED is Off::
    leds[19] = CRGB(0, 255, 0);
    leds[20] = CRGB(0, 255, 0);
    leds[21] = CRGB(0, 255, 0);
    leds[22] = CRGB(0, 255, 0);
    FastLED.show();
    delay(4000);
  }
    leds[19] = CRGB(255, 0, 0);
    leds[20] = CRGB(255, 0, 0);
    leds[21] = CRGB(255, 0, 0);
    leds[22] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[19] = CRGB(0, 0, 0);
    leds[20] = CRGB(0, 0, 0);
    leds[21] = CRGB(0, 0, 0);
    leds[22] = CRGB(0, 0, 0);
    FastLED.show();
    delay(500);
    break;
    case '4':
if (digitalRead(leds[29]) == LOW){        // If LED is Off:
    leds[29] = CRGB(0, 255, 0);
    leds[30] = CRGB(0, 255, 0);
    leds[31] = CRGB(0, 255, 0);
    leds[32] = CRGB(0, 255, 0);
    FastLED.show();
    delay(4000);
  }

    leds[29] = CRGB(255, 0, 0);
    leds[30] = CRGB(255, 0, 0);
    leds[31] = CRGB(255, 0, 0);
    leds[32] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[29] = CRGB(0, 0, 0);
    leds[30] = CRGB(0, 0, 0);
    leds[31] = CRGB(0, 0, 0);
    leds[32] = CRGB(0, 0, 0);
    FastLED.show();
    delay(500);
    break;
        case '5':
      if (digitalRead(leds[35]) == LOW){        // If LED is Off:
    leds[35] = CRGB(0, 255, 0);
    leds[36] = CRGB(0, 255, 0);
    FastLED.show();
    delay(4000);
  }

    leds[35] = CRGB(255, 0, 0);
    leds[36] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[35] = CRGB(0, 0, 0);
    leds[36] = CRGB(0, 0, 0);
    FastLED.show();
    delay(500);
    break;
            case '6':
      if (digitalRead(leds[38]) == LOW){        // If LED is Off:
    leds[38] = CRGB(0, 255, 0);
    leds[39] = CRGB(0, 255, 0);
    FastLED.show();
    delay(4000);
  }

    leds[38] = CRGB(255, 0, 0);
    leds[39] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[38] = CRGB(0, 0, 0);
    leds[39] = CRGB(0, 0, 0);
    FastLED.show();
    delay(500);
    break;
        case '7':
      if (digitalRead(leds[40]) == LOW){        // If LED is Off:
    leds[40] = CRGB(0, 255, 0);
    leds[41] = CRGB(0, 255, 0);
    leds[42] = CRGB(0, 255, 0);
    leds[43] = CRGB(0, 255, 0);
    FastLED.show();
    delay(4000);
  }

    leds[40] = CRGB(255, 0, 0);
    leds[41] = CRGB(255, 0, 0);
    leds[42] = CRGB(255, 0, 0);
    leds[43] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[40] = CRGB(0, 0, 0);
    leds[41] = CRGB(0, 0, 0);
    leds[42] = CRGB(0, 0, 0);
    leds[43] = CRGB(0, 0, 0);
    FastLED.show();
    delay(500);
    break;
            case '8':
      if (digitalRead(leds[44]) == LOW){        // If LED is Off:
    leds[44] = CRGB(0, 255, 0);
    leds[45] = CRGB(0, 255, 0);
    FastLED.show();
    delay(4000);
  }

    leds[44] = CRGB(255, 0, 0);
    leds[45] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[44] = CRGB(0, 0, 0);
    leds[45] = CRGB(0, 0, 0);
    FastLED.show();
    delay(500);
    break;
            case '9':
      if (digitalRead(leds[53]) == LOW){        // If LED is Off:
    leds[53] = CRGB(0, 255, 0);
    leds[54] = CRGB(0, 255, 0);
    FastLED.show();
    delay(4000);
  }

    leds[53] = CRGB(255, 0, 0);
    leds[54] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[53] = CRGB(0, 0, 0);
    leds[54] = CRGB(0, 0, 0);
    FastLED.show();
    delay(500);
    break;
            case 'a':
      if (digitalRead(leds[58]) == LOW){        // If LED is Off:
    leds[58] = CRGB(0, 255, 0);
    leds[59] = CRGB(0, 255, 0);
    leds[60] = CRGB(0, 255, 0);
    FastLED.show();
    delay(4000);
  }

    leds[58] = CRGB(255, 0, 0);
    leds[59] = CRGB(255, 0, 0);
    leds[60] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[58] = CRGB(0, 0, 0);
    leds[59] = CRGB(0, 0, 0);
    leds[60] = CRGB(0, 0, 0);
    FastLED.show();
    delay(500);
    break;
                case 'b':
      if (digitalRead(leds[68]) == LOW){        // If LED is Off:
    leds[68] = CRGB(0, 255, 0);
    leds[69] = CRGB(0, 255, 0);
    leds[70] = CRGB(0, 255, 0);
    FastLED.show();
    delay(4000);
  }

    leds[68] = CRGB(255, 0, 0);
    leds[69] = CRGB(255, 0, 0);
    leds[70] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[68] = CRGB(0, 0, 0);
    leds[69] = CRGB(0, 0, 0);
    leds[70] = CRGB(0, 0, 0);
    FastLED.show();
    delay(500);
    break;
            case 'c':
      if (digitalRead(leds[76]) == LOW){        // If LED is Off:
    leds[76] = CRGB(0, 255, 0);
    leds[77] = CRGB(0, 255, 0);
    leds[78] = CRGB(0, 255, 0);
    leds[79] = CRGB(0, 255, 0);
    FastLED.show();
    delay(4000);
  }

    leds[76] = CRGB(255, 0, 0);
    leds[77] = CRGB(255, 0, 0);
    leds[78] = CRGB(255, 0, 0);
    leds[79] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[76] = CRGB(0, 0, 0);
    leds[77] = CRGB(0, 0, 0);
    leds[78] = CRGB(0, 0, 0);
    leds[79] = CRGB(0, 0, 0);
    FastLED.show();
    delay(500);
    break;
     

    }
  }
}


So now I can open the serial and allow the raspberry pi to send a character to my arduino board and having the right section turning on for 4 seconds (green) and red for 1/2 a second.

Off course it is ugly and it takes a lot of memory but so far this is the only solution that works for me.

Other improvements I'd like to see is to accept more than one character at the time with a buffer.
dd if=/dev/brain of=/dev/head

johnwasser

This is how small your sketch would be if you used functions, loos, and constants instead of repeating code over and over.

Code: [Select]
#include <FastLED.h>

const byte LED_PIN  =   4;
const byte NUM_LEDS  =  80;
const byte LED0 = 13;          // Pin 13 is connected to the LED

char rxChar = 0;        // RXcHAR holds the received command.

const CRGB CRGBBlack = CRGB(0, 0, 0);
const CRGB CRGBGreen = CRGB(0, 255, 0);
const CRGB CRGBRed = CRGB(255, 0, 0);

CRGB leds[NUM_LEDS];

//---------------- setup ---------------------------------------------
void setup()
{
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

  Serial.begin(9600);   // Open serial port (9600 bauds).
  pinMode(LED0, OUTPUT); // Sets pin 13 as OUTPUT.
  Serial.flush();       // Wait for output buffer to empty
}


void SetLEDGroup(byte startIndex, byte endIndex, CRGB color)
{
  for (byte i = startIndex; i <= endIndex; i++)
    leds[i] = color;
  FastLED.show();
}

void DisplayLEDGroup(byte startIndex, byte endIndex)
{
  if (leds[startIndex] == CRGBBlack)
  {
    SetLEDGroup(startIndex, endIndex, CRGBGreen);
    delay(4000);
  }
  SetLEDGroup(startIndex, endIndex, CRGBRed);
  delay(500);

  SetLEDGroup(startIndex, endIndex, CRGBBlack);
}

//--------------- loop -----------------------------------------------
void loop()
{
  if (Serial.available() > 0)          // Check receive buffer.
  {
    rxChar = Serial.read();            // Save character received.
    Serial.flush();                    // Wait for output buffer to empty

    switch (rxChar)
    {
      case '1':  DisplayLEDGroup(0, 8); break;

      case '2':  DisplayLEDGroup(11, 14); break;

      case '3':  DisplayLEDGroup(19, 22); break;

      case '4':  DisplayLEDGroup(29, 32); break;

      case '5':  DisplayLEDGroup(35, 36); break;

      case '6':  DisplayLEDGroup(38, 39); break;

      case '7':  DisplayLEDGroup(40, 43); break;

      case '8':  DisplayLEDGroup(44, 45); break;

      case '9':  DisplayLEDGroup(53, 54); break;

      case 'a':   DisplayLEDGroup(58, 60); break;

      case 'b':  DisplayLEDGroup(68, 70); break;

      case 'c':   DisplayLEDGroup(76, 79); break;

    }
  }
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

komodo127

That is awesome, I wish I had the skills for that. I am relatively new to Arduino and I am moving my steps while having to deal with my bosses always involved with projects.

I will try to fix it as soon as possible.

Thank you again for this :D
dd if=/dev/brain of=/dev/head

Go Up