Skip to content

Instantly share code, notes, and snippets.

@td0m
Created April 23, 2019 11:20
Show Gist options
  • Save td0m/d18f8a4924b1fb984b70c6ff9d598f15 to your computer and use it in GitHub Desktop.
Save td0m/d18f8a4924b1fb984b70c6ff9d598f15 to your computer and use it in GitHub Desktop.
ELECTRONICS
// ask_receiver.pde
// -*- mode: C++ -*-
#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h> // Not actually used but needed to compile
#endif
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
RH_ASK driver(2000, D1, D0, 0);
int PIN = D2;
int NUMPIXELS = 60;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
#ifdef RH_HAVE_SERIAL
Serial.begin(9600); // Debugging only
#endif
if (!driver.init())
#ifdef RH_HAVE_SERIAL
Serial.println("init failed");
#else
;
#endif
strip.begin();
pinMode(A0, INPUT);
}
byte mode = 1;
bool isOn = true;
byte r = 255;
byte g = 0;
byte b = 0;
int i = 0;
void loop()
{
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen))
{
// decode the message
mode = buf[0];
isOn = buf[1] == 1;
r = buf[2];
g = buf[3];
b = buf[4];
// check if turned off
if (!isOn || mode == 0) {
setAll(strip.Color(0,0,0));
} else if (mode == 1) {
setAll(strip.Color(r, g, b));
} else if (mode == 2) {
i = fadingPattern(i);
} else if (mode == 3) {
i = rainbowPattern(i);
} else if (mode == 4) {
i = opacityFadePattern(i);
} else if (mode == 5) {
randomPattern(strip.Color(r,g,b));
} else if (mode == 6) {
temperaturePattern();
} else if (mode == 7) {
i = snakePattern(i, strip.Color(r,g,b));
}
}
}
int snakePattern(int i, uint32_t color) {
int size = 8; // define the size of the snake
strip.setPixelColor(i, color); // sets the ccolour of the currently selected pixel
if (i >= size) // if snake split between start and end
strip.setPixelColor(i-size, strip.Color(0,0,0)); // clear the tail of the snake
else // if snake leaving end
strip.setPixelColor(strip.numPixels()-size+i, strip.Color(0,0,0)); // clear the tail of the snake
strip.show(); // display changes
// move back to the start if end reached
if (i > strip.numPixels())
return 0;
// move the snake by 1 pixel
return i + 1;
}
unsigned int fadingPattern(unsigned int i) {
// get the current colour from the colour wheel
byte* rgb = Wheel(i);
// set the colour of the whole strip to the selected colour
setAll(strip.Color(rgb[0], rgb[1], rgb[2]));
// if end of colour wheel reached
if (i > 765)
return 0; // go back to the start of the wheel
return i + 1; // move up the colour wheel
}
unsigned int rainbowPattern(unsigned int i) {
// loop through all the pixels in the strip (1 to 60 as there are 60 pixels)
for(int j = 0; j < strip.numPixels(); j++) {
// divide the colour wheel into 60 different colour and get the colour for the current pixel
byte* rgb = Wheel((j * 255 / strip.numPixels() + i));
// set the colour of the selected pixel to the colour defined above
strip.setPixelColor(j, strip.Color(rgb[0], rgb[1], rgb[2]));
}
// show changes
strip.show();
// go back to the start of the colour wheel if end reached
if (i > 765) return 0;
return i + 1;
}
unsigned int opacityFadePattern(unsigned int i) {
unsigned int opacity = i;
// if already faded to white
if (i > 100)
opacity = 200 - i; // make the colour fade back to black
int c = opacity * 255 / 100; // opacity converted from between 0 to 100 to between 0 and 255
setAll(strip.Color(c, c, c));
// if faded form black to white then back to black
if (i >= 200)
return 0; // go back to the start
return i + 1; // move on to the next colour
}
void randomPattern(uint32_t colour)
{
// select a random pixel between 0 and 60
int pixel = random(0, strip.numPixels());
// set the colour of the pixel to the value selected on the potentiometer
strip.setPixelColor(pixel, colour);
strip.show(); // show changes
}
void temperaturePattern() {
// read the value of the analogue pin
int temperature = analogRead(A0);
// fx is used to define which pixel on the blue-red gradient is selected
// blue section
byte fx = 0;
// linear gradient section
if (temperature >= 397 && temperature <= 512)
{
fx = v;
} else if (temperature > 512) // red section
{
fx = 512;
}
// convert 0 to 512 to value between 0 and 255
byte r = fx / 512 * 255;
// blue is the opposite of r
byte b = 255 - r;
// set the colour of the strip
setAll(strip.Color(r, 0, b));
}
void setAll(uint32_t color)
{
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
}
strip.show();
}
// converts a byte to rgb array
byte * Wheel(byte WheelPos) {
// creates an array of bytes representing the RGB colour
static byte rgb[3];
// Red + Green section
if(WheelPos < 255) {
rgb[0] = WheelPos;
rgb[1] = 255 - WheelPos;
rgb[2] = 0;
} else if(WheelPos < 2*255) { // Green + Blue section
WheelPos -= 255;
rgb[0] = 255 - WheelPos;
rgb[1] = 0;
rgb[2] = WheelPos;
} else { // Blue + Red section
WheelPos -= 2 * 255;
rgb[0] = 0;
rgb[1] = WheelPos;
rgb[2] = 255 - WheelPos;
}
return rgb;
}
// ask_transmitter.pde
// -*- mode: C++ -*-
#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h> // Not actually used but needed to compile
#endif
RH_ASK driver(2000, 15, 14, 0);
// define pins
const int BIT_1 = 7;
const int BIT_2 = 8;
const int BIT_3 = 9;
const int IS_TURNED_ON_PIN = 10;
const int COLOUR_WHEEL = A2;
void setup()
{
#ifdef RH_HAVE_SERIAL
Serial.begin(9600); // Debugging only
#endif
if (!driver.init())
#ifdef RH_HAVE_SERIAL
Serial.println("init failed");
#else
;
#endif
pinMode(BIT_1, INPUT);
pinMode(BIT_2, INPUT);
pinMode(BIT_3, INPUT);
pinMode(IS_TURNED_ON_PIN, INPUT);
pinMode(COLOUR_WHEEL, INPUT);
}
void loop()
{
// get values
bool turnOn = digitalRead(IS_TURNED_ON_PIN);
bool b1 = digitalRead(BIT_1);
bool b2 = digitalRead(BIT_2);
bool b3 = digitalRead(BIT_3);
// get the mode
bool modeBits[] = {b1, b2, b3};
byte mode = bitArrayToByte(modeBits);
// get the wheel colour
unsigned int wheel = analogRead(COLOUR_WHEEL);
byte* rgb = Wheel(wheel);
// define the message
byte msg[] = {mode,turnOn, rgb[0], rgb[1], rgb[2]};
// transmit the message
driver.send(msg, sizeof(msg));
driver.waitPacketSent();
delay(10);
}
// converts an array of bits (e.g. [1,0,0,1,0,1,1,1]) to a byte
byte bitArrayToByte(bool* bits) {
byte result = 0;
for (int i = 0; i <= sizeof(bits); i++) {
result = result | (bits[i] << i);
}
return result;
}
// converts a byte to rgb array
byte * Wheel(byte WheelPos) {
// creates an array of bytes representing the RGB colour
static byte rgb[3];
// Red + Green section
if(WheelPos < 255) {
rgb[0] = WheelPos;
rgb[1] = 255 - WheelPos;
rgb[2] = 0;
} else if(WheelPos < 2*255) { // Green + Blue section
WheelPos -= 255;
rgb[0] = 255 - WheelPos;
rgb[1] = 0;
rgb[2] = WheelPos;
} else { // Blue + Red section
WheelPos -= 2 * 255;
rgb[0] = 0;
rgb[1] = WheelPos;
rgb[2] = 255 - WheelPos;
}
return rgb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment