Author Topic: DigiLED & DigiUSB  (Read 5572 times)

sprouts

  • Newbie
  • *
  • Posts: 4
DigiLED & DigiUSB
« on: April 13, 2015, 02:26:32 am »
I'm trying to change the LED colour by sending commands over USB to the Digispark.

The commands are received and echoed back, but the LED does not change colour.
I can change the LED colour if I remove the USB-code.

Is there any other way I can control the DigiLED from the computer? 

Code: [Select]
#include <Adafruit_NeoPixel.h>
#include <DigiUSB.h>

#define USB_CFG_DEVICE_NAME     'D','i','g','i','B','l','i','n','k'
#define USB_CFG_DEVICE_NAME_LEN 9

#define PIN            1
#define NUMPIXELS      1

int delayval = 500; // delay for half a second
byte in = 0;
int Blue = 0;
int Red = 0;
int Green = 0;
int next = 0;
bool dataread = false;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);

void setup()
{
  DigiUSB.begin();
  pixels.begin(); // This initializes the NeoPixel library.
  setLED(Red, Green, Blue);
}

void loop()
{
  DigiUSB.refresh();
  if (DigiUSB.available() > 0)
  {
    in = 0;

    in = DigiUSB.read();
    if (next == 0)
    {
      if (in == 115) {
        next = 1;
        DigiUSB.print("Start; ");
      }
    }
    else if (next == 1) {
      Red  = in;
      DigiUSB.print("Red:");
      DigiUSB.print(Red, DEC);
      next = 2;
    }
    else if (next == 2) {
      Green = in;
      DigiUSB.print(" Green:");
      DigiUSB.print(Green, DEC);
      next = 3;
    }
    else if (next == 3) {
      Blue = in;
      DigiUSB.print(" Blue:");
      DigiUSB.print(Blue, DEC);
      next = 0;
      dataread = true;
    }
    if (dataread)
    {
      pixels.setPixelColor(0, pixels.Color(Red, Green, Blue));
      pixels.show(); // This sends the updated pixel color to the hardware.
      dataread = false;
      DigiUSB.println(" END");
    }
  }
}

defragster

  • Sr. Member
  • ****
  • Posts: 467
« Last Edit: April 15, 2015, 03:48:36 am by defragster »

sprouts

  • Newbie
  • *
  • Posts: 4
Re: DigiLED & DigiUSB
« Reply #2 on: April 16, 2015, 02:50:04 am »
It defaults to pin1, but can be switched to pin5 by soldering a connection on the board.

I'll try replacing the NeoPixel library with another WS2912B-compatible library.

sprouts

  • Newbie
  • *
  • Posts: 4
Re: DigiLED & DigiUSB
« Reply #3 on: April 20, 2015, 10:32:54 pm »
I got it working by switching to to DigiCDC for serial communication.

It's currently just setting predefined LED colours by receiving a single byte.

Code: [Select]
#include <Adafruit_NeoPixel.h>
#include <DigiCDC.h>

#define PIN            1
#define NUMPIXELS      1

//33 ! OFF
//58 : White solid
//59 ; White blink
//60 < Red Solid
//61 = Red Blink
//62 > Green Solid
//63 ? Green Blink
//64 @ Blue Solid
//65 A Blue Blink

byte in = 0;
int Blue = 0;
int Red = 0;
int Green = 150;
int next = 0;
int buff = 0;
bool ledhigh = false;
bool flash = false;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);

void setup()
{
  pixels.begin(); // This initializes the NeoPixel library.
  pixels.setPixelColor(0, pixels.Color(Red, Green, Blue));
  pixels.show(); // This sends the updated pixel color to the hardware.
  SerialUSB.begin();
}

void loop()
{
  if (SerialUSB.available())
  {
    in = SerialUSB.read();

    SerialUSB.println(F("Ping"));
    if (in == 33)
    {
      Red = 0;
      Green = 0;
      Blue = 0;
    }
    else if (in == 58 || in == 59)
    {
      Red = 255;
      Green = 255;
      Blue = 255;
    }
    else if (in == 60 || in == 61)
    {
      Red = 255;
      Green = 0;
      Blue = 0;
    }
    else if (in == 62 || in == 63)
    {
      Red = 0;
      Green = 255;
      Blue = 0;
    }
    else if (in == 64 || in == 65)
    {
      Red = 0;
      Green = 0;
      Blue = 255;
    }

    pixels.setPixelColor(0, Red, Green, Blue);
    pixels.show(); // This sends the updated pixel color to the hardware.
    if (in == 59 || in == 61 || in == 63 || in == 65)
      flash = true;
    else
      flash = false;
  }
  else
  {
    
    if (flash)
    {
      if (ledhigh)
      {
        pixels.setPixelColor(0, Red, Green, Blue);
      }
      else
      {
        pixels.setPixelColor(0, 0, 0, 0);
      }
       pixels.show(); // This sends the updated pixel color to the hardware.
       ledhigh = !ledhigh;
     }
  }

  SerialUSB.delay(200);

}