Hi all,
First post, very exciting, but I’m stressing out of my mind, here. I’ve (rather, the students I teach) have created a fun robot for one of the team’s sponsors, but unfortunately, the only input from the smartphone that seems to be working is setting the ZeRGBa to 0 0 0 in order to get a strip of WS2812b’s to go into “rainbow mode” (per the example code for the Adafruit NeoPixel library). Moving the color around on the ZeRGBA will not let the code manually change the color.
Additionally, the 2-axis joystick will accept values, but will not update the PWM outputs with those values. Hmmm, after doing a little digging [ which is something I should have done first ] it seems as if the pins I chose as the motor outputs do not support PWM. Gonna change that when I get home and double check.
Anywho, Any help on the ZeRGBa issue would be appreciated, and, for reference, here’s my code:
// This #include statement was automatically added by the Particle IDE.
#define BLINK_SERIAL Serial
#include "blynk/blynk.h"
// This #include statement was automatically added by the Particle IDE.
#include "neopixel/neopixel.h"
#define PIXEL_PIN D2
#define PIXEL_COUNT 240
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
char auth[] = "2b0d690f7e8546afb1fd49d528cdddd4"; // insert Auth Token from Blynk app, here
Servo r_motor;
Servo l_motor;
char pCloudData[40];
// Motor pins
//int r_motor = D4; //D4
//int l_motor = D5; //D5
void setup() {
Serial.begin(115200);
// pinMode(r_motor, OUTPUT);
// pinMode(l_motor, OUTPUT);
r_motor.attach(D4);
l_motor.attach(D5);
delay(200);0
Blynk.begin(auth);
// Initialize all pixels to "off"
strip.begin();
strip.show();
}
// Joystick widget, use the 2-axis joystick with merged outputs
BLYNK_WRITE(V1) {
int x = param[0].asInt();
int y = param[1].asInt();
sprintf(pCloudData, "X: %d, Y: %d", x,y);
Particle.publish("detected joystick input", pCloudData);
// Particle.publish("X axis: ", x);
// Particle.publish("Y axis: ", y);
Arcade(x,y);
}
// RGB Widget: Use ZeRGBa widget on the app
BLYNK_WRITE(V2){
int r = param[0].asInt();
int g = param[1].asInt();
int b = param[2].asInt();
if (r > 0 && g > 0 && b > 0) {
sprintf(pCloudData, "R: %d, G: %d, B: %d", r,g,b);
Particle.publish("detected RGB input", pCloudData);
strip.Color(r,g,b);
strip.show();
// delay(20);
}
else {
//P article.publish("Entering Rainbow Mode");
rainbow(20);
}
}
void loop() {
// rainbow(20);
Blynk.run();
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
// delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
void Arcade(int x, int y) // make joystick arcade style controller
{
int powY;
int powX;
int powRightMotor;
int powLeftMotor;
// convert joystick 0 - 255 range to -100 to 100 for powering motors
powY = ((y - 127)* 100) / 127; // joystick y axis gives maximum power level
powX = ((x - 127)* 100) / 127; // x axis determines which motor is reduced or
// reversed for a turn
sprintf(pCloudData, "X: %d, Y: %d", powX, powY);
Particle.publish("Arcade Values", pCloudData);
if (powX < 0) // if x negative, turning left; otherwise, turning right
{
powLeftMotor = (powY * (100 + (2 * powX)) / 100); // left motor reduced for right turn
powRightMotor = powY; // right motor not changed
}
else
{
powRightMotor = (powY * (100 - (2 * powX)) / 100); // right motor reduced for left turn
powLeftMotor = powY; // left motor not changed
}
r_motor.write(powRightMotor);
l_motor.write(powLeftMotor);
}