I’ve been trying to write a new library, and now I’ve gotten no compiler errors I finally flashed my code, only to get a hard fault right of the bat.
I was hoping someone could help me diagnose the problem.
This is my main code:
#include "lights.h"
#include "neopixel/neopixel.h"
#include "application.h"
#define PIXEL_PIN D0
#define PIXEL_COUNT 60
#define PIXEL_TYPE WS2812B
int select;
//constructor
lights lightstrip = lights(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
void setup() {
Particle.subscribe("value", orders);
}
void loop() {
//control();
}
void orders(const char *event, const char *data)
{
/*
If they are the same, strcmp will return 0.
*/
if (strcmp(data,"rainbow")==0) {
select=0;
}
else if (strcmp(data,"wave")==0) {
select=1;
}
else if (strcmp(data,"spread")==0) {
select=2;
}
else if (strcmp(data,"spectrum")==0) {
select=3;
}
else if (strcmp(data,"off")==0) {
select=4;
}
else if (strcmp(data,"solaire")==0) {
select=5;
}
else {
// if the data is something else, don't do anything.
}
}
void control() {
switch(select) {
case 0: lightstrip.rainbow();
break;
case 1: lightstrip.wave();
break;
case 2: lightstrip.spread();
break;
case 3: lightstrip.spectrum();
break;
case 4: lightstrip.off();
break;
case 5: lightstrip.setColor(255,147,51);
break;
}
}
This is lights.cpp:
#include "lights.h"
#include "neopixel/neopixel.h"
#define DEFAULT_SPEED 20
int speed = DEFAULT_SPEED;
//constructor
lights::lights(uint16_t n, uint8_t p, uint8_t t) : Adafruit_NeoPixel(n,p,t) {
begin();
show();
};
//These are different ways to display
void lights::rainbow() {
uint16_t i, j;
for (j = 0; j < 256; j++) {
for (i = 0; i < numPixels(); i++) {
setPixelColor(i, Wheel((i + j) & 255));
}
show();
delay(speed);
}
}
void lights::wave() {
uint16_t i, j;
for (j = 0; j < 256; j++) {
for (i = 0; i < (numPixels() / 2); i++) {
setPixelColor(29 - i, Wheel((i + j) & 255));
setPixelColor(30 + i, Wheel((i + j) & 255));
}
show();
delay(speed);
}
}
void lights::spread() {
uint16_t i, j;
for (j = 0; j < 256; j++) {
for (i = 0; i < ((numPixels() / 2)+1); i++) {
setPixelColor(60 - i, Wheel((i + j) & 255));
setPixelColor(0 + i, Wheel((i + j) & 255));
}
show();
delay(speed);
}
}
void lights::spectrum() {
uint16_t i, j;
for (j = 0; j < 256; j++) {
for (i = 0; i < numPixels(); i++) {
setPixelColor(i, Wheel((j) & 255));
}
show();
delay(speed);
}
}
void lights::off() {
for(uint16_t i=0; i<numPixels(); i++) {
setPixelColor(i, 0);
show();
delay(speed);
}
}
void lights::setColor(uint32_t r, uint32_t g, uint32_t b) {
uint16_t i;
for (i = 0; i < numPixels(); i++) {
setPixelColor(i,r,g,b);
}
show();
delay(speed);
}
void lights::setSpeed(uint32_t speedset) {
speed=speedset;
}
//Private
uint32_t lights::Wheel(byte WheelPos) {
if (WheelPos < 85) {
return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
and this is lights.h:
#ifndef lights_H
#define lights_H
#include "neopixel/neopixel.h"
#include "application.h"
class lights : Adafruit_NeoPixel {
public:
//constructor
lights(uint16_t n, uint8_t p, uint8_t t);
int speed;
//fancy functions
void
rainbow(),
wave(),
spread(),
spectrum(),
off(),
setColor(uint32_t r, uint32_t g, uint32_t b),
setSpeed(uint32_t speedset);
private:
uint32_t Wheel(byte);
};
#endif
I really have no idea where to go about troubleshooting this, so any help would be greatly appreciated.