led light show using arduino - arduino

sketch_Show_The_Hat.ino:77:5: error: expected declaration before ‘}’ token
Can some please help figure where this error is coming from?
Here is my code:
#include "LPD8806.h"
#include "SPI.h"
//#define HOLIDAY_MODE
#define MOMENTARY_SWITCH
// Example to control LPD8806-based RGB LED Modules in a strip!
int dataPin = 2;
int clockPin = 3;
int onOffPin = 4;
// Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row
// The LED strips are 32 LEDs per meter but you can extend/cut the strip
LPD8806 strip = LPD8806(32, dataPin, clockPin);
void setup() {
// Start up the LED strip
strip.begin();
// Update the strip, to start they are all 'off'
strip.show();
randomSeed(analogRead(A0));
// On/Off switch
pinMode(onOffPin, INPUT_PULLUP);
}
int x = 0;
int dim = 0;
#ifdef MOMENTARY_SWITCH
int tdim = 0;
#endif
void loop();
int r, g, b, y, z;
#ifdef MOMENTARY_SWITCH
} if (digitalRead(onOffPin) == LOW) {
if ((dim == 0) && (tdim == 0)
tdim = !0;
} else if ((dim == 128) && (tdim != 0))
tdim = 0;
if (tdim) {
if (dim < 128) {
dim++;
} else {
}
if (dim > 0) {
dim--;
}
} if (digitalRead(onOffPin) == LOW) {
}
}
} if (dim < 128) {
dim++;
} else {
if (dim > 0) {
dim--;
}
}
#endif
for (int i = 0; i < strip.numPixels(); i++) {
y = (x + (i << 5)) & 511;
#ifdef HOLIDAY_MODE
// Holiday
if (y >= 256) {
y = 511 - y;
}
z = 255 - y;
r = (y <= 128) ? (y - 128) << 1 : 0;
g = (z <= 128) ? (z - 128) << 1 : 0;
b = 0;
# else
// Normal
if (y < 128) {
r = y;
g = 0;
b = 255 - y;
} else if (y < 256) {
r = 127;
g = y - 128;
b = 0;
} else if (y < 384) {
r = 127;
g = 127 - (y - 256);
b = 0;
} else {
r = 127 - (y - 384);
g = 0;
b = y - 384;
}
#endif
if (dim == 0) {
r = 0;
g = 0;
b = 0;
} else if (dim < 128) {
r = (r * dim) / 128;
g = (g * dim) / 128;
b = (b * dim) / 128;
}
strip.setPixelColor(i, r & 127, g & 127, b & 127);
}
strip.show();
x += 2;
x &= 511;
delay(5);
// for(int j=0; j < random(100,200); j++ ) {
// int i = random(0,strip.numPixels()-2) + 1;
// strip.setPixelColor(i-1, 43,54,219);
// strip.setPixelColor(i, 255,255,0);
// strip.setPixelColor(i+1, 255,69,0);
// strip.setPixelColor(i-1, 0,0,255);
// strip.setPixelColor(i, 255,0,0);
// strip.setPixelColor(i+1, 0,255,0);
// strip.show();
// delay(5);
// strip.setPixelColor(i-1, 255,128,0);
// strip.setPixelColor(i-4, 255,128,0);
// strip.setPixelColor(i+1, 4,0,8);
// strip.setPixelColor(i-4, 104,10,80);
// strip.setPixelColor(i-10, 43,54,219);
// strip.setPixelColor(i-30, 255,165,0);
// strip.setPixelColor(i+5, 219,125,45);
// strip.setPixelColor(i, 100,100,127);
// strip.setPixelColor(i+1, 104,100,198);
// strip.show();
// delay(1);
// strip.setPixelColor(i+32, 219,112,147);
//
// strip.setPixelColor(i+1, 255,69,0);
// strip.setPixelColor(i-2, 255,127,75);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i+5, 255,128,0);
// strip.setPixelColor(i+2, 10,227,100);
// strip.show();
// delay(0);
// strip.setPixelColor(i-1, 255,69,0);
// strip.setPixelColor(i, 255,69,0);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i-12, 148,0,211);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i-3, 148,0,211);
// strip.setPixelColor(i-1, 148,0,211);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i+2, 148,0,211);
// strip.show();
// delay(random(10,100));
// }
// for(int j=0; j < random(10,15); j++ ) {
// for (int i=32; i < strip.numPixels(); i++) {
// strip.setPixelColor(i, 0,0,255);
// }
// strip.show();
// delay(15);
// for (int i=2; i < strip.numPixels(); i++) {
// strip.setPixelColor(i, 255,110,0);
// }
// strip.show();
// delay(15);
// }

