Optimize code with predefined variables with same prefix - arduino

I'm well on my way on learning coding for Arduino.
For the following case I Googled my ass off, but can't find any helpful answer.
So, asking here. Maybe you can help me with some terminology to Google, or direct links where this is elaborated.
I want to compress the following code. The predefined colors have to be as they are.
All variables have the same prefix, namely COLOR_. Is there any way to compress the part that is in the setup?
See following link.
// Define the colors of remote control in RGB values
// R G B
#define COLOR_0 { 0, 0, 0}
#define COLOR_1 {255, 0, 0} // Red
#define COLOR_2 {255, 160, 25} // Orange
#define COLOR_3 {255, 255, 0} // Yellow
#define COLOR_4 {127, 255, 127} // Pink
#define COLOR_5 { 0, 255, 0} // Green
#define COLOR_6 { 0, 255, 255} // Cyan
#define COLOR_7 { 0, 127, 255} // Light blue
#define COLOR_8 { 0, 0, 255} // Blue
#define COLOR_9 {191, 0, 255} // light green
uint16_t RGB_565(uint8_t red, uint8_t green, uint8_t blue){
// 16-bit value to express the RGB565-color:
// |R|R|R|R|R|G|G|G|G|G|G|B|B|B|B|B|
uint16_t red_565 = red / 8;
uint16_t green_565 = green / 4;
uint16_t blue_565 = blue / 8;
uint16_t mixed_565 = (red_565 << 11) | (green_565 << 5) | blue_565;
return mixed_565;
}
void setup(){
// Convert the preset colors to a RGB565-value
const uint8_t tint_1[] = COLOR_1;
uint16_t color_1 = RGB_565(tint_1[0], tint_1[1], tint_1[2]);
const uint8_t tint_2[] = COLOR_2;
uint16_t color_2 = RGB_565(tint_2[0], tint_2[1], tint_2[2]);
const uint8_t tint_3[] = COLOR_3;
uint16_t color_3 = RGB_565(tint_3[0], tint_3[1], tint_3[2]);
const uint8_t tint_4[] = COLOR_4;
uint16_t color_4 = RGB_565(tint_4[0], tint_4[1], tint_4[2]);
// etc...
// This part should be able to be compressed
}
void loop(){
// Not relevant
}

What about
#define COLOR_1 255, 0, 0
Then simply call
RGB_565(COLOR_1);

Related

Why this adafruit screen code interferes with this code?

I have a code for artificial neural network on Arduino. I'm using an Arduino UNO at 16Mhz and a SSD1306 led screen. The ANN is a simple logic XOR function. The code generates two random numbers 1.0 or 0.0, then passes the values to the inputs of the ANN, then the ANN inference the value and prints the inference time in milliseconds on the led screen, after that, prints on Serial monitor the input values, the output value and the inference time. Complete code is:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <math.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
volatile unsigned long adcTime = 0;
const float NEFTIS_E = 2.71828183;
const int NEFTIS_ANN_LENGHT = 13; // Total neurons in the network.
const int NEFTIS_OUT_LENGHT = 1; // Total output neurons.
const int NEFTIS_INPUT_LENGHT = 2; // Total input neurons.
enum ENUM_ActivationFuntionType
{
AF_Logistic, // Logistic sigmoid.
AF_TanH, // Hypervolic tangent.
AF_ArcTan, // Arc tangent.
AF_ReLU, // Rectified Linear Unit.
AF_LeakyReLU, // Leaky ReLU.
AF_ELU, // Exponentian Linear Unit.
AF_SoftPlus, //
AF_Boolean, // Boolean.
};
// Synapse structure
typedef struct
{
float weight;
int inputNeuron;
} struct_synapse;
// Neuron structure
typedef struct
{
float value;
float bias;
int indexStart;
int nInputs;
bool isInput;
ENUM_ActivationFuntionType activationFunction;
float linearDelta;
} struct_neuron;
struct_neuron Neftis_Neurons[] = {
{1.0f, 0, 0, 0, true, AF_Logistic, 0.01},
{1.0f, 0, 0, 0, true, AF_Logistic, 0.01},
{0.0f, 0, 0, 2, false, AF_Logistic, 0.01},
{0.0f, 0, 2, 2, false, AF_Logistic, 0.01},
{0.0f, 0, 4, 2, false, AF_Logistic, 0.01},
{0.0f, 0, 6, 2, false, AF_Logistic, 0.01},
{0.0f, 0, 8, 2, false, AF_Logistic, 0.01},
{0.0f, 0, 10, 2, false, AF_Logistic, 0.01},
{0.8f, 0, 12, 2, false, AF_Logistic, 0.01},
{0.0f, 0, 14, 2, false, AF_Logistic, 0.01},
{0.0f, 0, 16, 2, false, AF_Logistic, 0.01},
{0.0f, 0, 18, 2, false, AF_Logistic, 0.01},
{0.0f, 0, 20, 10, false, AF_Logistic, 0.01}
};
struct_synapse Neftis_Synapses[] = {
{-5.43316f, 0},
{-5.543714f, 1},
{-4.262666f, 0},
{1.139778f, 1},
{2.446421f, 0},
{-6.2367f, 1},
{-2.993009f, 0},
{-0.5112332f, 1},
{-3.330907f, 0},
{0.02449156f, 1},
{-1.701479f, 0},
{-1.936165f, 1},
{0.5805757f, 0},
{1.178081f, 1},
{-3.712192f, 0},
{0.5605027f, 1},
{-4.816404f, 0},
{-4.946669f, 1},
{-3.816507f, 0},
{0.6793132f, 1},
{-9.65515f, 2},
{2.788617f, 3},
{5.523755f, 4},
{0.8867579f, 5},
{1.384886f, 6},
{-0.4491375f, 7},
{-3.600839f, 8},
{1.99833f, 9},
{-6.788064f, 10},
{2.101213f, 11}
};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);/*
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();*/
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
float inputs[NEFTIS_INPUT_LENGHT];
inputs[0] = (float) random(0, 2);
inputs[1] = (float) random(0, 2);
Neftis_SetInputs(inputs);
unsigned long start = micros();
Neftis_Run();
adcTime = micros() - start;
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(1, 16); // Start at top-left corner
display.print(adcTime);
Serial.println(Neftis_Neurons[0].value);
Serial.println(Neftis_Neurons[1].value);
Serial.println(Neftis_Neurons[12].value);
display.display();
Serial.println(adcTime);
digitalWrite(LED_BUILTIN, true);
delay(1000);
digitalWrite(LED_BUILTIN, false);
delay(500);
}
void Neftis_SetInputs(float inputs[])
{
for(int i = 0; i < NEFTIS_INPUT_LENGHT; i++)
{
Neftis_Neurons[i].value = inputs[i];
}
}
void Neftis_Run()
{
// for every neuron
for(int i = 0; i < NEFTIS_ANN_LENGHT; i++)
{
float sum = 0.0f;
for(int j = 0; j < Neftis_Neurons[i].nInputs; j++)
{
// sums the inputs
sum += Neftis_Synapses[Neftis_Neurons[i].indexStart + j].weight * Neftis_Neurons[Neftis_Synapses[Neftis_Neurons[i].indexStart + j].inputNeuron].value;
}
sum += Neftis_Neurons[i].bias;
// apply activation function if is not input neuron
if(Neftis_Neurons[i].isInput == false)
{
// >> Logistic
if(Neftis_Neurons[i].activationFunction == AF_Logistic){
Neftis_Neurons[i].value = (float) (1 / (1 + pow(NEFTIS_E, -sum)));
// >> TanH.
}
}
}
}
void Neftis_GetOutputs(float* outArray)
{
for(int i = 0; i < NEFTIS_OUT_LENGHT; i++)
{
outArray[i] = Neftis_Neurons[NEFTIS_ANN_LENGHT - NEFTIS_OUT_LENGHT + i].value;
}
}
The problem is, the screen don't show anything and the serial monitor shows strange values for the inputs, output and inference time, as show in image below.
But if I remove the lines
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();*/
and
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(1, 16); // Start at top-left corner
then the serial monitor shows the right values for inputs, output and elapsed time for inference.
Now, I modified this line of code from this:
Neftis_Neurons[i].value = (float) (1 / (1 + pow(NEFTIS_E, -sum)));
to this:
Neftis_Neurons[i].value = (float) (1 / (1 + pow(NEFTIS_E, -1.5)));
and then the screen works and shows the inference time. The question is simple, why the screen code is interfering with pow function and serial monitor? or pow function is interfering with screen and serial monitor code? By the way, I executed the same code on a Raspberry Pi Pico and works ok, no issues there.
Thank you very much, I hope you find the answer.
I followed the suggestion of CherryDT and posted my question on the Arduino forum and was answered in [https://forum.arduino.cc/t/why-this-adafruit-screen-code-interferes-with-this-other-code/1049580]this link1
You are running out of dynamic memory on the Uno, which causes unpredictable errors during run time.
This line allocates a screen buffer of 1024 bytes, which won't be shown in the memory usage report during compiling/uploading:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
To save dynamic memory space, you can put your other data arrays in program memory using the PROGMEM keyword. Check the Arduino reference for usage.
I tried the same code but with a smaller ANN model and it worked.

ILI9488 TFT LCD Arduino Shield - Can't use Touch and SDcard reading in the same program

I have a problem with my ILI9488 TFT Touch LCD module (Arduino Uno Shield). (320x480)
I can show .bmp pictures on the screen, read out of a SD-card.
In another testprogram, I can Serial.print() a char when I touch the display. (That's all it needs to do)
But when I merge the two programs together, it doesnt't work anymore.
I think the libraries are interferring with each other.
Is there any way to fix this, or can anyone recommend two compatible libraries to fix this?
Thanks in advance!
//----------------------------------------------------------------------------------------
// Declaration MCUFRIEND
//----------------------------------------------------------------------------------------
#include "MCUFRIEND_kbv.h"
MCUFRIEND_kbv tft;
#define LOWFLASH (defined(__AVR_ATmega328P__) && defined(MCUFRIEND_KBV_H_))
//----------------------------------------------------------------------------------------
// Declaration SDCard & Dependencies
//----------------------------------------------------------------------------------------
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <stdint.h>
#include "TouchScreen.h"
#define YP A2
#define XM A3
#define YM 8
#define XP 9
#define MINPRESSURE 1000
#define MAXPRESSURE 10000
//---------------------------------------------------------------------------------------
///Variables & Constants
#define BLACK 0x0000 /* 0, 0, 0 */
#define NAVY 0x000F /* 0, 0, 128 */
#define DARKGREEN 0x03E0 /* 0, 128, 0 */
#define DARKCYAN 0x03EF /* 0, 128, 128 */
#define MAROON 0x7800 /* 128, 0, 0 */
#define PURPLE 0x780F /* 128, 0, 128 */
#define OLIVE 0x7BE0 /* 128, 128, 0 */
#define LIGHTGREY 0xC618 /* 192, 192, 192 */
#define DARKGREY 0x7BEF /* 128, 128, 128 */
#define BLUE 0x001F /* 0, 0, 255 */
#define GREEN 0x07E0 /* 0, 255, 0 */
#define CYAN 0x07FF /* 0, 255, 255 */
#define RED 0xF800 /* 255, 0, 0 */
#define MAGENTA 0xF81F /* 255, 0, 255 */
#define YELLOW 0xFFE0 /* 255, 255, 0 */
#define WHITE 0xFFFF /* 255, 255, 255 */
#define ORANGE 0xFDA0 /* 255, 180, 0 */
#define GREENYELLOW 0xB7E0 /* 180, 255, 0 */
#define PINK 0xFC9F
#define SD_CS 10 // Chip Select from SPI Interface
#define LOGO "default"
#define AUTHUSER "authuser"
#define ADDUSER "adduser"
#define PALETTEDEPTH 4
File root;
char ReadMode = '0'; // Sleep = 0, Default = 1, Finger ID = 2, Finger Enroll = 3
char namebuf[32] = "/"; // BMP=Files in the root directory
int x, y, pathlen, count;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
bool pressed = false;
void setup()
{
uint16_t ID = tft.readID();
Serial.begin(9600);
tft.begin(ID);
//Please select the mode you want to use
//Sleep --> 0
//Default --> 1
//FingerID --> 2
//FingerEnroll --> 3
//Getting SDCard Ready
bool good = SD.begin(SD_CS);
root = SD.open(namebuf);
pathlen = strlen(namebuf);
x = 0;
y = 0;
}
void loop(void)
{
if (Serial.available()) {
ReadMode = Serial.read();
}
if (ReadMode == '0') {
tft.fillScreen(NULL);
}
if(ReadMode == '1') // The Default HomeScreen displays, Read from the SDCard
{
for(int i = 0; i < 5; i++)
{
char *nm = namebuf + pathlen;
File f = root.openNextFile();
uint8_t ret;
uint32_t start;
if (f != NULL)
{
#ifdef USE_SDFAT
f.getName(nm, 32 - pathlen);
#else
strcpy(nm, (char *)f.name());
#endif
f.close();
strlwr(nm);
if (strstr(nm, ".bmp") != NULL && strstr(nm, LOGO) != NULL)
{
ret = showBMP(namebuf, x, y);
}
}
else root.rewindDirectory();
}
}
if (ReadMode == '2') // The AuthUser displays, Read from the SDCard
{
for(int i = 0; i < 5; i++)
{
char *nm = namebuf + pathlen;
File f = root.openNextFile();
uint8_t ret;
uint32_t start;
if (f != NULL)
{
#ifdef USE_SDFAT
f.getName(nm, 32 - pathlen);
#else
strcpy(nm, (char *)f.name());
#endif
f.close();
strlwr(nm);
if (strstr(nm, ".bmp") != NULL && strstr(nm, AUTHUSER) != NULL)
{
ret = showBMP(namebuf, x, y);
}
}
else root.rewindDirectory();
}
}
TSPoint p = ts.getPoint();
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) { // TOUCH
Serial.println('X');
}
}
//----------------------------------------------------------------------------------------
// Methods for reading from SDCard
//----------------------------------------------------------------------------------------
#define BMPIMAGEOFFSET 54
#define BUFFPIXEL 20
uint16_t read16(File& f) {
uint16_t result; // read little-endian
f.read((uint8_t*)&result, sizeof(result));
return result;
}
uint32_t read32(File& f) {
uint32_t result;
f.read((uint8_t*)&result, sizeof(result));
return result;
}
uint8_t showBMP(char *nm, int x, int y)
{
File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24, 16, 8, 4, 1)
uint32_t bmpImageoffset; // Start of image data in file
uint32_t rowSize; // Not always = bmpWidth; may have padding
uint8_t sdbuffer[3 * BUFFPIXEL]; // pixel in buffer (R+G+B per pixel)
uint16_t lcdbuffer[(1 << PALETTEDEPTH) + BUFFPIXEL], *palette = NULL;
uint8_t bitmask, bitshift;
boolean flip = true; // BMP is stored bottom-to-top
int w, h, row, col, lcdbufsiz = (1 << PALETTEDEPTH) + BUFFPIXEL, buffidx;
uint32_t pos; // seek position
boolean is565 = false; //
uint16_t bmpID;
uint16_t n; // blocks read
uint8_t ret;
if ((x >= tft.width()) || (y >= tft.height()))
return 1; // off screen
bmpFile = SD.open(nm); // Parse BMP header
bmpID = read16(bmpFile); // BMP signature"
(void) read32(bmpFile); // Read & ignore file size
(void) read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data
(void) read32(bmpFile); // Read & ignore DIB header size
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
n = read16(bmpFile); // # planes -- must be '1'
bmpDepth = read16(bmpFile); // bits per pixel
pos = read32(bmpFile); // format
if (bmpID != 0x4D42) ret = 2; // bad ID
else if (n != 1) ret = 3; // too many planes
else if (pos != 0 && pos != 3) ret = 4; // format: 0 = uncompressed, 3 = 565
else if (bmpDepth < 16 && bmpDepth > PALETTEDEPTH) ret = 5; // palette
else {
bool first = true;
is565 = (pos == 3); // ?already in 16-bit format
// BMP rows are padded (if needed) to 4-byte boundary
rowSize = (bmpWidth * bmpDepth / 8 + 3) & ~3;
if (bmpHeight < 0) { // If negative, image is in top-down order.
bmpHeight = -bmpHeight;
flip = false;
}
w = bmpWidth;
h = bmpHeight;
if ((x + w) >= tft.width()) // Crop area to be loaded
w = tft.width() - x;
if ((y + h) >= tft.height()) //
h = tft.height() - y;
if (bmpDepth <= PALETTEDEPTH) { // these modes have separate palette
bmpFile.seek(bmpImageoffset - (4<<bmpDepth)); //54 for regular, diff for colorsimportant
bitmask = 0xFF;
if (bmpDepth < 8)
bitmask >>= bmpDepth;
bitshift = 8 - bmpDepth;
n = 1 << bmpDepth;
lcdbufsiz -= n;
palette = lcdbuffer + lcdbufsiz;
for (col = 0; col < n; col++) {
pos = read32(bmpFile); //map palette to 5-6-5
palette[col] = ((pos & 0x0000F8) >> 3) | ((pos & 0x00FC00) >> 5) | ((pos & 0xF80000) >> 8);
}
}
// Set TFT address window to clipped image bounds
tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
for (row = 0; row < h; row++) { // For each scanline...
uint8_t r, g, b, *sdptr;
int lcdidx, lcdleft;
if (flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
else // Bitmap is stored top-to-bottom
pos = bmpImageoffset + row * rowSize;
if (bmpFile.position() != pos) { // Need seek?
bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
for (col = 0; col < w; ) { //pixels in row
lcdleft = w - col;
if (lcdleft > lcdbufsiz) lcdleft = lcdbufsiz;
for (lcdidx = 0; lcdidx < lcdleft; lcdidx++) { // buffer at a time
uint16_t color;
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
r = 0;
}
switch (bmpDepth) { // Convert pixel from BMP to TFT format
case 24:
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
color = tft.color565(r, g, b);
break;
case 16:
b = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
if (is565)
color = (r << 8) | (b);
else
color = (r << 9) | ((b & 0xE0) << 1) | (b & 0x1F);
break;
case 1:
case 4:
case 8:
if (r == 0)
b = sdbuffer[buffidx++], r = 8;
color = palette[(b >> bitshift) & bitmask];
r -= bmpDepth;
b <<= bmpDepth;
break;
}
lcdbuffer[lcdidx] = color;
}
tft.pushColors(lcdbuffer, lcdidx, first);
first = false;
col += lcdidx;
} // end cols
} // end rows
tft.setAddrWindow(0, 0, tft.width() - 1, tft.height() - 1); //restore full screen
ret = 0; // good render
}
bmpFile.close();
return (ret);
}

Arduino RGB LED Control from 2 pots

I'm trying to write some code that reads a voltage from 2 different pots and converts that to 3 pwm outputs that I can then send to an RGB LED. My idea is to use something like a colour map that is used to plot complex functions, but I'm not sure how to implement that. Any suggestions?
#define COLOUR_POT_INPUT 4
#define INTENSITY_POT_INPUT 3
#define LED_RED 9
#define LED_GREEN 10
#define LED_BLUE 11
float colour_angle;
float colour_radius;
float colour_x_value;
float colour_y_value;
int red_value;
int green_value;
int blue_value;
const float pi = 3.1415;
void setup() {
pinMode(COLOUR_POT_INPUT, INPUT);
pinMode(INTENSITY_POT_INPUT, INPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
}
void loop() {
colour_angle = analogRead(COLOUR_POT_INPUT);
colour_radius = analogRead(INTENSITY_POT_INPUT);
colour_angle = map(colour_angle, 0, 1023, 0, 2*pi);
colour_radius = map(colour_radius, 0, 1023, 0, 255);
colour_x_value = colour_radius*cos(colour_angle);
colour_y_value = colour_radius*sin(colour_angle);
}
//Insert function here that maps colour x and y value to red green blue
value
Assuming colour_angle and colour_radius represent hue and saturation respectively, then you can use any HSL/HSV-to-RGB conversion code, with a fixed lightness/value.
Picking randomly from google results:
HSL to RGB conversion
HSV to RGB conversion

FSR presses to increase variable on a display

Update: So I'm getting the error: "expected initializer before 'fsrAnalogPin'" with this code. Can someone help me sort this out?
// testshapes demo for Adafruit RGBmatrixPanel library.
// Demonstrates the drawing abilities of the RGBmatrixPanel library.
// For 32x32 RGB LED matrix:
// http://www.adafruit.com/products/607
// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
// If your 32x32 matrix has the SINGLE HEADER input,
// use this pinout:
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
#define PULLUP true //use the AVR's internal pullup resistor
#define INVERT true //low level means fsr pressed
#define DEBOUNCE_TIME 50 //milliseconds
// If your matrix has the DOUBLE HEADER input, use:
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
//#define LAT 9
//#define OE 10
//#define A A3
//#define B A2
//#define C A1
//#define D A0
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
void loop(){
Serial.begin(9600);
matrix.begin();
// draw a pixel in solid white
matrix.drawPixel(0, 0, matrix.Color333(7, 7, 7));
delay(500);
// fix the screen with green
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 7, 0));
delay(500);
// draw a box in yellow
matrix.drawRect(0, 0, 32, 32, matrix.Color333(7, 7, 0));
delay(500);
// draw an 'X' in red
matrix.drawLine(0, 0, 31, 31, matrix.Color333(7, 0, 0));
matrix.drawLine(31, 0, 0, 31, matrix.Color333(7, 0, 0));
delay(500);
// draw a blue circle
matrix.drawCircle(10, 10, 10, matrix.Color333(0, 0, 7));
delay(500);
// fill a violet circle
matrix.fillCircle(21, 21, 10, matrix.Color333(7, 0, 7));
delay(500);
// fill the screen with 'black'
matrix.fillScreen(matrix.Color333(0, 0, 0));
// draw some text!
matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
matrix.setTextSize(1); // size 1 == 8 pixels high
matrix.setTextWrap(false); // Don't wrap at end of line - will do ourselves
matrix.setTextColor(matrix.Color333(7,7,7));
matrix.println("FIST");
matrix.println(" BUMP");
// print each letter with a rainbow color
matrix.setTextColor(matrix.Color333(7,0,0));
matrix.print('C');
matrix.setTextColor(matrix.Color333(7,4,0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(7,7,0));
matrix.print('U');
matrix.setTextColor(matrix.Color333(4,7,0));
matrix.print('N');
matrix.setTextColor(matrix.Color333(0,7,0));
matrix.println('T');
{
const int fsr_pin = A4; //connect fsr from this pin to ground
int read fsrAnalogPin = 4;
fsr_pin.read();
if( fsr_pin.wasPressed() ){
counter = counter + 1
matrix.setTextColor( matrix.Color333(0,7,7) );
matrix.print( counter )
}
}
// whew!
}
void loop() {
// do nothing
}
if someone can help me with my code I would be very grateful. What I'm trying to do is: Using a force-sensitive resistor, I want to count a given amount of pressure on the FSR with an arduino, that then displays each tap on an LED matrix. In other words, I have a glove with an FSR on it, and I want to count "fist bumps" that then display on an 32x32 Adafruit LED matrix.
Here's an example of what I want it to look like:
https://drive.google.com/file/d/0B4kq-ADrtz4mSzNWUTJoNGlBSU0/view?usp=sharing
That is just a static number on the bottom though. I need help making the code to connect a counting variable to the FSR "bumps." It's probably pretty simple but I'm entirely new to this so it's a lot to learn. So any help is appreciated
Here is what I have for code:
// testshapes demo for Adafruit RGBmatrixPanel library.
// Demonstrates the drawing abilities of the RGBmatrixPanel library.
// For 32x32 RGB LED matrix:
// http://www.adafruit.com/products/607
// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
// If your 32x32 matrix has the SINGLE HEADER input,
// use this pinout:
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
#define FSR_PIN A4 //connect fsr from this pin to ground
#define PULLUP true //use the AVR's internal pullup resistor
#define INVERT true //low level means fsr pressed
#define DEBOUNCE_TIME 50 //milliseconds
// If your matrix has the DOUBLE HEADER input, use:
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
//#define LAT 9
//#define OE 10
//#define A A3
//#define B A2
//#define C A1
//#define D A0
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
void setup() {
Serial.begin(9600);
matrix.begin();
// draw a pixel in solid white
matrix.drawPixel(0, 0, matrix.Color333(7, 7, 7));
delay(500);
// fix the screen with green
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 7, 0));
delay(500);
// draw a box in yellow
matrix.drawRect(0, 0, 32, 32, matrix.Color333(7, 7, 0));
delay(500);
// draw an 'X' in red
matrix.drawLine(0, 0, 31, 31, matrix.Color333(7, 0, 0));
matrix.drawLine(31, 0, 0, 31, matrix.Color333(7, 0, 0));
delay(500);
// draw a blue circle
matrix.drawCircle(10, 10, 10, matrix.Color333(0, 0, 7));
delay(500);
// fill a violet circle
matrix.fillCircle(21, 21, 10, matrix.Color333(7, 0, 7));
delay(500);
// fill the screen with 'black'
matrix.fillScreen(matrix.Color333(0, 0, 0));
// draw some text!
matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
matrix.setTextSize(1); // size 1 == 8 pixels high
matrix.setTextWrap(false); // Don't wrap at end of line - will do ourselves
matrix.setTextColor(matrix.Color333(7,7,7));
matrix.println("FIST");
matrix.println(" BUMP");
// print each letter with a rainbow color
matrix.setTextColor(matrix.Color333(7,0,0));
matrix.print('C');
matrix.setTextColor(matrix.Color333(7,4,0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(7,7,0));
matrix.print('U');
matrix.setTextColor(matrix.Color333(4,7,0));
matrix.print('N');
matrix.setTextColor(matrix.Color333(0,7,0));
matrix.println('T');
void loop(void)
{
fsr.read();
if (fsr.wasPressed()) {
Serial.print(++counter, DEC);
Serial.println(" steps");
}
matrix.setTextColor(matrix.Color333(0,7,7));
matrix.print("29291");
// whew!
}
void loop() {
// do nothing
}
Firstly, you need to remove the loop() method from setup().
In the void loop() method, making following changes should get things working.
void loop(){
fsr.read();
if( fsr.wasPressed() ){
counter = counter + 1
matrix.setTextColor( matrix.Color333(0,7,7) );
matrix.print( counter )
}
}
EDIT
// testshapes demo for Adafruit RGBmatrixPanel library.
// Demonstrates the drawing abilities of the RGBmatrixPanel library.
// For 32x32 RGB LED matrix:
// http://www.adafruit.com/products/607
// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
// If your 32x32 matrix has the SINGLE HEADER input,
// use this pinout:
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
#define PULLUP true //use the AVR's internal pullup resistor
#define INVERT true //low level means fsr pressed
#define DEBOUNCE_TIME 50 //milliseconds
// If your matrix has the DOUBLE HEADER input, use:
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
//#define LAT 9
//#define OE 10
//#define A A3
//#define B A2
//#define C A1
//#define D A0
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
void setup(){
Serial.begin(9600);
matrix.begin();
// draw a pixel in solid white
matrix.drawPixel(0, 0, matrix.Color333(7, 7, 7));
delay(500);
// fix the screen with green
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 7, 0));
delay(500);
// draw a box in yellow
matrix.drawRect(0, 0, 32, 32, matrix.Color333(7, 7, 0));
delay(500);
// draw an 'X' in red
matrix.drawLine(0, 0, 31, 31, matrix.Color333(7, 0, 0));
matrix.drawLine(31, 0, 0, 31, matrix.Color333(7, 0, 0));
delay(500);
// draw a blue circle
matrix.drawCircle(10, 10, 10, matrix.Color333(0, 0, 7));
delay(500);
// fill a violet circle
matrix.fillCircle(21, 21, 10, matrix.Color333(7, 0, 7));
delay(500);
// fill the screen with 'black'
matrix.fillScreen(matrix.Color333(0, 0, 0));
// draw some text!
matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
matrix.setTextSize(1); // size 1 == 8 pixels high
matrix.setTextWrap(false); // Don't wrap at end of line - will do ourselves
matrix.setTextColor(matrix.Color333(7,7,7));
matrix.println("FIST");
matrix.println(" BUMP");
// print each letter with a rainbow color
matrix.setTextColor(matrix.Color333(7,0,0));
matrix.print('C');
matrix.setTextColor(matrix.Color333(7,4,0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(7,7,0));
matrix.print('U');
matrix.setTextColor(matrix.Color333(4,7,0));
matrix.print('N');
matrix.setTextColor(matrix.Color333(0,7,0));
matrix.println('T');
}
void loop(){
const int fsr_pin = A4; //connect fsr from this pin to ground
int read fsrAnalogPin = 4;
fsr_pin.read();
if( fsr_pin.wasPressed() ){
counter = counter + 1
matrix.setTextColor( matrix.Color333(0,7,7) );
matrix.print( counter )
}
}
I will also suggest following proper indentation. That will make your code readable. It would be also great if you revise your Arduino prog. concepts.