First of all I have corrected your formatting with an edit. The error is saying that in 77. line 5. character there is a mistake related with } character. The mistake is you did not declared any { before }.
When I tried to correct your formatting (on my version of your file) 77. line is an empty line. But the problem is probably in 76. line } if (digitalRead(onOffPin) == LOW) { so try to remove { character. I mean try to replace the line with if (digitalRead(onOffPin) == LOW) { and let's see the error persists or not.
Curly Braces: https://www.arduino.cc/en/reference/braces
Reminding: Please always pay attention to your formatting on StackOverflow while asking a question. Formatting the text especially in the question is the key to properly interchange the knowledge. While sharing one of your code file in here, formatting is a necessity. Because if you did not format your text (your codes actually) we can not test them.

Related

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);
}

Bop it Arduino Circuit Playground

I'm trying to create a bop it game with 2 players where:
Green LED – Gyro Sensor (Z-axis)
Red LED – Temperature sensor
Blue LED – Sound sensor
Yellow LED – Light Sensor
However when I press the left button to start the game, only the yellow light above GND goes on and doesn't respond to anything, what can be wrong with my code?
long randNumber;
int temp;
int val;
int number;
int score;
boolean state;
int light;
uint8_t pixeln = 0;
int sound;
int shake;
unsigned long lastmillis = 0;
boolean game;
const byte numPins = 7;
byte pins[] = {0, 1, 2, 3, 4, 5, 6, 7};
int player1 = 0;
int player2 = 0;
#include "Adafruit_CircuitPlayground.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
CircuitPlayground.begin();
CircuitPlayground.clearPixels();
//timer();
}
void loop() {
if (digitalRead(4) == HIGH && digitalRead(19) == LOW){
Serial.println("Button Pressed");
lastmillis = millis();
for (int i=0; i < 10; i ){
randNumber = random(4);
//Serial.println("Loop");
switch (randNumber){
//Serial.println("Switch");
case 0:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 255,0,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
temp = analogRead(A0);
//Serial.println(temp);
while (state == true) {
val = analogRead(A0);
number = val - temp;
if (number > 10){
score ;
state = false;
//Serial.println("Temperature");
delay(500);
}
}
break;
case 1:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 127,127,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
light = analogRead(A5);
//Serial.println(light);
while (state == true) {
val = analogRead(A5);
number = light - val;
if (number > 20){
score ;
state = false;
//Serial.println(number);
delay(500);
}
}
break;
case 2:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 0,0,255);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
sound = analogRead(A4);
//Serial.println(sound);
while (state == true) {
val = analogRead(A4);
number = val - sound;
if (number > 50){
score ;
state = false;
//Serial.println(number);
delay(500);
}
}
break;
case 3:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 0,255,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
val = CircuitPlayground.motionZ();
//Serial.println(val);
state = true;
while (state == true) {
shake = CircuitPlayground.motionZ();
number = val - shake;
//Serial.println(number);
if (number > 15 or number < -15){
score ;
state = false;
//Serial.println("shake");
delay(500);
}
}
break;
case 4:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(0, 0,255,0);
CircuitPlayground.setPixelColor(1, 0,255,0);
CircuitPlayground.strip.show();
delay(3000);
break;
case 5:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(3, 255,0,0);
CircuitPlayground.setPixelColor(4, 255,0,0);
CircuitPlayground.strip.show();
delay(3000);
break;
case 6:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(5, 127,0,255);
CircuitPlayground.setPixelColor(6, 127,0,255);
CircuitPlayground.strip.show();
delay(3000);
break;
case 7:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(8, 255,128,0);
CircuitPlayground.setPixelColor(9, 255,128,0);
CircuitPlayground.strip.show();
delay(3000);
}
}
if (CircuitPlayground.slideSwitch()) {
player1 = (millis() - lastmillis) / 1000;
Serial.println(player1);
CircuitPlayground.clearPixels();
byte num = player1;
for (byte i=0; i < numPins; i ){
byte state = bitRead(num, i);
if (state == 1){
CircuitPlayground.setPixelColor(pins[i], 255,255,255);
CircuitPlayground.strip.show();
}
}
} else {
player2 = (millis() - lastmillis) /1000;
Serial.println(player2);
CircuitPlayground.clearPixels();
byte num = player2;
for (byte i=0; i < numPins; i ){
byte state = bitRead(num, i);
if (state == 1){
CircuitPlayground.setPixelColor(pins[i], 255,255,255);
CircuitPlayground.strip.show();
}
}}
CircuitPlayground.playTone(330, 500);
}
while (digitalRead(19) == HIGH && digitalRead(4) == LOW){
if (player1 < player2){
CircuitPlayground.clearPixels();
delay(250);
for (int i =0; i < 5; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
else if (player1 > player2){
CircuitPlayground.clearPixels();
delay(250);
for (int i =5; i < 10; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
else {
CircuitPlayground.clearPixels();
delay(250);
for (int i =0; i < 10; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
}
if (digitalRead(19) == HIGH && digitalRead(4) == HIGH){
player1 = 0;
player2 = 0;
for (int a =0; a < 10; a ){
CircuitPlayground.clearPixels();
delay(100);
for (int i =0; i < 10; i ){
CircuitPlayground.setPixelColor(i, CircuitPlayground.colorWheel(25 * i));
CircuitPlayground.strip.show();
}
delay(100);
CircuitPlayground.clearPixels();
}
}
}
visual after pressing left button:

Arduino ignoring serial interrupts when using FastLED

I have tried several different methods found around the internet, however none of them seem to work. This code works for cases 0-2 but when it goes into case 3 which is the rainbow chase loop, the pressing of the button does not interrupt the loop and move the counter forward. I'm assuming, as usual, I'm missing something stupid so many thanks in advance.
#define FASTLED_ALLOW_INTERRUPTS 1
#define FASTLED_INTERRUPT_RETRY_COUNT 1
#include <FastLED.h>
#define AnalogIn A0
#define SwIn 2
#define LED_Out 12
#define NUM_LEDS 5
int pushCounterz = 0;
volatile int buttonState; // Set volatile for interrupt DO NOT SHAKE!
int lastButtonState;
CRGB leds[NUM_LEDS];
void setup() {
// put your setup code here, to run once:
FastLED.setMaxRefreshRate(250);
FastLED.addLeds<WS2812, LED_Out, GRB>(leds, NUM_LEDS);
pinMode(SwIn, INPUT);
pinMode(LED_Out, OUTPUT);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 255, 0, 255 );
}
FastLED.show();
delay(120);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 0 );
}
FastLED.show();
Serial.begin(115200);
Serial.println(pushCounterz);
lastButtonState = digitalRead(SwIn); // Set the button state to the startup state
attachInterrupt((SwIn-2), button_ISR, CHANGE); // Set SwIn button as an interrupt pin // Change to Low???
}
void loop() {
if (pushCounterz != 3) {
FastLED.show();
}
Serial.println(pushCounterz);
delay(120);
}
void button_ISR () {
buttonState = digitalRead(SwIn);
digitalWrite(13, buttonState);
if (buttonState == LOW && buttonState != lastButtonState) {
if (pushCounterz > 3) {
//Serial.println("Reset to 0: ");
pushCounterz = 0;
} else {
pushCounterz = pushCounterz + 1;
//Serial.println("Incerment");
}
//Serial.println(pushCounterz);
switch (pushCounterz) {
case 0:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB (255, 0, 0);
}
break;
case 1:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 255, 0);
}
break;
case 2:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 255);
}
break;
case 3:
theaterChaseRainbow(1,50);
break;
default:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 0);
}
break;
}
}
lastButtonState = buttonState;
}
// Theater-style crawling lights with rainbow effect
void theaterChaseRainbow(int cycles, int speed) { // TODO direction, duration
for (int j = 0; j < 256 * cycles; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
int pos = i + q;
leds[pos] = Wheel( (i + j) % 255); //turn every third pixel on
}
FastLED.show();
delay(speed);
for (int i = 0; i < NUM_LEDS; i = i + 3) {
leds[i + q] = CRGB::Black; //turn every third pixel off
}
}
}
}
CRGB Wheel(byte WheelPos) {
if (WheelPos < 85) {
return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if (WheelPos < 170) {
WheelPos -= 85;
return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
Your issue is not that the button is not changing the value, but rather your code has no exit point if it does; the button will change the value, but nothing in theaterChaseRainbow tells it to stop.
Simply add a check in the method to return out if the button state changes:
// Theater-style crawling lights with rainbow effect
void theaterChaseRainbow(int cycles, int speed) { // TODO direction, duration
for (int j = 0; j < 256 * cycles; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
int pos = i + q;
leds[pos] = Wheel( (i + j) % 255); //turn every third pixel on
}
FastLED.show();
if (pushCounterz != 3) return; //ADDED THIS HERE*****
delay(speed);
for (int i = 0; i < NUM_LEDS; i = i + 3) {
leds[i + q] = CRGB::Black; //turn every third pixel off
}
}
}
}
In addition, I would suggest simplifying your ISR to just increment the button and not have it handle the logic of the program as well. That should either be contained in the loop method or called from the loop method. This should make for some cleaner and less confusing code, as the ISR's job is simply to adjust the value of the button counter, and the loops job is to deal with the state that the program is currently in.
Also - you can't allow interrupts on AVR, or rather I should say it does nothing. I should put in a warning message when that's happening - AVR/arduino's ISR handling is so slow that even the clock tick ISR would be enough to disrupt writing out WS2812 data (resulting in FastLED cutting the frame off) so I yanked that code out of the avr WS2812 asm implementation. Most arm and esp platforms that FastLED supports do allow for interrupt handling to occur during in the small window between writing out each led's data - courtesy of their higher clock speeds.
If you're using an ARM or ESP based platform, then feel free to ignore this comment (mostly putting it here so folks who stumble on this question in a good search know what's up).
As a reference, the working code with the ISR cleanup. (mind you there is still some serial debugging code in there as I have more work to do with brightness etc)
#define FASTLED_ALLOW_INTERRUPTS 1
#define FASTLED_INTERRUPT_RETRY_COUNT 1
#include <FastLED.h>
#define AnalogIn A0
#define SwIn 2
#define LED_Out 12
#define NUM_LEDS 5
int pushCounterz = 4; // 4 = off
volatile int buttonState; // Set volatile for interrupt DO NOT SHAKE!
int lastButtonState;
CRGB leds[NUM_LEDS];
void setup() {
// put your setup code here, to run once:
FastLED.setMaxRefreshRate(250);
FastLED.addLeds<WS2812, LED_Out, GRB>(leds, NUM_LEDS);
pinMode(SwIn, INPUT);
pinMode(LED_Out, OUTPUT);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 255, 0, 255 );
}
FastLED.show();
delay(120);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 0 );
}
FastLED.show();
Serial.begin(19200);
Serial.println(pushCounterz);
lastButtonState = digitalRead(SwIn); // Set the button state to the startup state
attachInterrupt((SwIn-2), button_ISR, LOW); // Set SwIn button as an interrupt pin // Change to Low???
}
void loop() {
// if (pushCounterz != 3) {
//FastLED.show();
//Serial.println(pushCounterz);
// }
//delay(20);
switch (pushCounterz) {
case 0:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB (255, 0, 0);
}
FastLED.show();
break;
case 1:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 255, 0);
}
FastLED.show();
break;
case 2:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 255);
}
FastLED.show();
break;
case 3:
theaterChaseRainbow(1,50);
break;
default:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 0);
}
FastLED.show();
break;
}
}
void button_ISR () {
buttonState = digitalRead(SwIn);
//digitalWrite(13, buttonState);
if (buttonState == LOW && buttonState != lastButtonState) {
if (pushCounterz > 3 || pushCounterz < 0) {
Serial.println("Reset to 0: ");
pushCounterz = 0;
} else {
pushCounterz = pushCounterz + 1;
Serial.println("Incerment");
}
Serial.println(pushCounterz);
}
lastButtonState = buttonState;
}
// Theater-style crawling lights with rainbow effect
void theaterChaseRainbow(int cycles, int speed) { // TODO direction, duration
for (int j = 0; j < 256 * cycles; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
int pos = i + q;
leds[pos] = Wheel( (i + j) % 255); //turn every third pixel on
}
FastLED.show();
if (pushCounterz != 3) return;
delay(speed);
for (int i = 0; i < NUM_LEDS; i = i + 3) {
leds[i + q] = CRGB::Black; //turn every third pixel off
}
}
}
}
CRGB Wheel(byte WheelPos) {
if (WheelPos < 85) {
return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if (WheelPos < 170) {
WheelPos -= 85;
return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
}
}

How to convert Ximea xiAPI camera data into QImage?

I have data from a camera in mono 8bit.
This is converted into an int vector using
std::vector<int> grayVector(size);
// convert / copy pointer data into vector: 8 bit
if (static_cast<XI_IMG_FORMAT>(format) == XI_MONO8)
{
quint8* imageIterator = reinterpret_cast<quint8*> (pMemVoid);
for (size_t count = 0; count < size; ++count)
{
grayVector[count] = static_cast<int>(*imageIterator);
imageIterator++;
}
}
Next, I need to convert this into a QImage. If I set the image format to QImage::Format_Mono the app crashes. With QImage::Format_RGB16 I get strippes, and with QImage::Format_RGB32 everything is black.
I would like to know how to do this the best, efficient and correct way?
// convert gray values into QImage data
QImage image = QImage(static_cast<int>(sizeX), static_cat<int>(sizeY), QImage::Format_RGB16);
for ( int y = 0; y < sizeY; ++y )
{
int yoffset = sizeY*y;
QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(y)) ;
for ( int x = 0; x < sizeX ; ++x )
{
int pos = x + yoffset;
int color = grayVector[static_cast<size_t>(pos)];
*line++ = qRgb(color, color, color);
}
}
The conversion to int is unnecessary and you do it in a very inefficient way; all you need is to use the QImage::Format_Grayscale8 available since Qt 5.5 (mid-2015).
Anyway, what you really want is a way to go from XI_IMG to QImage. The default BP_UNSAFE buffering policy should be adequate - the QImage will do a format conversion, so taking the data from XiApi's internal buffer is OK. Thus the following - all of the conversions are implemented in Qt and are quite efficient - much better than most any naive code.
I didn't check whether some Xi formats may need a BGR swap. If so, then the swap can be set to true in the format selection code and the rest will happen automatically.
See also: xiAPI manual.
static QVector<QRgb> grayScaleColorTable() {
static QVector<QRgb> table;
if (table.isEmpty()) {
table.resize(256);
auto *data = table.data();
for (int i = 0; i < table.size(); ++i)
data[i] = qRgb(i, i, i);
}
return table;
}
constexpr QImage::Format grayScaleFormat() {
return (QT_VERSION >= QT_VERSION_CHECK(5,5,0))
? QImage::Format_Grayscale8
: QImage::Format_Indexed8;
}
QImage convertToImage(const XI_IMG *src, QImage::Format f) {
Q_ASSERT(src->fmt == XI_MONO16);
Q_ASSERT((src->padding_x % 2) == 0);
if (src->fmt != XI_MONO16) return {};
const quint16 *s = static_cast<const quint16*>(src->bp);
const int s_pad = src->padding_x/2;
if (f == QImage::Format_BGR30 ||
f == QImage::Format_A2BGR30_Premultiplied ||
f == QImage::Format_RGB30 ||
f == QImage::Format_A2RGB30_Premultiplied)
{
QImage ret{src->width, src->height, f};
Q_ASSERT((ret->bytesPerLine() % 4) == 0);
const int d_pad = ret->bytesPerLine()/4 - ret->width();
quint32 *d = (quint32*)ret.bits();
if (s_pad == d_pad) {
const int N = (src->width + s_pad) * src->height - s_pad;
for (int i = 0; i < N; ++i) {
quint32 const v = (*s++) >> (16-10);
*d++ = 0xC0000000 | v << 20 | v << 10 | v;
}
} else {
for (int j = 0; j < src->height; ++j) {
for (int i = 0; i < src->width; ++i) {
quint32 const v = (*s++) >> (16-10);
*d++ = 0xC0000000u | v << 20 | v << 10 | v;
}
s += s_pad;
d += d_pad;
}
}
return ret;
}
QImage ret{src->width, src->height, grayScaleFormat()};
const int d_pad = ret->bytesPerLine() - ret->width();
auto *d = ret.bits();
if (s_pad == d_pad) {
const int N = (src->width + s_pad) * src->height - s_pad;
for (int i = 0; i < N; ++i) {
*d++ = (*s++) >> 8;
} else {
for (int j = 0; j < src->height; ++j) {
for (int i = 0; i < src->width; ++i)
*d++ = (*s++) >> 8;
s += s_pad;
d += d_pad;
}
}
return ret;
}
QImage fromXiImg(const XI_IMG *src, QImage::Format dstFormat = QImage::Format_ARGB32Premultiplied) {
Q_ASSERT(src->width > 0 && src->height > 0 && src->padding_x >= 0 && src->bp_size > 0);
Q_ASSERT(dstFormat != QImage::Format_Invalid);
bool swap = false;
int srcPixelBytes = 0;
bool externalConvert = false;
QImage::Format srcFormat = QImage::Format_Invalid;
switch (src->fmt) {
case XI_MONO8:
srcPixelBytes = 1;
srcFormat = grayScaleFormat();
break;
case XI_MONO16:
srcPixelBytes = 2;
externalConvert = true;
break;
case XI_RGB24:
srcPixelBytes = 3;
srcFormat = QImage::Format_RGB888;
break;
case XI_RGB32:
srcPixelBytes = 4;
srcFormat = QImage::Format_RGB32;
break;
};
if (srcFormat == QImage::Format_Invalid && !externalConvert) {
qWarning("Unhandled XI_IMG image format");
return {};
}
Q_ASSERT(srcPixelBytes > 0 && srcPixelBytes <= 4);
int bytesPerLine = src->width * srcPixelBytes + src->padding_x;
if ((bytesPerLine * src->height - src->padding_x) > src->bp_size) {
qWarning("Inconsistent XI_IMG data");
return {};
}
QImage ret;
if (!externalConvert)
ret = QImage{static_cast<const uchar*>(src->bp), src->width, src->height,
bytesPerLine, srcFormat};
else
ret = convertToImage(src, dstFormat);
if (ret.format() == QImage::Format_Indexed8)
ret.setColorTable(grayScaleColorTable());
if (ret.format() != dstFormat)
ret = std::move(ret).convertToFormat(dstFormat);
if (swap)
ret = std::move(ret).rgbSwapped();
if (!ret.isDetached()) // ensure that we don't share XI_IMG's data buffer
ret.detach();
return ret;
}

Incremental index doesn't change at end of for loop

I want to interface Arduino with PLC to pull some information.
My problem is at Function 3: Set alarm flag / reset flag. This function is used to compare history value and present value. I tried to process some integer number (test_number) and process like binary 16 bits data for finding 1 at some bit. I found the for loop in Findbit function, which should repeat 16 times, runs infinitely. It does not change the incremental index (variable name bit_1) which is still stuck at 1.
This is my code :
int test_number_array[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int test_number = 0;
int bit_1 = 0;
int Andbit = 0;
const char* message;
int flagAlarm[2][16] = {};
int flagReset[2][16] = {};
void setup()
{
// put your setup code here, to run once:
Serial.begin( 9600 );
}
void loop()
{
// put your main code here, to run repeatedly:
for (int j = 1; j <= 2; j++)
{
for (int i = 1; i <= 2; i++) // Example with 2 modbus address
{
unsigned int address = 40000 + i;
Serial.print ("Modbus address = ");
Serial.println(address, DEC);
pull_data(i);
Serial.print("Test number is ");
Serial.println(test_number);
Findbit(i);
Serial.println("------------------------------------------------- ");
}
}
while (1)
{
}
}
// ---------------Function 1 : Function finding alarm bit-----------------//
void Findbit(int i)
{
for (bit_1 = 0; bit_1 <= 15; bit_1++)
{
Andbit = test_number & 1;
Serial.print("Test number (BINARY) is ");
Serial.println(test_number, BIN);
Serial.print("Check at bit number ");
Serial.println(bit_1);
Serial.print("And bit is ");
Serial.println(Andbit, BIN);
Serial.print("flagAlarm(Before1) = ");
Serial.println(flagAlarm[i][bit_1]);
Serial.print("flagreset(Before1) = ");
Serial.println(flagReset[i][bit_1]);
if (Andbit == 1) //found "1" pass into loop
{
flagAlarm[i][bit_1] = 1;
}
else
{
}
Serial.print("flagAlarm(Before2) = ");
Serial.println(flagAlarm[i][bit_1]);
Serial.print("flagreset(Before2) = ");
Serial.println(flagReset[i][bit_1]);
Set_reset_flag(i,bit_1);
test_number = test_number >> 1;
Serial.print("flagAlarm(After) = ");
Serial.println(flagAlarm[i][bit_1]);
Serial.print("flagreset(After) = ");
Serial.println(flagReset[i][bit_1]);
Serial.println(" ");
}
}
// -----------------------Function 2 : Pull data------------------------- //
int pull_data(int i)
{
i = i - 1;
test_number = test_number_array[i];
return test_number;
}
// -------------Function 3 : Set alarm flag / reset flag ---------------- //
void Set_reset_flag(int i, int bit_1)
{
Serial.print("i = ");
Serial.println(i);
Serial.print("bit_1 = ");
Serial.println(bit_1);
if (flagAlarm[i][bit_1] == 1 && flagReset[i][bit_1] == 0)
{
Serial.print("Alarm at bit ");
Serial.println(bit_1);
flagAlarm[i][bit_1] = 0;
flagReset[i][bit_1] = 1;
}
else if (flagAlarm[i][bit_1] == 0 && flagReset[i][bit_1] == 1)
{
Serial.print("Reset Alarm at bit ");
Serial.println(bit_1);
flagReset[i][bit_1] = 0;
}
else if (flagAlarm[i][bit_1] == 1 && flagReset[i][bit_1] == 1)
{
Serial.print("Alarm still active at bit ");
Serial.println(bit_1);
flagAlarm[i][bit_1] = 0;
flagReset[i][bit_1] = 1;
}
else
{
}
}
Could it be that your bit_1 variable is modified from some other code not mentioned here, or get optimized at all? Also, is it necessary to make a loop counter a global variable? Can you declare it inside the Findbit function?

Resources