RGB-led cycle colours of the rainbow arduino

I am fairly new to programming and have bought an arduino uno for practice. I want to make an RGB-led light up circling the colours of the rainbow (cyan - blue - magenta etc..)
There is some sort of logical problem with my code. The first circle is fine, it goes through the colors nicely. It is in the second circle that the red color starts acting up and flashes 9 times when it is not supposed to in the "blue segment of the circle".
My whole code is like this:
int BLUEARR[] = {1,1,1,0,0,0};
int GREENARR[] = {1, 0, 0, 0, 1, 1};
int REDARR[] = {0, 0, 1, 1, 1, 0};
int red, green, blue;
int RedPin = 9;
int GreenPin = 10;
int BluePin = 11;
void setup() {
}
void loop() {
for (int i=0; i<7; i++) {
int k = (i+1)%6;
for (int j=0; j<256; j++) {
blue = BLUEARR[i]*255+(BLUEARR[k] - BLUEARR[i])*j;
red = REDARR[i]*255+(REDARR[k] - REDARR[i])*j;
green = GREENARR[i]*255+(GREENARR[k] - GREENARR[i])*j;
analogWrite (RedPin, red);
analogWrite (GreenPin, green);
analogWrite (BluePin, blue);
delay(20);
}
}
}
anyone knows? Kind regards.

Resources