body:
I'm trying to use a 4x3 matrix keypad as a user interface to control parameters of a Neopixel strip control program.

I'm using Adafruit's Neopixel library and This Keypad library.
Both work fine on their own and none of my code, outside this instantiation of the keypad library, interferes with the programs functionality.

The code is around 400 lines so I'm only including a snippet:
I've now included all functions which are called in setup, as well as the one we are supposed to land on in loop() as a result of the switch statement.

#include "math.h"
#include "Adafruit_NeoPixel.h"
#include "Keypad.h"
#ifdef __AVR__
#include "avr/power.h"
#endif

#define PIN 9 //Neopixel data pin
#define NUMPIXELS 300 //currently 300

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0','#'}
};

byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

//f=1/p period should be between 1s and 10s so...
//frequencies should be somewhere between 0.1Hz and 1Hz
int period[NUMPIXELS];

float twoPI = 2 * PI;

int rainbowNow = 0;
unsigned int xOff = 0;

int switcha = 0;

byte hue = 192;
float sat = 0;
float lum = 1;

float lumSave;

int *rgb;

char select = '5';
char hueInc = '6';
char hueDec = '4';
char lumInc = '2';
char lumDec = '8';
char satInc = '3';
char satDec = '1';
char onoff = '0';

boolean holdFlag = 0;

void setup(void) {
  //create array of period lengths for each pixels sine wave
  randomSeed(10);
  for (int i = 0; i < NUMPIXELS; i++) {
    period[i] = random(2, 20);
  }
  Serial.begin(9600);

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  //showOff();
  fadeIn();
  sparkle();
}

void loop(void) {

  char key = keypad.getKey();
  change(key);

  switch(switcha) {
    case 0: solidColour(); break;
    case 1: rainbow(); break;
    case 2: twinkle(); break;
    case 3: satSine(); break;
    case 4: hueSine(); break;
    case 5: rainbowCycle(); break;
  }
}

void solidColour() {
  rgb = Wheel(hue, sat, lum);
  for(int i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, strip.Color(rgb[0], rgb[1], rgb[2]));
  }
  strip.show();
  delay(20);
}

void fadeIn() {
  Serial.println("FADE IN");
  for(float j=0; j<1; j+=0.16) {
    rgb = Wheel(hue, sat, j);
    for(int i=0; i<NUMPIXELS; i++) {
      strip.setPixelColor(i, strip.Color(rgb[0], rgb[1], rgb[2]));
    }
    strip.show();
    delay(100);
  }
}

void sparkle() {
  Serial.println("SPARKLE");
  rgb = Wheel(hue, sat, lum);
  strip.setPixelColor(0, strip.Color(rgb[0], rgb[1], rgb[2]));
  for(int i=1; i<NUMPIXELS; i++) {
    int blnk = random(0, NUMPIXELS-1);
    strip.setPixelColor(blnk, strip.Color(0, 0, 0));
    strip.show();
    delay(40);
    strip.setPixelColor(blnk, strip.Color(rgb[0], rgb[1], rgb[2]));
    strip.show();
  }
}

//will create pseudo-Saturation by reducing white with increasing sat
//brightness will be achieved by scaling all by a constant
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
int* Wheel(byte WheelPos, float pseudoSat, float pseudoLum) {
  static int rgb[3];
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    rgb[0] = (255 - WheelPos * 3) * pseudoLum;
    rgb[1] = 0;
    rgb[2] = (WheelPos * 3) * pseudoLum;
  } else if (WheelPos < 170) {
    WheelPos -= 85;
    rgb[0] = 0;
    rgb[1] = (WheelPos * 3) * pseudoLum;
    rgb[2] = (255 - WheelPos * 3) * pseudoLum;
  } else if(WheelPos >= 170) {
    WheelPos -= 170;
    rgb[0] = (WheelPos * 3) * pseudoLum;
    rgb[1] = (255 - WheelPos * 3) * pseudoLum;
    rgb[2] = 0;
  }

  return rgb;
}

void change(char key) {
  if(key == select) { //select
    if(switcha < 5) {
      switcha++;
    } else {
      switcha = 0;
    }
  } else if(key == hueDec) { //fwd
    if(hue > 0) {
      hue--;
    } else {
      hue = 255;
    }
  } else if(key == hueInc) { //back
    if(hue < 255) {
      hue++;
    } else {
      hue = 0;
    }
  } else if(key == lumInc) { //up
    if(lum < 1) {
      lum+=0.05;
    } else {
      lum = 0.05;
    }
  } else if(key == lumDec) { //down
    if(lum > 0.05) {
      lum-=0.05;
    } else {
      lum = 1;
    }
  } else if(key == onoff) {
    if(lum != 0) {
      lumSave = lum;
      lum = 0;
    } else if(lum == 0) {
      lum = lumSave;
    }
   } else if(key == satInc) { //play
    if(sat < 1) {
      sat+=0.025;
    } else {
      sat = 0.025;
    }
  } else if(key == satDec) {
    if(sat > 0.025) {
      sat-=0.025;
    } else {
      sat = 1;
    }
  }
}

So long as I comment out the line `Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );`, the code runs as expected and the Neopixel strip lights up in the way I have programmed.
However, with that line, nothing lights up at all. The code still reaches all the functions, (I have checked with Serial.print).

(EDIT: It also works with the `Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );` included so long as I comment out the switch statement in loop(). How can a statement in loop affect the setup function?!)

I think it must have something to do with either interrupts, or a polling frequency within the Keypad library with interferes with the 'delicate' timing of the Neopixels.

The massively frustrating this is that I have solved this problem before on this exact hardware but the code was lost and I'm having to reconstruct it for a repair job.

If you could help me specifically identify the problem, or suggest how I might implement my keypad interface with the neopixels I would be very grateful.

Arduino ide2:1.0.5 (EDIT: also used 1.8.9 with no different result)

Arduino Nano Board

Sodial 4x3 Matrix Keypad

ws2812b rgb 300 neopixel 5M strip

title: How can if resolve this conflict between matrix keypad library and neopixel library?

Answers: 0


body:
I'm worked with Low Fuse bits in AVRDUDEPROG 3.3. Fuses was non-inverted. And by mistake I enabled SUT1 and SUT0, when SUT0 was enabled. And when I programmed avr,
it shows me error that avrdude.exe: initialization failed, rc=-1. I have read I can restore it with Avr Fuse Doctor, but I can't buy or make with pcb. Can I recover avr with:
```cpp
#include <avr/io.h>

int main(void) {
    DDRB = 0xFF;
    while(1) {
        PORTB ^= 0xFF;
    }
}
```

and load it to another avr and connect it to broken avr?

Can I make doctor on breadboard or Can I do it with usbasp/isp?

Can I load a new bootloader in it?

title: Restoring all fuse bits on AtMega328P-PU

Answers: 1

body:
The following is from:
https://www.allaboutcircuits.com/projects/atmega328p-fuse-bits-and-an-external-crystal-oscillator/

"The two bits to be avoided unless you are absolutely certain you know what you are doing are bit7, the RSTDISBL (external reset disable) bit and bit 5, the SPIEN (Serial Peripheral Interface Enable) bit. Disabling either one is often the cause for "bricked" Atmel µCs; "leave them alone" is good advice."

According to the article SUT0 and SUT1 are clock start up time related bits. It doesn't seem as though it is necessary to use anything special to change them.

You will find the information at the website very usefu. I know I have.

The High Voltage (other names too) programmer you have mentioned should not be necessary if you only changed the SUT0 and SUT1 fuses.

HOWEVER, if you did change the SPIEN or RSTDISBL fuses, you should either get one of the special programmers or just purchase another 328P microprocessor.

If you search the internet for "AVR High Voltage programmer" you will find many ideas for how to build one.

This link :https://electronics.stackexchange.com/questions/33/arduino-as-avr-programmer/71
provides good information.


body:
I'm looking to create a blind controller and have set up the hardware and software as I think it should be.

I had the software working fine using an IC motor driver, but as I am no great at soldering, I decided to use a motor shield. The software (below) now only turns the motor one way. I did amend the code to try to get the motor to turn the other way, and it did but only when the 'BlindOn' was during it's on.

Therefore, the hardware is set up correctly (pins D1 and D3) but there is some modification needed to this program; any help or advice greatly appreciated.
```python
/**********************************************************************************
  * Code for controlling multiple devices connected to one NodeMCU using Amazon Echo
  *
  * Written by Sid for Sid's E Classroom
  *
  * https://www.youtube.com/c/SidsEClassroom
  *********************************************************************************/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"

#define WIFI_SSID "VM0718169" //change your Wifi name
#define WIFI_PASS "8kcgvwSFymjx" //Change your Wifi Password
#define SERIAL_BAUDRATE                 115200

fauxmoESP fauxmo;
//declare switching pins
//Change pins according to your NodeMCU pinouts
#define BlindOp D1
#define BlindCl D3
#define Test D2

volatile bool BlindOpOn = false;  // needs to be volatile since used in ISR
volatile bool BlindClOn = false;
volatile bool TestOn = false;

const unsigned long BlindOpOnTime = 5;    // seconds
const unsigned long BlindClOnTime = 5;     // seconds
const unsigned long TestOnTime = 5;

volatile unsigned long BlindOpStartTime;
volatile unsigned long BlindClStartTime;
volatile unsigned long TestStartTime;

// -----------------------------------------------------------------------------
// Wifi Setup
// -----------------------------------------------------------------------------

void wifiSetup() {

    // Set WIFI module to STA mode
    WiFi.mode(WIFI_STA);

    // Connect
    Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
    WiFi.begin(WIFI_SSID, WIFI_PASS);

    // Wait
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(100);
    }
    Serial.println();

    // Connected!
    Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
// -----------------------------------------------------------------------------
// Device Callback
// -----------------------------------------------------------------------------
void callback(uint8_t device_id, const char * device_name, bool state) {
  Serial.print("Device "); Serial.print(device_name);
  Serial.print(" state: ");
  if (state) {
    Serial.println("ON");
  } else {
    Serial.println("OFF");
  }
  //Switching action on detection of device name
  if ( (strcmp(device_name, "BlindOp") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(BlindOp, HIGH);
      BlindOpOn = true;
      BlindOpStartTime = millis();
    } else {
      digitalWrite(BlindOp, LOW);
      BlindOpOn = false;
    }
  }
  if ( (strcmp(device_name, "BlindCl") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(BlindCl, HIGH);
      BlindClOn = true;
      BlindClStartTime = millis();
    } else {
      digitalWrite(BlindCl, LOW);
      BlindClOn = false;
    }
  }
  if ( (strcmp(device_name, "Test") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(Test, HIGH);
      TestOn = true;
      TestStartTime = millis();
    } else {
      digitalWrite(Test, LOW);
      TestOn = false;
    }
  }
}

void setup() {
    //Initialize pins to Low on device start
    pinMode(BlindOp, OUTPUT);
    digitalWrite(BlindOp, LOW);
    pinMode(BlindCl, OUTPUT);
    digitalWrite(BlindCl, LOW);
    pinMode(Test, OUTPUT);
    digitalWrite(Test, LOW);

    // Init serial port and clean garbage
    Serial.begin(SERIAL_BAUDRATE);
    Serial.println("FauxMo demo sketch");
    Serial.println("After connection, ask Alexa/Echo to 'turn <devicename> on' or 'off'");

    // Wifi
    wifiSetup();

    // Device Names for Simulated Wemo switches
    fauxmo.addDevice("BlindOp");
    fauxmo.addDevice("BlindCl");
    fauxmo.addDevice("Test");
    fauxmo.onMessage(callback);
}

void loop() {
  unsigned long duration;

  fauxmo.handle();
  if ( BlindOpOn == true ) {
    duration = (millis() - BlindOpStartTime) / 1100;  // convert to seconds
    if ( duration >= BlindOpOnTime ) {
      digitalWrite(BlindOp, LOW);
      BlindOpOn = false;
    }
  }

  if ( BlindClOn == true ) {
    duration = (millis() - BlindClStartTime) / 1100;  // convert to seconds
    if ( duration >= BlindClOnTime ) {
      digitalWrite(BlindCl, LOW);
      BlindClOn = false;
    }
  }
  if ( TestOn == true ) {
    duration = (millis() - TestStartTime) / 1100;  // convert to seconds
    if ( duration >= TestOnTime ) {
      digitalWrite(Test, LOW);
      TestOn = false;
    }
  }

}

title: Using Arduino Motor Shield

Answers: 0


body:
Why my servo motor is always on?

[![Schematic][1]][1]

    #include <Servo.h>

    Servo myservo;

    int pos = 0;

    void setup() {

    pinMode(2, INPUT);
    myservo.attach(9);

    }

    void loop() {

      if (digitalRead(2) == LOW) {

        for (pos = 0; pos <= 180; pos += 1) {
          myservo.write(pos);
          delay(15);
        }

        for (pos = 180; pos >= 0; pos -= 1) {
          myservo.write(pos);
          delay(15);
        }
      }
      else {
        myservo.write(0);
        delay(15);
      }
    }

  [1]: https://i.stack.imgur.com/hnYrF.png "Schematic"

title: Why my servo motor is always on?

Answers: 1

body: It looks like you need to enable the pull-up resistor on D2.  As written D2 is left floating and most likely is a low voltage relative to ground.  Enabling the pull up resistor will force the pin high until you flip the switch.


body:
**Question**

Trying to call the Display `draw` API using the sample code for the [azure logo bitmap sample code][1]. I'm unable to reach a point where I can the API because breaks when constructing the bitmap char arrays in my `BMPManager` class. I'm also testing custom bitmaps using this [pixelcraft][2] tool.

>In file included from /Users/gregorydegruy/Code/GitHub/_mr.u/mxchip/Main.ino:5:0:
sketch/srcsrc/BMPManager.cpp:4:2371: error: too many initializers for 'unsigned char [0]' unsigned char AzureBMP[] = {0,0,0,0,0,0,0,0,0,0,0,0, . . .

**Main**

    void loop()
    {
        Screen.print("start prgm...");
        delay(1000);
        Screen.draw(0, 0, 128, 8, bitMapManager.get_azure_bitmap());
        delay(5000);
        Screen.draw(0, 0, 128, 8, bitMapManager.get_nintendo_bitmap());
    }

**BMPManager**

    class BMPManager
    {
        private:
            unsigned char AzureBMP[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,192,192,224,240,56,12,192,240,224,192,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,224,224,224,224,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,192,224,248,252,254,255,255,63,15,3,64,248,254,255,255,255,255,255,252,248,224,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,240,252,127,31,3,3,15,127,254,240,192,0,0,0,0,8,28,28,28,28,156,220,252,124,60,28,0,0,0,252,252,248,0,0,0,0,0,0,252,252,248,0,0,0,0,252,252,252,112,56,28,28,28,0,128,224,240,248,28,28,12,12,28,248,248,240,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,224,248,252,255,255,255,255,63,15,3,0,0,0,0,0,0,3,7,15,31,63,127,255,255,255,255,255,252,240,224,128,0,0,0,0,0,0,64,112,124,127,31,7,7,7,7,7,7,7,7,31,127,126,120,64,0,96,112,120,126,127,103,99,97,96,96,96,0,0,0,63,127,127,240,224,224,96,96,56,127,127,127,0,0,0,0,127,127,127,0,0,0,0,0,0,15,63,127,123,243,227,227,227,227,99,99,35,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,8,8,8,8,8,12,12,12,12,12,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
            unsigned char NintendoSwitchBMP[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80, 0xc0,0xc0,0xe0,0xe0,0xf0,0xf0,0xf8,0xf8, 0xf8,0xf8,0xf8,0xf8,0xf8,0xf8,0xf8,0xf8, 0xf8,0xf8,0xf8,0xf8,0xf8,0xf8,0xf8,0xf8, 0x00,0x00,0x00,0x00,0xf8,0xf8,0xf8,0xf8, 0xf8,0xf8,0xf8,0xf8,0xf8,0xf8,0xf8,0xf8, 0xf8,0xf8,0xf0,0xf0,0xe0,0xe0,0xc0,0xc0, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xc0,0xf0,0xfc,0xff,0xff, 0xff,0xff,0x7f,0x1f,0x0f,0x07,0x87,0x83, 0x83,0x81,0x81,0x81,0x81,0x81,0x01,0x01, 0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xfc,0xf0,0x80,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff, 0xff,0x0f,0x00,0x00,0xfe,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x78, 0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0x00,0x00,0x00,0xc0,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff, 0xff,0x00,0x00,0x00,0x00,0x01,0x03,0x03, 0x07,0x07,0x07,0x07,0x03,0x03,0x01,0x00, 0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0x7f,0x3f,0x3f, 0x3f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff, 0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xf1,0xc0,0x80,0x80,0x00, 0x00,0x80,0xc0,0xc0,0xf1,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff, 0xff,0xf0,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01,0x0f,0x1f,0xff,0xff, 0xff,0xff,0xfe,0xf8,0xe0,0xe0,0xe0,0xc0, 0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0, 0x80,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0x00,0x00,0x00,0x01,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0x1f,0x0f,0x01,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x03,0x03,0x03,0x07,0x0f,0x0f,0x0f,0x1f, 0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f, 0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f, 0x00,0x00,0x00,0x00,0x1f,0x1f,0x1f,0x1f, 0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f, 0x1f,0x0f,0x0f,0x0f,0x07,0x03,0x03,0x03, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

        public:
            BMPManager() {}
            unsigned char* get_azure_bitmap() {
                return AzureBMP;
            }
            unsigned char* get_nintendo_bitmap() {
                return NintendoSwitchBMP;
            }
    };

  [1]: https://microsoft.github.io/azure-iot-developer-kit/docs/apis/display/#draw
  [2]: https://pixelcraft.cc/

title: MXCHIP IoT DevKit too many initializers for 'unsigned char [0]

Answers: 1

body:
In C++ you are not allowed to declare non-static member arrays with unspecified size (empty `[]`). Supplying an in-class *brace-or-equal* (BoE) initializer for such non-static member array does not override this rule

    struct S
    {
      int a[] = { 1, 2, 3 }; // <-- ERROR: `[]` array declaration is not allowed here
    };

If you really need these arrays as *non-static* members of the class, you will have to explicitly specify their size (which is `1024` in your case). Once you do that, you will also be able to BoE initializers for them right inside the class definition

    class BMPManager
    {
    private:
      unsigned char AzureBMP[1024] = { /* whatever */ };
      unsigned char NintendoSwitchBMP[1024] = { /* whatever */ };
      ...
    };

The `=` is optional.

---

However, the real question in this case is whether you really need these arrays as non-static (and non-const) members of the class. Do you really need independent instances of these arrays in each instance of `BMPManager` class? Are these arrays supposed to be modifiable?

If you really need independent instances of such arrays in each object, then you will have to do what's shown above.

But if you don't need independent arrays in each object (which would probably also imply that the arrays are not supposed to change) then the proper way to go about it would be to declare them as `static` members

    class BMPManager
    {
    private:
      static unsigned char AzureBMP[];          // static const ?
      static unsigned char NintendoSwitchBMP[]; // static const ?
      ...
    };

and then define them in one of the `.cpp` files as

    unsigned char BMPManager::AzureBMP[] = { /* whatever */ };
    unsigned char BMPManager::NintendoSwitchBMP[] = { /* whatever */ };

Note that in this case you will not be able to use BoE initializers directly in class definition. The initializers are supplied at the point of static member definition in `.cpp` file.

This can be done much more compactly in C++17, but I'd guess that you are using Arduino IDE, which still sticks to C++11.

---

Also, make them `const`, if appropriate.

Looking at the documentation at IoT DevKit web site you linked I see that it contains a number of ridiculous typos making it almost unusable. It is quite possible that you will run into problems with their library if you properly use const-correctness in your code.


body:
I've got a few questions about these LEDs (WS2812C). According to what I understand in the data sheet, they are adressable RGB LEDs with 4 pins: VCC, GND, DIN,DOUT. I'm working on a project that is an LED cube, and these LEDs are a very good candidate for my project, but I'm trying to figure out how I'm supposed to wire them (like how the circuit diagram should be).

So I have three questions:

1) is 1111 the maximum number of LEDs I can have in series? because the datasheet says that the maximum transfer rate is 800Kbps, and if I have to refresh the LEDs at a rate of 30 fps, and if my math is correct, 800Kbps/30fps/24bits = 1111 pixels. So please let me know if my math is correct on that, because that would imply that I would need two rows of LEDs if I were to make a 20x10x10 LED cube.

2) In the cicuit diagram of the datasheet there was only a capacitor and no resistor. According to what I know, you should always have a resistor in series with your LED, so that you don't blow it out. It is still the case with these?

3) Can I have one big capacitor instead of many small ones in parallel? I'd think that it would be equivalent but I'm not an expert so I don't know.

If any of you can help me figure out the answer to these questions I would highly appreciate it. Thanks.

title: On the LEDs WS2812C

Answers: 0


body:
I got some LCD display from my friend. Base on the model number (TopWay LM6023) printed on product, I found the data sheet from google. According to the [datasheet][1], it is a 128*64 LCD display, but I am confusing the pin out does not same/similar to the famous one. I read the datasheet, there are C86 pin. I have no idea what’s that, and how to make it work. I created a [github repo][2], I am wonder if someone could help to write an example code and wiring to Arduino UNO.

Moreover, I also would like to know what is 6800 & 8080 interface, is it Arduino UNO can simulate ? If yes, any reference?
[![Terminal Functions][3]][3]

  [1]: https://github.com/allenchak/LM6023/blob/master/Datasheet/SP-MCU-20110107-01_LM6023A-Manual-Rev0.3.pdf
  [2]: https://github.com/allenchak/LM6023
  [3]: https://i.stack.imgur.com/kUQif.png

title: LCD Display 128x64 weird pin, C86 (input)

Answers: 1

body:
Unfortunately driving a graphic LCD is not as easy as driving one of the most popular type HD44780 LCD's.
Back to the subject:
If the state on this pin is HIGH then you shall use  Motorola's 6800 interface, when state on C86 is LOW, use Intel's 8080 interface.
Depending on your selection you would have to keep the communication standard. For 8080 interface refer to point 3.3.1 of you datasheet, for 6800 see point 3.3.2.
Note: If you are struggling, try to search for the libraries using controller's name: LM6023A or one of the S1D15300 or S1D16700
Also, consider number of pins available on your Arduino. It may be that you can drive this LCD with Arduino Uno using serial interface only. (See this [link][1])

  [1]: https://electronics.stackexchange.com/questions/84841/which-interface-is-better-for-my-oled-display


body:
I know it is not a good practice to use the `new` command to dynamically create data, however, if I would never use the `delete`, would it result in memory gaps/other problems?

**Background**

The reason I need to create instances dynamically, is that I want to test my application first on a PC. And I want for several classes to have more properties for the PC version than for the Arduino version (for example, to store names, or information for the PC GUI to display). The PC version classes will be inherited from the Arduino (real) classes, but with added properties.

In some classes I need to build up a list, and I cannot use a type with hardcoded values to fill an array.

Example:

I have a LightSetup class that has a list of Pars (which are LED lights):

    class LightSetup
    {
    protected:
     Par* _pars[NR_OF_PARS];

Par is the Arduino/real version, to fill in these I need:

    LightSetup::LightSetup()
    {
     for (int n = 0; n < NR_OF_PARS; n++)
     {
      _pars[n] = new Par(1 + 8 * n);
     }
    }

In the PC version, instead of new Par, I use new TestPar.
And in the header I cannot define it without a pointer like this:

     Par _pars[NR_OF_PARS];

Otherwise it cannot contain TestPar instances.

title: Is new without a delete acceptable?

Answers: 2

body:
Sure. Using `new` or `malloc` to set things up initially is really no different to static allocations. There is nothing inherently bad with using `malloc` or `new`, only in using it repeatedly with `free` or `delete`.

Allocating a block of heap memory and keeping it allocated for the rest of eternity does not cause heap fragmentation. It's only repeated allocation and deallocation of variable sizes (such as `String` causes) that start to create problems. Allocating at the start of your program and leaving it allocated, as long as you don't allocate *too much*, of course, is not a problem.

body:
In the destructor you would need

    LightSetup::~LightSetup()
    {
        for (int n = 0; n < NR_OF_PARS; n++)
        {
            delete _pars[n];
        }
    }

and every time you replace a _pars you need to delete the old one. This can be done automatically by using unique_ptr.

However for your purpose it sounds like a `#if` is better suited to differentiate at compile time which type you actually store inside that array.

    #if ON_ARDUINO
        Par _pars[NR_OF_PARS];
    #else
        TestPar _pars[NR_OF_PARS];
    #endif


body:
Facing a weird issue while reading data from esp8266.

I am trying to read dynamic data from a demo text file on test server on same network in deep sleep mode.

Case1: Joining new network

I am using WifiManager to join network and then followed by http.get()
However whenever i try to join the wifi with correct credentials it does not connect sometimes. Post some time ,if i try to wake the device it automatically connects and displays the data.

Case2: Rejoining existing network

Similar issue happens when i stop the test server and update the text file contents and up the server and try to read it on esp8266. It ask me the credentials again which it should have remembered 1st time. Post some time, if i try to wake up device sometimes it automatically connnects and prints updated value.

I am unable to figure out the problem here and its solution.
Any suggestion please.

    #include <WiFiManager.h>
    #include <ESP8266HTTPClient.h>
    #include <ESP8266WiFi.h>
    #include <ESP8266WiFiMulti.h>
    #include <Arduino.h>
    #include <ESP8266httpUpdate.h>

    ESP8266WiFiMulti WiFiMulti;
    HTTPClient http;
    WiFiManager manager;

      int publishPin = 0;
      int wifiPin = 5;
      unsigned int baud = 115200;
      const char* url3 = "192.168.43.215";
      const int httpsPort = 80;

      const String macValue = WiFi.macAddress();

     void publishData()
     {
        String link = "/one.txt";
        Serial.print("connecting to ");
        Serial.println(url3);

        http.begin("http://192.168.43.215/one.txt");

        int httpCode = http.GET();
        if (httpCode > 0) {
        if (httpCode == HTTP_CODE_OK) {
          Serial.println("This device is authorized!");
          Serial.println(http.getString());
          Serial.println(httpCode);

        }else {
        Serial.println("Failed to connect to API.");
        Serial.println(httpCode);
        return;
      }

      http.end();
      delay(1000);
    }

    void restPublish(){
      Serial.println("Rest publish");

      publishData();

    }

    void setup()
    {
      Serial.begin(115200);
      Serial.println("setup2");

      manager.autoConnect("espdevice");
      restPublish();

     Serial.println("entering deepsleep now. DND");
     ESP.deepSleep(0);

    }

    void loop()
    {
    }

title: ESP8266 http.get() issue due to wifi authentication failure

Answers: 0


body:
I have been working on a small project using android app over Bluetooth to control a servo motor (close or open lock). To authenticate the communication, I implement HMAC (with SHA256) on both the android app and the arduino controller.

When user press the button to unlock the door, it will generate a random code,  which will be seen as a message. Then HMAC is called to create a hash string with this message and the shared key. The final string sent to arduino would be  "randomcode+commmand+hashed-string"

```
Something like this:

 5uq0xboq82oih111e30774e4bf8302a04e71eb84c70ce2c130717304f52c67ce709203caea0ce5
(command and the random code are hidden in the string)
```

As the arduino received the message, it will break the string into three parts. The command, the random code and the hashed string. It will run HMAC with the shared key and the random code. Then compare that with the received hashed string. If matched, the perform the action (command).

This is my first project on implementing HMAC. I know this may be sort of subjective question but any comment on this implementation may helpful. Thanks.

title: How to secure the communication between android app and door lock controller?

Answers: 0


body:
I am working on a morse interpreter. I can now use light-sensor and a regular key as input device and want to add a mic to the system. I recieve the input convert it to text and show it on nokia 5110 lcd. And also whilst input, an led and a buzzer turns on and off simultaneously. When I use mic the led works just as it should but I do not get any correct input to the lcd. I attached Out and Gnd pins of mic just as I would attach the key and vcc to 5v.

The following is the code which I use

    #define RST 12
    #define CE  11
    #define DC  10
    #define DIN  9
    #define CLK  8

    #include "font.h";

    unsigned long signal_len,t1,t2; //time for which button is pressed
    int inputPin=2; //input pin for push button
    int ledPin=4; //output pin for LED
    String code=""; //string in which one alphabet is stored

    void LcdWriteString(char *characters){
      while(*characters) LcdWriteCharacter(*characters++);
    }

    void LcdWriteCharacter(char character){
    for(int i=0;i<5;i++) LcdWriteData(ASCII[character-0x20][i]);
    LcdWriteData(0x00);
    }
    void LcdWriteData(byte dat)
    {
      digitalWrite(DC, HIGH); //DC pin is low for commands
      digitalWrite(CE, LOW);
      shiftOut(DIN, CLK, MSBFIRST, dat); //transmit serial data
      digitalWrite(CE, HIGH);
    }
    void LcdWriteCmd(byte cmd)
    {
      digitalWrite(DC, LOW); //DC pin is low for commands
      digitalWrite(CE, LOW);
      shiftOut(DIN, CLK, MSBFIRST, cmd); //transmit serial data
      digitalWrite(CE, HIGH);

    }
    void setup()

    {
      Serial.begin(9600);
      pinMode(inputPin, INPUT_PULLUP); //internal pullup resistor is used to simplify the circuit
      pinMode(ledPin,OUTPUT);

        pinMode(RST, OUTPUT);
      pinMode(CE, OUTPUT);
      pinMode(DC, OUTPUT);
      pinMode(DIN, OUTPUT);
      pinMode(CLK, OUTPUT);
      digitalWrite(RST, LOW);
      digitalWrite(RST, HIGH);

      LcdWriteCmd(0x21);  // LCD extended commands
      LcdWriteCmd(0xA9);  // set LCD Vop (contrast)
      LcdWriteCmd(0x04);  // set temp coefficent
      LcdWriteCmd(0x14);  // LCD bias mode 1:40
      LcdWriteCmd(0x20);  // LCD basic commands
      LcdWriteCmd(0x0C);  // LCD normal video

      for(int i=0;i<504;i++)LcdWriteData(0x00);
    }

    void loop()
    {
    NextDotDash:
      while (digitalRead(inputPin) == HIGH) {}
      t1 = millis();                            //time at button press
      digitalWrite(ledPin, HIGH);               //LED on while button pressed
      while (digitalRead(inputPin) == LOW) {}
      t2 = millis();                            //time at button release
      digitalWrite(ledPin, LOW);                //LED off on button release
      signal_len = t2 - t1;                     //time for which button is pressed
      if (signal_len > 50)                      //to account for switch debouncing
      {
        code += readio();                       //function to read dot or dash
      }
      while ((millis() - t2) < 500)           //if time between button press greater than 0.5sec, skip loop and go to next alphabet
      {
        if (digitalRead(inputPin) == LOW)
        {
          goto NextDotDash;
        }
      }
      convertor();                          //function to decipher code into alphabet
    }
    char readio()
    {
      if (signal_len < 150 && signal_len > 20)
      {
        return '.';                        //if button press less than 0.6sec, it is a dot
      }
      else if (signal_len > 150)
      {
        return '-';                        //if button press more than 0.6sec, it is a dash
      }
    }
    void convertor()
    {
      static String letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
                                 ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "E"
                                };
      int i = 0;
      if (code == ".-.-.-")
      {
        Serial.print(".");        //for break
        LcdWriteCharacter(".");
      }
      else
      {
        while (letters[i] != "E")  //loop for comparing input code with letters array
        {
          if (letters[i] == code)
          {
            Serial.print(char('A' + i));
            LcdWriteCharacter(char('A' + i));
            break;
          }
          i++;
        }
        if (letters[i] == "E")
        {
          Serial.println("");  //if input code doesn't match any letter, error
          LcdWriteCharacter("");
        }
      }
      code = "";                            //reset code to blank string
    }

title: Morse Interpreter-Microphone Problem

Answers: 1

body:
> I attached Out and Gnd pins of mic just as I would attach the key and
> vcc to 5v.

This can't work. Unlike the key, the microphone is an analog device that
will output a continuously varying analog signal. If you expose it to a
pure tone it will output a sine wave. If you sing a note in front of it,
it will pick not only the sine but also a lot of harmonics. In any case,
it will also pick the ambient noise.

What you need to do is somehow determine the amplitude of the sound
signal, then compare that to some threshold in order to decide whether
the amplitude is high enough to be considered “on”. You could use a
diode-based peak detector and a comparator. Doing all this in code is
kind of tricky. You may want to take a look at this [sound-level meter
sketch][sound-meter], but note that a software peak detection could be
simpler than the true-RMS intensity detection implemented in that
sketch.

[sound-meter]: https://gist.github.com/edgar-bonet/e693799f58a545ca89a2


body:
I currently have the code running through Serial1, which works exactly as I want it to:

    int RFIDResetPin = 13;

    void setup()
    {
      Serial.begin(9600);
      Serial1.begin(9600);
      Serial2.begin(9600);
      pinMode(RFIDResetPin, OUTPUT);
      digitalWrite(RFIDResetPin, HIGH);
      Serial.println("Arduino is ready");
    }

    void loop(){

      char tagString1[13];
      int index = 0;
      boolean reading1 = false;
      ///////////////////////////////////////
      //this reads from the reader installed
      //on serial1, which is pin 19
      //////////////////////////////////////
      while (Serial1.available() > 0) {

        int readByte1 = Serial1.read(); //read next available byte

        if (readByte1 == 2) reading1 = true;
        if (readByte1 == 3) reading1 = false;

        if (reading1 && readByte1 != 2 && readByte1 != 10 && readByte1 != 13) {
          tagString1[index] = readByte1;
          index++;
        }
      }
      Serial.print("Reader 1: "); //read out any unknown tag
      Serial.println(tagString1);

      clearTag(tagString1); //Clear the char of all value
      resetReader(); //reset the RFID reader
    }

    void resetReader() {
      ///////////////////////////////////
      //Reset the RFID reader to read again.
      ///////////////////////////////////
      digitalWrite(RFIDResetPin, LOW);
      digitalWrite(RFIDResetPin, HIGH);
      delay(150);
    }

    void clearTag(char one[]) {
      ///////////////////////////////////
      //clear the char array by filling with null – ASCII 0
      //Will think same tag has been read otherwise
      ///////////////////////////////////
      for (int i = 0; i < strlen(one); i++) {
        one[i] = 0;
      }
    }

However, I want to add serial0, 2, and 3 as well. there are 2 main questions I have
1) is it possible to have copy paste this section of the code:

        while (Serial1.available() > 0) {

        int readByte1 = Serial1.read(); //read next available byte

        if (readByte1 == 2) reading1 = true;
        if (readByte1 == 3) reading1 = false;

        if (reading1 && readByte1 != 2 && readByte1 != 10 && readByte1 != 13) {
          tagString1[index] = readByte1;
          index++;
        }
      }
      Serial.print("Reader 1: "); //read out any unknown tag
      Serial.println(tagString1);

      clearTag(tagString1); //Clear the char of all value
      resetReader(); //reset the RFID reader
    }

while changing some of the variable names to connect the other serial ports? I saw something [here](https://forum.arduino.cc/index.php?topic=472055.0), but don't know how to implement it, or if it even would work for me.

and
2) since I am routing the incoming data through serial0, Is it possible to have this code working on it as well?

title: I want my arduino mega to read from all 4 serial ports at the same time, running sequentially through each one

Answers: 1

body:
The Arduino already reads from the 4 serial ports at the same time. Or
at least as close to “at the same time” as possible with a single CPU:
every time a byte is received at one port, an interrupt is triggered
that reads the byte from the UART and puts it into a buffer. You can
then get it from the buffer using `Serial.read()`.

What you now have to do is move the bytes from these buffers (which
belong to the Arduino hardware serial library) into you own buffers for
further processing. You can do this by checking the ports sequentially
and processing each one in turn. Everything will be fine as long as you
do not have blocking code in there. If you do block, then you run the
risk of having one of the library buffers overflow. The code you posted
is non-blocking, so you are fine.

> is it possible to have copy paste

You should avoid programming by copy-and-paste. It's not that it won't
work (it will), but you would be creating a maintenance nightmare.
Instead, put the code you would want to copy-paste into a function, and
call that function as many times as required. For example:

<!-- language-all: lang-c++ -->

    void handlePort(Stream &port, const char *readerName, char *tagString)
    {
        boolean reading1 = false;
        while (port.available() > 0) {
            int readByte1 = port.read(); //read next available byte

            if (readByte1 == 2) reading1 = true;
            if (readByte1 == 3) reading1 = false;
            if (reading1 && readByte1 != 2 && readByte1 != 10 && readByte1 != 13) {
                tagString[index] = readByte1;
                index++;
            }
        }
        Serial.print(readerName); //read out any unknown tag
        Serial.print(": ");
        Serial.println(tagString);

        clearTag(tagString); //Clear the char of all value
        resetReader(); //reset the RFID reader
        }
    }

And you call it in `loop()`:

    handlePort(Serial1, "Reader 1", tagString1);
    handlePort(Serial2, "Reader 2", tagString2);
    ...


body:
I haven't needed classes before in programming for Arduino, but I now have a good use case. I'm not sure I grasp how this will affect code space, though.

Currently I have a struct that holds the properties of an object, and common code that iterates over an array of 12 of that struct. It's fairly compact.

It would be cleaner to wrap the operations on the struct in a class and act on an array of 12 of that class. Essentially the current struct would become private data of the class.

Will all the code in the class be repeated in each instance, or is the compiler clever enough to avoid such repetition?

Any clarifications welcome. C++ is not my main language.

title: Is code in a class repeated for each instance?

Answers: 2

body:
No, the code is not repeated for each instance. Instance methods are like functions. There is only one copy of the code.

There's a dispatch table that maps method calls to their implementations. (Actually, that's standard C++. I don't know for sure how it's implemented in the Arduino C++ compiler)

body:
No, it is not repeated. Unless you are using templates, each method of
each class is stored only once in the compiled program. Or even not
stored at all if the optimizer notices it is not used.

Just to demystify the classes a bit, you can consider them as a kind of
“syntactic sugar” over an “object oriented” programming pattern. The
following C++ code:

<!-- language: lang-c++ -->

    class MyClass {
    public:
        int aMember;
        long anotherMember;
        int aMethod(short);
    };

    int MyClass::aMethod(short theParameter)
    {
        return aMember + theParameter;
    }

    void some_function()
    {
        MyClass objects[2];
        objects[1].aMethod(42);
    }

is interpreted by the compiler as being roughly equivalent to this C
code:

<!-- language: lang-c -->

    typedef struct {
        int aMember;
        long anotherMember;
    } MyClass;

    int MyClass_aMethod(MyClass *this, short theParameter)
    {
        return this->aMember + theParameter;
    }

    void some_function()
    {
        MyClass objects[2];
        MyClass_aMethod(&objects[1], 42);
    }

Can you spot the differences and the similarities?


body:
I have connected DRV8835 with 2 DC motors with 11 power supply. Arduino nano.

When I for loop like i from 0 to 1024 for analogWrite(i,Motor1) for the DC Motor1 it's accelerating from 0 to 200 and it's stopping. When I becomes 250 it's starting at higher speed. When it becomes 500 it's stopping again. And 550 we have our power again.

Now I tried to run analogWrite(1000, ***) but it's not working. When I change the value from 1000 to 1005 it's not working again. But at 1024 we have the maximum speed and it is working?
<!-- language-all: lang-cpp -->

     #define LEFT_PWM 9
     #define RIGHT_PWM 10
     #define LEFT_DIR 5
     #define RIGHT_DIR 6

    void setup() {
      pinMode(5, OUTPUT);
      pinMode(6, OUTPUT);
    }

    void loop() {
      digitalWrite(LEFT_DIR, HIGH);
      analogWrite(LEFT_PWM, 1000);
    }

title: AnalogWrite problem

Answers: 1

body:
`analogWrite` has the following prototype

    void analogWrite(uint8_t, int);

which means that its first parameter has `[0, 256)` range and follows the rules of modulo arithmetic, with `256` as the modulo. If you supply arguments outside of `[0, 256)` range, they will be interpreted `% 256`. `1000` is the same as `232` (`1000 % 256 == 232`), `500` is the same as `244`, `550` is the same as `38` and so on.

This is not fully consistent with the symptoms you describe (if your description is accurate), which might imply that something else is a factor here. Are sure you are using your DRV8835 in `PHASE/ENABLE` mode (pin 11 pulled high)?


body:
So I tried to save a little money and bought (5) HC-05 bluetooth modules off of our good buddy eBay.

When the modules arrived, as I was testing and setting up for my project (2 nano's connected via an HC-05 each to a Galaxy S4), I noticed all 5 modules shipped with the same MAC address.

I have been looking everywhere I can think of to modify the MAC address of the modules, but have had no luck.  Well, that isn't 100% accurate.  I found bdaddr on linux, but I do not have a linux machine.  Just Windows for me right now.

So, my issue is trying to find a way to set the HC-05 modules with different MAC addresses so I can have (2) connect at the same time to my S4.  I have been able to change the name of the units via the AT commands, just not the MAC address.

Thank you in advance for your help.

title: Change the mac address on an HC-05

Answers: 0


body:
I am trying to connect to my robot car with the HC-02 module. The problem is that the module is visible on my phone only while the arduino uno board is connected to my laptop. At this point I can pair my phone with the module, but I can't controll the car. When I disconnect the car from my laptop and turn it on, HC-02 is not visible anymore. It's blinking fast which means it should be available for connecting. I cant access the AT mode and I don't know what to do anymore. Has anyone had problems with this.
My phone is Android, I also tried with another one and still not working.

I also removed arduino uno from the car, I connected only the Bluetooth module. I can see HC-02 when Arduino is connected to the PC with the USB cable. But when I disconnect it from the PC and power it with the battery it disappears from available devices.

[![enter image description here][1]][1]

  [1]: https://i.stack.imgur.com/op1IY.png

title: HC-02 Bluetooth module not visible

Answers: 0


body:
I am working on sample Arduino project for communication over NRF24L01+ (TMRH20 library), everything is working fine with my example except one issue my Receiver gets message but that message is blank. Now i am not sure how do i debug as per serial monitor data is getting transmitted, as per receiver serial monitor it receives data but its blank.

Transmitter code -
<!-- language-all: lang-cpp -->

    // SimpleTx - the master or the transmitter

    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>

    #define CE_PIN   9
    #define CSN_PIN 10

    const byte slaveAddress[5] = {'R','x','A','A','A'};

    RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

    char dataToSend[12] = "Msg AKSHAY";
    //int dataToSend = 1;
    char txNum = '0';

    unsigned long currentMillis;
    unsigned long prevMillis;
    unsigned long txIntervalMillis = 1000; // send once per second

    void setup() {

        Serial.begin(9600);

        Serial.println("SimpleTx Starting");

        radio.begin();
        radio.setDataRate( RF24_250KBPS );
        radio.setRetries(3,5); // delay, count
        radio.openWritingPipe(slaveAddress);
    }

    //====================

    void loop() {
        currentMillis = millis();
        if (currentMillis - prevMillis >= txIntervalMillis) {
            send();
            prevMillis = millis();
        }
    }

    //====================

    void send() {

        bool rslt;
        rslt = radio.write( &dataToSend, sizeof(dataToSend) );
            // Always use sizeof() as it gives the size as the number of bytes.
            // For example if dataToSend was an int sizeof() would correctly return 2

        Serial.print("Data Sent ");
        Serial.print(dataToSend);
        if (rslt) {
            Serial.println("  Acknowledge received");
            updateMessage();
        }
        else {
            Serial.println("  Tx failed");
        }
    }

    //================

    void updateMessage() {
            // so you can see that new data is being sent
        txNum += 1;
        if (txNum > '9') {
            txNum = '0';
        }
        dataToSend[10] = txNum;
    }

Receiver code -

    // SimpleRx - the slave or the receiver

    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>

    #define CE_PIN   9
    #define CSN_PIN 10

    const byte thisSlaveAddress[5] = {'R','x','A','A','A'};

    RF24 radio(CE_PIN, CSN_PIN);

    char dataReceived[10]; // this must match dataToSend in the TX
    bool newData = false;

    //===========

    void setup() {

        Serial.begin(9600);

        Serial.println("SimpleRx Starting");
        radio.begin();
        radio.setDataRate( RF24_250KBPS );
        radio.openReadingPipe(1, thisSlaveAddress);
        radio.startListening();
    }

    //=============

    void loop() {
        getData();
        showData();
    }

    //==============

    void getData() {
      if ( radio.available() ) {

           radio.read( &dataReceived, sizeof(dataReceived) );
          Serial.println(dataReceived);
           newData = true;
       } else {
           Serial.println("No radio available");
       }
       delay(100);
    }

    void showData() {
        if (newData == true) {
            Serial.print("Data received ");
            Serial.println(dataReceived);
            newData = false;
        }
    }

Below is the output TX1 (Transmitter ) and RX1 is Receiver. You can see that Transmitter even gets acknowledgment also..

[![enter image description here][1]][1]

Both of my devices are arduino nano and below is wiring diagram between arduino and nrf24l01.
[![enter image description here][2]][2]

  [1]: https://i.stack.imgur.com/723hy.png
  [2]: https://i.stack.imgur.com/GisSt.png

I also saw few post where people suggested using 100uf capacitor in NRF24L01 (GND & VCC), this helped me with accuracy in connection but data is still missing.

title: NRF24L01+ (TMRH20 library): Receiver gets blank data

Answers: 2

body:
A couple of clarifications first

1. At the time instant 22:38, the receiver isn't detecting any available radio devices but at the transmitter side, an acknowledge is received. How is that possible? How is your tx getting an ack without the rx getting connected.

2. I have not seen slave adresses being set as -
`const byte slaveAddress[5] = {'R','x','A','A','A'};`
 For the record, **NRF24L01 adresses are 5 bytes long** by default and not 1 byte long as you have defined. While defining the pipe addresses, the first 4 bytes of the adresses should be same among all the pipe addresses. Only the last byte should be unique.
 Pipe addresses can be `0xF0F0F0F0A1, 0XF0F0F0F0B1, 0XF0F0F0F0C1, 0XF0F0F0F0D1, 0XF0F0F0F0E1`

Try setting the pipe addresses as
`uint_64t address[5] = {0xF0F0F0F0A1LL, 0XF0F0F0F0B1LL, 0XF0F0F0F0C1LL, 0XF0F0F0F0D1LL, 0XF0F0F0F0E1LL};`

Do the same declaration at the receiver end too. And btw, you can specify any byte you want in the last byte of the address. Here I have defined the last bytes as `A1`, `B1`, `C1`, `D1`, `E1`.

3. To make sure that communication is successful, set some custom ack byte and compare it with the received ack. If both match, you can be sure that the rx did get the data. Sometimes, the NRF24L01 can pickup garbage values from stray signals present around and it can consider it as data sent by the receiver. So to avoid false triggering, its better to make a fool-proof ACK mechanism.

body:
I have tested your code. There appears to be defective clones of the nRF24L01 around that exhibit an issue I show in post https://arduino.stackexchange.com/questions/85874/nrf24l01-continuously-reading-closed-pipe
You can work around this issue by changing your code as follows (although your process still has to read all those nulls):
```
void getData() {
  uint8_t pipeNum;
  if ( radio.available(&pipeNum) ) {

       radio.read( &dataReceived, sizeof(dataReceived) );
      //Serial.println(dataReceived);
      if (pipeNum < 2) {
       newData = true;
      }
   } else {
       Serial.println("No radio available");
   }
   // delay(100);
}
```


body:
Following on from this question, https://arduino.stackexchange.com/questions/24502/problem-with-programming-an-attiny85-error-avrdude-yikes-invalid-device-sig,
if the response from `avrdude` is `0x000000` then the internal oscillator is probably not being used and an external one (be that crystal or clock signal) is required.

However, as this menu, from [Engbedded Atmel AVR® Fuse Calculator][1], shows, there are a *lot* of options:

[![ATtiny85 oscillator selection][2]][2]

So, instead of having to configure circuits for *each* of the options, in turn, until the ATtiny responds, is there any way that I can read which of the `CKSEL` and `SUT` fuses are set?1

---

### Background

I am using a Nano/Uno as an ISP programmer, and the ATtiny is on a dev board like this (although it would be the same issue if I were to use a breadboard):

[![ATtiny development board][3]][3]

I know for sure that the connections are fine as I have checked them from pin to pin of the two ICs (ATmega328PU to ATtiny85), using a DMM.

 I am receiving `0x000000` as in:

         avrdude: Device signature = 0x000000
         avrdude: Yikes!  Invalid device signature.
         Double check connections and try again, or use -F to override
         this check.

because the ISP programmer can't read the ATtiny, because an external clock source has been set via the fuses. However, I wish to know how to read *which* clock source has been specified, without having to manually try *all* of the circuit permutations.

I don't think that just connecting a crystal (and two ceramic capacitors) will work if the exact setting, i.e. external clock, has been selected. However, if a 16MHz crystal works for all, or any, of the external clock settings then that would be great, but I'm sure it doesn't work like that, as you can have a crystal attached but the it might not be used, see [Does device signature 0x000000 mean the atmega is bad](https://www.avrfreaks.net/forum/does-device-signature-0x000000-mean-atmega-bad):

> Even though you have a crystal connected, that does not mean it is oscillating. For example, IF you set fuses for "External Oscillator", that will turn the crystal oscillator off.

When I move the ATtiny85 to a breadboard and add a crystal (16 MHz) and two 22 pF capacitors, the error has changed for the better (at least now it sees that a ATtiny is connected)

    avrdude: Device signature = 0xff0000
    avrdude: Expected signature for ATtiny85 is 1E 93 0B
             Double check chip, or use -F to override this check.

Interestingly the signature varies:

 - `0xff0000`
 - `0x00ffff`
 - `0xff00ff`

When I add an external power supply to the ATtiny on the breadboard, following advice from [Atmega (any chip) Device Signature issues and stk500_getsync()(resolutions)][4], then unfortunately the device signature in the response returned by `avrdude` reverts back to `0x000000`

         avrdude: Device signature = 0x000000
         avrdude: Yikes!  Invalid device signature.
         Double check connections and try again, or use -F to override
         this check.

[![ATtiny85 on breadboard and Nano as ISP][5]][5]

---

1 This is, admittedly, a bit chicken-and-egg, as if I can't communicate with the ATtiny, due to the incorrect clock setting, then how can I read which fuses have been set..?
 It's a bit like saying "If I don't have petrol in my car, then how can I drive to the petrol station to fill up?" - but I was hoping that there might be some sort of time saving measure

  [1]: http://www.engbedded.com/fusecalc/
  [2]: https://i.stack.imgur.com/xZka6.png "ATtiny85 oscillator selection"
  [3]: https://i.stack.imgur.com/tyPdr.png "ATtiny development board"
  [4]: https://forum.arduino.cc/index.php/topic,51819.0.html?utm_source=rb-community&utm_medium=forum&utm_campaign=device-signature-0x000000-while-burning-bootloader
  [5]: https://i.stack.imgur.com/r7TtT.jpg "ATtiny85 on breadboard and Nano as ISP"

title: How can I tell which fuses are set on an ATtiny?

Answers: 2

body:
You need a Programmer that connects to the ISP pins

http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf

The programmer will hold Reset low, then communicate via the SCK/MOSI/MISO pins to read/update the fuses, read/program memory, and optionally read/program the EEPROM.

Section 20.2 discusses the 3 fuse bytes.

I use an older Atmel AVR-ISP MKii. Atmel stopped making them, there seem to be plenty of replacement choices available, and Atmel Studio 7 when I struggle with Atmega328P, '1284P, and 2560. I haven't used an Attiny in a project yet.

https://www.microchip.com/mplab/avr-support/atmel-studio-7

body:
This seems to be an X -> Y problem, with X being the missing clock source on your ATTiny, which prevents communication.

If you have a second Uno or other AVR device, you can use that to generate a clock signal on any of the digital pins:

    void setup() {
      DDRB = 0b00100000;
    }

    void loop() {
      PINB = 0b00100000;
    }
This creates a square(~ish) signal on Pin 13 with a frequency of 1.6 MHz.
Depending on the IDEs optimization level (platform.txt), a frequency of 4 MHz is also achievable (at `-O3` instead of `-Os`).

Connecting this to the `XTAL1` pin of your ATTiny85 should allow you to communicate with the board.
Make sure to interconnect all grounds.


body:
I have written a small program to send serial commands from Python to the Arduino Micro.  This is my code for both:

Python

    import serial

    ser = serial.Serial('COM5', 9600)

    ser.write(b'0')
    ser.write(b'1')
    # ser.write(b'2')

    print('done')

Arduino

    void setup()
    {
      Serial.begin(9600);
    }

    void loop()
    {

      while (!Serial.available()){}
      Serial.println(Serial.read()-48);

    }

If I keep the `ser.write(b'2')` line in the Python code commented out, the code executes nearly instantly, and I am able to see both transmissions in the serial monitor. If I uncomment it, the Python code takes about 5-7 seconds to execute, and none of the data comes through the serial monitor.

I've tried different baud rates but that hasn't helped. I've also tried sending an integer rather than `b'#'`, and the same thing happens, no data is transmitted if I have all three serial commands active.

What is happening in my code? Is the serial buffer overloaded by the three successive writes and the buffer ends up flushing?

title: Arduino will not read three successive packets in the serial connection

Answers: 1

body:
The problem is that your code works but you substract 48 from what you received.
<br>Which if python sends a ``char`` with values: ``'0'`` ``'1'`` ``'2'`` it will result in the serially outputted values of ``NUL`` ``SOH`` ``STX`` which are invisible.

If python transmits the raw byte value (aka ``0x00`` ``0x01`` ``0x02``) this would result in ``0x00 - 48 = undefined behaviour`` with a underflow (same goes for ``0x01`` and ``0x02``)


body:
Arduino programming has been a huge learning curve for me and I am trying to create a project with an rgb that's color is determined by a potentiometer. To where the further I turn it, the further across the rainbow spectrum it goes. I can't figure out how to do all values in between because I am not educated enough but I came up with this basic code...
```
int redpin = 6;
int greenpin = 3;
int bluepin = 5;
int potPin = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(potPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int wavecolor = analogRead(potPin);
  Serial.print(wavecolor);
  {
    if (wavecolor = 0) {
      setColor(255, 0, 0);  // red
    }
    if (wavecolor > 0 or wavecolor < 333) {
      setColor(0, 255, 0);  // green
    }
    if (wavecolor > 333 or wavecolor < 667) {
      setColor(0, 0, 255);  // blue
    }
    if (wavecolor > 667 or wavecolor < 1022) {
      setColor(255, 255, 0);  // yellow
    }
    if (wavecolor = 1023) {
      setColor(80, 0, 80);  // purple
    }
  }
}

void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
  red = 255 - red;
  green = 255 - green;
  blue = 255 - blue;
#endif
  analogWrite(redpin, red);
  analogWrite(greenpin, green);
  analogWrite(bluepin, blue);
}
```

And for some reason, where the light should change colors once the potentiometer goes past a certain number, the light just flickers and only changes colors to white when it is all the way up or down. Also, it has a weird flickery yellow color.
Since I am new to this, can one of you better educated people be willing to assist me?

title: What is wrong with my code? RGB potentiometer

Answers: 2

body:
* Equality comparison in C and C++ is expressed by `==` operator. `==`, not `=`. This

        if (wavecolor = 0) {

 actually *assigns* zero to your `wavecolor` variable, thus destroying the value you just read.

* The common sense says that you actually need `and` in your conditions, not `or`

        if (wavecolor > 0 and wavecolor < 333) {

 The `or` conditions that you curently use are simply *always true*, which makes them nonsensical.

* This is valid syntax

        wavecolor > 0 or wavecolor < 333
        wavecolor > 0 and wavecolor < 333

 but you might want to try using more "canonical" forms

        wavecolor > 0 || wavecolor < 333
        wavecolor > 0 && wavecolor < 333

 `or` and `and` are "alternative" forms of `||` and `&&` operators, introduced a long time ago for reasons that are all but obsolete today.

* Assuming that you meant `and` instead of `or`, in your conditions you managed to "exclude" such values as `333`, `667` and `1022` - they wouldn't match any of `and` conditions. Changing some strict comparisons to non-strict ones might be in order.

* As a side note, in a single-file program all file-scope variables should be either `const` or `static`. In your case

        const int redpin = 6;
        const int greenpin = 3;
        const int bluepin = 5;
        const int potPin = 0;

body:
Try this code below. This will give you the desired output!

    int redpin = 6;
    int greenpin = 3;
    int bluepin = 5;
    int potPin = 0;

    void setup() {
      // put your setup code here, to run once:
      pinMode(redpin, OUTPUT);
      pinMode(bluepin, OUTPUT);
      pinMode(greenpin, OUTPUT);
      pinMode(potPin, INPUT);
      Serial.begin(9600);
    }

    void loop() {
      // put your main code here, to run repeatedly:
      int wavecolor = analogRead(potPin);
      Serial.print(wavecolor);
      {
        if (wavecolor == 0) {
          setColor(255, 0, 0);  // red
        }
        else if (wavecolor > 0 && wavecolor < 333) {
          setColor(0, 255, 0);  // green
        }
        else if (wavecolor >= 333 && wavecolor < 667) {
          setColor(0, 0, 255);  // blue
        }
        else if (wavecolor >= 667 && wavecolor < 1022) {
          setColor(255, 255, 0);  // yellow
        }
        else if (wavecolor >= 1022) {
          setColor(80, 0, 80);  // purple
        }
      }
    }

    void setColor(int red, int green, int blue)
    {
    #ifdef COMMON_ANODE
      red = 255 - red;
      green = 255 - green;
      blue = 255 - blue;
    #endif
      analogWrite(redpin, red);
      analogWrite(greenpin, green);
      analogWrite(bluepin, blue);
    }

Read up on logical and relational operators in Arduino (`||`, `&&`, `>`, `<`, `==`, `>=`, `<=`) etc.
<br> I will leave the logic development to you as it is what develops your coding ability and trust me logic is the most important and fun part of writing a code!


body:
This sketch does not compile in the Arduino IDE

    void setup() {
      // put your setup code here, to run once:

    }

    struct test {
      int i;
      char variable[];
    };

    typedef struct test test;

    test t = {
      0, "hi"
    };

    void loop() {
      // put your main code here, to run repeatedly:

    }

Arduino throws

    sketch_may09a:16: error: initializer-string for array of chars is too long [-fpermissive]

     };

     ^

    exit status 1
    initializer-string for array of chars is too long [-fpermissive]

However compiling with g++ works just fine. How can I fix it? Or is there a principle reason why flexible array members are not supported?

title: Why does a variable size struct not compile in the Arduino IDE?

Answers: 2

body:
Maybe you've compiled it in g++ without any warnings enabled (or maybe it yeld warnings but was compiled). The arduino uses flags to consider all warnings as an error, so it won't compile. It may wary for different platforms, I've got -fpermissive used (and I don't like it at all).

That's because size of struct must be known compile time and if you provide `char variable[];` you'll get zero sized array. Therefore anything biiger than nothing you'll try to initialize for is too long.

Maybe you're looking for something like this:

    struct test {
      int a;
      const char * ptr;
    };

    test paramx {1, "some string"};
    test paramy {2, "another string"};

    test array[] = {{3,"three"}, {4, "four"}, {5,"five"}};

body:
*Flexible array member* is a C feature. It does not exist in C++. On top of that, the way you use it to declare and initialize a static struct of flexible size is non-standard even for C.

**However**, GNU-C language supports it as an extension. Also, newer versions of GCC (6 and higher) allow this in GNU-C++ code as an extension as well. But GCC 5.4.0 used by Arduino IDE doesn't support this non-standard feature in C++ code.

If you flip through different GCC versions on Godbolt (https://godbolt.org/z/Iul7hD) you'll see that support for this feature has always been present in C code, but for C++ code it first appeared in GCC 6.

This is why you were able to compile it with your standalone `g++` compiler (which is apparently a much later version), but were unable to compile it in Arduino IDE.

It means that if you really want this non-standard code to compile in the current version of Arduino IDE, you have to place it into a `.c` file. Or just specify the array size explicitly.


body:
I want to use a lot of `.h`/`.cpp` files to be used by my `ino` sketch.
In one of these .cpp files (and later more) I want to use constant arrays (like lookup tables). To save SRAM I want to use `PROGMEM`.

What worked before in the Ino sketch is:

    const uint8_t _red[]   PROGMEM = { 0, 9, 10, 220, 230, 240, 255 };

But when I put the line above in a `.h` or `.cpp` file I get the error:

    Par.h:13:25: error: expected initializer before 'PROGMEM'

Should I include something or can't I use `PROGMEM` in a `.h`/`.cpp` file?

title: How to use PROGMEM in a .h/.cpp file instead of a .ino file?

Answers: 1

body:
I already found the answer.

Include:

    #include <avr/pgmspace.h>

**Update**

See also remark of AnT, which is a more generic (and in most cases better solution):

    #include "Arduino.h"

(just to help others with the same problem).


body:
I've a code which records sound to SD, then starts an FTP connection for a limited time. I want to make an automatic system which records then sends the sound file continuously. Now I'm recording sound for 3s, then opening a FTP connection for 3s. In that 3s interval, my python code (PC) is trying to download the data in ESP (it also runs continuously. However, sometimes if ftp time is not enough, my connection is interrupting and ESP is resetting itself. So, I dont want to fetch this file via my computer, ESP32 should send this file after the recording, then it should start recording after the successful connection. I couldn't configured the code in the [Arduino Playground][1]
I pasted my code below, waiting for suggestions for valid examples.
You can find the libraries [here][2]

    #include <WiFi.h>
    #include <WiFiClient.h>
    #include "ESP32FtpServer.h"
    #include <FS.h>
    #include <SD.h>
    #include "Wav.h"
    #include "I2S.h"

    const int record_time = 3;  // second
    const char filename[] = "/sound.wav";
    const int headerSize = 44;
    const int waveDataSize = record_time * 88000;
    const int numCommunicationData = 8000;
    const int numPartWavData = numCommunicationData / 4;
    byte header[headerSize];
    char communicationData[numCommunicationData];
    char partWavData[numPartWavData];
    unsigned long starttime;
    unsigned long endtime;
    File file;

    const char* ssid = "*****";
    const char* password = "*****";

    FtpServer ftpSrv;   //set #define FTP_DEBUG in ESP32FtpServer.h to see ftp verbose on serial

    void setup(void) {
      Serial.begin(115200);
      WiFi.begin(ssid, password);
      Serial.println("");

      // Wait for connection
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.print("Connected to ");
      Serial.println(ssid);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());

      /////FTP Setup, ensure SD is started before ftp;  /////////
      if (SD.begin()) {
        Serial.println("SD opened");
        ftpSrv.begin("esp32", "esp32");   //username, password for ftp.  set ports in ESP32FtpServer.h  (default 21, 50009 for PASV)
      }
      CreateWavHeader(header, waveDataSize);

    }

    void loop(void) {

      SD.remove(filename);
      file = SD.open(filename, FILE_WRITE);
      if (!file) return;
      Serial.println("recording");
      file.write(header, headerSize);
      I2S_Init(I2S_MODE_RX, I2S_BITS_PER_SAMPLE_32BIT);
      for (int j = 0; j < waveDataSize / numPartWavData; ++j) {
        I2S_Read(communicationData, numCommunicationData);
        for (int i = 0; i < numCommunicationData / 8; ++i) {
          partWavData[2 * i] = communicationData[8 * i + 2];
          partWavData[2 * i + 1] = communicationData[8 * i + 3];
        }
        file.write((const byte*)partWavData, numPartWavData);
      }
      file.close();

      Serial.println("recording finished, ftp starting");

      starttime = millis();
      endtime = starttime;
      while ((endtime - starttime) <= 3000) // do this loop for up to 1000mS
      {
        ftpSrv.handleFTP();        //make sure in loop you call handleFTP()!!
        endtime = millis();
      }
      Serial.println("ftp finished");

    }

  [1]: https://playground.arduino.cc/Code/FTP/
  [2]: https://ufile.io/kp0gl1w7

title: Send file from ESP to server via FTP

Answers: 0


body:
I am using an Arduino UNO which worked fine until today. My system: Ubuntu 18.04

Now, the IDE can no longer connect to it:

 - Cannot do any sketch upload
 - Cannot ask for board info
 - No USB ports shown in tools --> port (only 2 serial ports shown)
 - `ttyyACM0` not showing up in `/dev`

However, both `lsusb` and `dmesg` show that the board is recognized somehow.

`lsusb` output:

`Bus 001 Device 012: ID 03eb:2fef Atmel Corp.`

`dmesg` output:

    [ 2922.171042] usb 1-1.3: new full-speed USB device number 12 using ehci-pci
    [ 2922.282047] usb 1-1.3: New USB device found, idVendor=03eb, idProduct=2fef
    [ 2922.282052] usb 1-1.3: New USB device strings: Mfr=0, Product=1, SerialNumber=0

The problem is the same on 2 different computers, which leads me to suspect that there is something wrong with the board. Is it possible that a board be shown by both `dmesg` and `lsusb` and still be malfunctioning ?

title: Cannot connect through /dev/ttyACM0 (Ubuntu 18.04)

Answers: 1

body:
The board was bad. I just replaced it and it now works.

So, to answer my own question, an Arduino board can show up on `lsusb` and `dmesg` and yet be faulty. This is what the `dmesg` output is for a healthy board:

    [10492.388028] usb 1-1.4: new full-speed USB device number 13 using ehci-pci
    [10492.500520] usb 1-1.4: New USB device found, idVendor=2341, idProduct=0043
    [10492.500526] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=220
    [10492.500529] usb 1-1.4: Manufacturer: Arduino (www.arduino.cc)
    [10492.500533] usb 1-1.4: SerialNumber: 55736323739351010292
    [10492.520354] cdc_acm 1-1.4:1.0: ttyACM0: USB ACM device
    [10492.520832] usbcore: registered new interface driver cdc_acm
    [10492.520835] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters

And here is the `lsusb` output:

    Bus 001 Device 013: ID 2341:0043 Arduino SA Uno R3 (CDC ACM)

Judging on the difference with the previous outputs from both commands (when the faulty board was connected), it seems that my board had reverted to the basic Atmel USB interface and that the Arduino specific USB handshake was not working. Can anyone confirm ?


body:
I have followed numerous guides and tutorials for getting the NRF24L01+ modules working with an Arduino Uno. I have swapped to a genuine Arduino and tried 4 different NRF modules. All without luck.

I am trying to get the most basic example from [Simple nRF24L01+ 2.4GHz transceiver demo][1] to work.

Hardware Connections:
[![Hardware Connections][2]][2]

Simple TX Test Code:

    // SimpleTx - the master or the transmitter

    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>

    #define CE_PIN   9
    #define CSN_PIN 10

    const byte slaveAddress[5] = {'R','x','A','A','A'};

    RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

    char dataToSend[10] = "Message 0";
    char txNum = '0';

    unsigned long currentMillis;
    unsigned long prevMillis;
    unsigned long txIntervalMillis = 1000; // send once per second

    void setup() {

        Serial.begin(9600);

        Serial.println("SimpleTx Starting");

        radio.begin();
        radio.setDataRate( RF24_250KBPS );
        radio.setRetries(3,5); // delay, count
        radio.openWritingPipe(slaveAddress);
    }

    //====================

    void loop() {
        currentMillis = millis();
        if (currentMillis - prevMillis >= txIntervalMillis) {
            send();
            prevMillis = millis();
        }
    }

    //====================

    void send() {

        bool rslt;
        radio.stopListening();
        rslt = radio.write( &dataToSend, sizeof(dataToSend) );
            // Always use sizeof() as it gives the size as the number of bytes.
            // For example if dataToSend was an int sizeof() would correctly return 2

        Serial.print("Data Sent ");
        Serial.print(dataToSend);

        if (rslt) {
            Serial.println("  Acknowledge received");
            updateMessage();
        }
        else {
            Serial.println("  Tx failed");
        }
    }

    //================

    void updateMessage() {
            // so you can see that new data is being sent
        txNum += 1;
        if (txNum > '9') {
            txNum = '0';
        }
        dataToSend[8] = txNum;
    }

Serial monitor:

[![enter image description here][3]][3]

Using `radio.printDetails( );` returns:

[![enter image description here][4]][4]

Please let me know what I'm missing.

  [1]: https://forum.arduino.cc/index.php?topic=421081.0
  [2]: https://i.stack.imgur.com/6nt1C.png
  [3]: https://i.stack.imgur.com/iAvZ8.png
  [4]: https://i.stack.imgur.com/JT2Oj.png

title: NRF24L01+ Getting Started Acknowledge Failed

Answers: 1

body:
The Arduinos 3V3 does not privide enough current. To eliminate this as a source of your problem you can use a ceramic capacitor of 100µF across VCC and GND pins of the nRF24L01+ module or buy a [5V to 3V3 step down converter specially made for nRF24L01][1].

  [1]: https://www.aliexpress.com/item/New-Socket-Adapter-plate-Board-for-8Pin-NRF24L01-Wireless-Transceive-module-51/32825890092.html


body:
I use a Adafruit Ultimate GPS Breakout - 66 channel w/10 Hz updates to collect data on animal movement.

I have two questions regarding the produced NMEA:
- there are some information about PGACK, PMTK that are produced in the code that I don't need. Do you know a way of not including it?
- I'd like to add some information in the NMEA line, but when I do it, it creates a space between the NMEA and the new information that do not allow me to process it correctly then in Excel or R. This is the information I've added:

    fichier.print(buffer);
    fichier.print(",");
    fichier.print(GPS.hour, DEC);
    fichier.print(':');
    fichier.print(GPS.minute, DEC);
    fichier.print(':');
    fichier.print(GPS.seconds, DEC);
    fichier.print(",");
    fichier.print(GPS.day, DEC); fichier.print('/');
    fichier.print(GPS.month, DEC); fichier.print("/20");
    fichier.print(GPS.year, DEC);
    fichier.print(",");
    fichier.println(j);
    fichier.close();
    acquisition = true;

title: Modifying NMEA of Adafruit GPS

Answers: 0


body:
While running a program compiled in the Arduino IDE 1.8.5 and then using the `.ELF` file inside *LabCenter Proteus 8.0*, for an *Arduino Nano* *ATMEL Mega 328P*, for a code involving the use of the `SoftwareSerial.h` Library, hence involving several loops, without user interaction, I am getting the following error:

    Invalid opcode 0x0024 at PC=0x0096

Now I am completely lost. According the [AVR Instruction Set Manual][1] such opcode should (not sure about this?) be *RETI - Return From Interrupt* at the Program Line (?) no.150 = 0x096. The full code has 779 lines plus several library calls, and the line 150 happens to fall just before the `loop()` function (?), which should never repeat, being just a declaration.

So, how should I debug this error? Evidently I am not asking to solve the problem for me but, how should I proceed for debugging it and find the real error?

I have been unable to find in Proteus a tool for "Debug Stop if Error"

I have been unable to isolate the error in a simple way to share. It should be related someway with interruptions used by the library `SoftwareSerial.h`, because at some modifications of the program, the code freezes at an interruption return. I am working on that, but advancing really slowly actually.

[![enter image description here][2]][2]
[![enter image description here][3]][3]
[![enter image description here][4]][4]
[![enter image description here][5]][5]

Related question [No.1][6].

  [1]: http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-0856-AVR-Instruction-Set-Manual.pdf
  [2]: https://i.stack.imgur.com/Ctx6N.png
  [3]: https://i.stack.imgur.com/UeXmh.png
  [4]: https://i.stack.imgur.com/Fowci.png
  [5]: https://i.stack.imgur.com/Y0b0g.png
  [6]: https://electronics.stackexchange.com/questions/65271/how-to-get-rid-of-invalid-opcode-0x002a-at-pc-0x004-in-isis-proteus

title: Proteus - How to Debug Invalid Opcode?

Answers: 2

body:
First: Use the ino.hex file instead of the  .ELF file
Second: this error is showing because you are compiling your code with a different board. Make sure you are using the correct board over at the Arduino IDE. My advice is: switch your board to Arduino Uno both in proteus and over at the Arduino IDE and see if it works. Later on, you can try different boards.

body: Guys if you are facing the error of invalid opcode 0xffff at pc = 0x3e402 in proteus using Arduino, the GO to Arduino IDE software, and from the top menu, click on 'tool' and change the board type, e.g., Arduino UNO to Arduino Mega 2560.


body:
I use VSCode, not the Arduino IDE. I saw and tried to use a lot of code snippets which are provided for Arduino projects...

So when I copy/paste these examples into my VSCode and try to compile it fails. (I won't believe that people offer non-working examples.)

When I place the functions above the function calls it compiles.

Is this normal behavior or do I have to add some settings to get this working as provided?

**Fails in VSCode** (works in Arduino IDE):

    #include "Arduino.h"
    void setup() {
    }
    int zero = 0;

    void loop() {
      zero = myfunction();
    }

    int myfunction(){
      return 0;
    }

**Works in VSCode** (Works in Arduino IDE):

    #include "Arduino.h"

    void setup() {
    }
    int zero = 0;

    int myfunction(){
      return 0;
    }

    void loop() {
      zero = myfunction();
    }

title: Project wont compile when I place functions below function calls

Answers: 2

body:
You need to declare your function before you use it.  So either the whole function body needs to be before the usage, or you must add a [function prototype][1] declaration.

    void myfunction();

    myfunction();

    void myfunction(){
      return 0;
    }

  [1]: http://www.cplusplus.com/articles/yAqpX9L8/

body:
Just make the the abstract method declaration before writing your code after the header files.

    #include<Arduino.h>
    void myfunction();
    void setup();
    void loop();

After this you write your code in any order


body: I just recently purchased Usbasp from Ali Express, when i'm connecting it to the windows 10 desktop in device manager its Unknown USB Device (device descriptor request failed). How to fix this error? Even in Linux its not detecting.

title: how to fix USBASP Unknown USB Device (device descriptor request failed) - Win 10?

Answers: 0


body:
    int tPin = 11;
    int ePin = 12;
    long duration, inches, cm;

    void setup()
    {
        Serial.begin(9600);
        pinMode(tPin, OUTPUT);
        pinMode(ePin, INPUT);
        pinMode(2, OUTPUT);
    }

    void loop()
    {
        digitalWrite(tPin, LOW);
        delayMicroseconds(5);
        digitalWrite(tPin, HIGH);
        delayMicroseconds(10);
        digitalWrite(tPin, LOW);
        duration = pulseIn(ePin, HIGH);
        cm = (duration / 2) / 29.1;
        inches = (duration / 2) / 74;
        Serial.print(inches);
        Serial.print("in, ");
        Serial.print(cm);
        Serial.print("cm");
        Serial.println();
        delay(100);

        if (inches <= 10) {
            digitalWrite(2, HIGH);
            delay(50);
            digitalWrite(2, LOW);
            delay(50);
        }
        else {
            digitalWrite(2, HIGH);
            delay(500);
            digitalWrite(2, LOW);
            delay(500);
        }
    }

The serial print displays the same distance that it first detects. How can I fix this?

title: The Ultrasonic sensor just detects the same distance

Answers: 1

body:
 First of all you need to make cm, inches and duration float to be more precise. Second, you need to make the delay 2 and 10 microseconds between triggers. Overall, if I'm not wrong you can use the code below without changing your circuit.

    int tPin = 11;
    int ePin = 12;
    float duration, inches, cm; //floating numbers to make the result more precise

    void setup()
    {
        Serial.begin(9600);
        pinMode(tPin, OUTPUT);
        pinMode(ePin, INPUT);
        pinMode(2, OUTPUT);
    }

    void loop()
    {
        digitalWrite(tPin, LOW);
        delayMicroseconds(2);         //Delay 2 microseconds
        digitalWrite(tPin, HIGH);
        delayMicroseconds(10);        //Delay 10 microseconds
        digitalWrite(tPin, LOW);
        duration = pulseIn(ePin, HIGH);
        cm = duration / 58.2;          //Instead of duration/2/28.1
        inches = cm / 2.54;            //You can use cm that was created before for conversion
        Serial.print(inches);
        Serial.print("in, ");
        Serial.print(cm);
        Serial.print("cm");
        Serial.println();
        delay(100);

        if (inches <= 10) {
            digitalWrite(2, HIGH);
            delay(50);
            digitalWrite(2, LOW);
            delay(50);
        }
        else {
            digitalWrite(2, HIGH);
            delay(500);
            digitalWrite(2, LOW);
            delay(500);
        }
    }


body:
There are of course a lot of different battery charging boards, which can be used with the Arduino, most being TP4056 based. There are also a few battery charged boards with an on-board display showing the charge status and there are of course also a lot of programmable battery charger controllers (ICs) with status and control interfaces.

But is there any board available, not just the 'raw' IC, using a charging controller with a status interface, which allows access to the status interface from pins on the board? I can't seem to find anything.

For a battery powered Arduino project I am planning, it could at least be very convenient if I could query the battery charging status from software.

title: Battery charger board with status interface

Answers: 2

body:
I know it's been asked quite long time ago, but still.  This is how would have I done the same, with the existing TP4056:

1) use an electric key in a form of mosfet transistor, controlled by digital output pin to disconnect your battery from the charging board once in a while (like every 5-10 minutes or so, depending on your project power drain) for a very short period of time to do the measurement below.

2) while the battery is disconnected, use an analog input pin of arduino with resistor-based voltage divider to see what's your battery voltage. Google for "measure voltage with arduino alone" for schemas and code, it's pretty straightforward.

For obvious reasons, you cannot do this measurement while having your charging board connected to battery, because you'll be getting voltage from the charger, and not from your battery. This is why we disconnected battery from the charging circuit in step 1.

3) Knowing
- current voltage you just get from your battery
- what is the voltage should be on your battery when it's fully charged
- the voltage you allow it to discharge to
... you can calculate percentages

4) digitalWrite to that pin to enable mosfet to connect your battery back to charging circuit

body: You might consider a coulomb counter IC, it is designed to give you the state of charge of the battery. There is a module labeled "LTC4150 Coulomb Counter" that will do exactly what you want.


body:
I am working on an arduino mega 2560 sketch.
I have to parse http query strings.

My program is concatenating strings for each http request.
And i have an http request every second during all day.

I have read that this kind of work may cause ram memory fragmentation. I am very upset about this because my program will randomly crash if there are no free ram memory.

Do you know if there is a way to initialise a string with a given memory length ?

I do not want to work with char arrays because i need indexof, substring functions and comparison with ==

Thanks

title: Arduino Strings concatenation

Answers: 1

body:
There is a way to initialize an Arduino `String` with a given memory length: see `reserve()` method. It will generally greatly reduce or entirely eliminate memory fragmentation, if you can afford reserving a sufficient amount of memory in advance.

Just keep in mind that this is a "soft" boundary: once your string gets longer than the reserved amount, `String` will quietly resume reallocating memory with all those typical consequences.

It is not clear what you plan to do if the pre-reserved amount of memory proves to be insufficient. Fall back to "memory fragmentation"? Anything more elaborate than that will be pretty much as complex as performing manual management of a `char[]` array.

All the functionalities you requested are present for `char[]` arrays as well, might just require a few more lines of code. And it is not clear why one might "need" comparisons to be `==` specifically.

This might also be an XY problem. Why are you trying to concatenate all these requests in memory? What are you planning to do with them once you are done "concatenating"?


body:
Look at this code :

char * function1()
{
    return « hello »
}

Where is stored hello string ?
Is there a copy ?

What i want to avoid is to have a memory leak or accessing to another string instead of hello

Thanks

title: Return constant string

Answers: 2

body:
    const char* function1() { return "hello"; }

> Where is stored hello string ? Is there a copy ?

The string is compiled into the program memory. Before setup() is called the constant values are copied to the data memory (aka SRAM).

The function will return the pointer to the same string (memory location).

To save data memory the string literal can be access directly in program memory.

    __FlashStringHelper* function1() { return F("hello"); }

Cheers!

Ref.

 1. https://en.cppreference.com/w/cpp/language/string_literal
 2. https://www.nongnu.org/avr-libc/user-manual/pgmspace.html

body:
String literals in C and C++ are stored in static memory, which in case of Arduino platform is ordinary data memory.

If we overlook the typos, your code is essentially equivalent to

    char *function1()
    {
      static char literal[6] = { 'h', 'e', 'l', 'l', 'o', '\0' };
      return literal;
    }

with a remark that in C and C++ you are not allowed to modify string literals. (Some nuances also depend on the specific language - C vs. C++ - which you failed to specify). Regardless of the language, it is always a better idea to use `const char *` pointers (as opposed to `char *`) to point to string literals.

There's no dynamic memory allocation here, and therefore no memory leaks possible. What you mean by "accessing another string" is not clear.


body:
I have three libraries that I've written and I'm trying to use. They're stored as follows

<!-- language: lang-none -->

    Documents
    ├── Some/Path/To/Folder
    │   └── firmware.ino
    └── Arduino
        └── libraries
            ├── error
            │   ├── error.h
            │   └── error.cpp
            ├── lmp91000
            │   ├── lmp91000.h
            │   └── lmp91000.cpp
            └── tps7a7100
                ├── tps7a7100.h
                └── tps7a7100.cpp

Both lmp91000.h and tps7a7100.h have `#include "../error/error.h"` and are able to access variables and functions declared in error.h and error.cpp just fine.

firmware.ino includes all three libraries as you would expect them to be included and it is able to create instances of classes defined in both lmp91000 and tps7a7100 and use their methods (which depend on error). I am also able to create instances of a struct (`ERROR`) defined in error.h from firmware.ino, however I am not able to access functions from error.h and error.cpp. Error recived is
```
firmware.ino:19: undefined reference to `LOG_E(ERROR)'
```

However when I remove the guards in error.h that error no longer appears (implying that `LOG_E` can be resolved) but I get the expected errors of multiple definitions as there are multiple `#include error.h`.

My thoughts are that because of the guards error is not being included in a location where the sketch has access to the functions, but I may be completely off on that. I know there must be a way around this, as I'm able to access Wire.h for example from firmware.ino even though that is included in lmp91000.h - how do I solve this?

firmware.ino
```
#include <error.h>
#include <lmp91000.h>
#include <tps7a7100.h>
#include <Wire.h>

lmp91000 pot(12);
tpsa7100 vreg(10,9,8,7,6,5,4);

void setup() {
    Wire.begin();
    Serial.begin(115200);
    while(!Serial);
    vreg.enable();
    pot.enable();
    ERROR misc;
    misc.errorCode = 0x50;
    LOG_E(misc);
}

void loop() {
}
```

error.h
```
#ifndef error_h_included
#define error_h_included

#include "Arduino.h"

// Error Structure
typedef struct{
    int errorCode;
    char const *errorName;
    char const *errorDesc;
    bool isFatalError;
    bool shouldRetry;
}ERROR;

// Global error types
const ERROR E_SUCCESS = {0x00, "E_SUCCESS", "Success", false, false};
const ERROR E_INVALID_ARG = {0x10, "E_INVALID_ARG", "Invalid argument passed", true, false};
const ERROR E_I2C_NACK_ADDR = {0x20, "E_I2C_NACK_ADDR", "NACK on I2C address byte", false, true};
const ERROR E_I2C_NACK_DATA = {0x21, "E_I2C_NACK_DATA", "NACK on I2C data byte", false, true};
const ERROR E_I2C_BUFF_LEN = {0x22, "E_I2C_BUFF_LEN", "Transmit Data too long for I2C buffer", true, false};
const ERROR E_I2C_ERROR = {0x23, "E_I2C_ERROR", "Undefined I2C Error", true, false};
const ERROR E_I2C_TARGET_DISABLED = {0x24, "E_I2C_TARGET_DISABLED", "Target device for I2C comms disabled", false, false};
const ERROR E_I2C_READ_ERROR = {0x25, "E_I2C_READ_ERROR", "I2C Data could not be read", false, false};
const ERROR E_I2C_WRITE_ERROR = {0x26, "E_I2C_WRITE_ERROR", "Attempting to write zero bytes to I2C bus", false, false};

// Function Definitions
bool E(ERROR error_struct);
void LOG_E(ERROR error_struct);
ERROR WireErrorDecode(int code);

#endif
```

error.cpp
```
#include "error.h"

// Error Handling Functions

// E(ERROR)
// Evaluate the successfullness of the error
// Return 0 if error is a success error
// Return >0 if error is not success error
bool E(ERROR error_struct) {
    return (int)(error_struct.errorCode & 0xF0);
}

// LOG(ERROR)
// Log the error
void LOG_E(ERROR error_struct) {
    Serial.print(error_struct.errorCode);
    Serial.print("\t");
    Serial.print(error_struct.errorName);
    Serial.print("\t");
    Serial.println(error_struct.errorDesc);
}

// WireErrorDecode(code)
// Decode Wire.EndTransmission error codes
ERROR WireErrorDecode(int code) {
    switch(code) {
        case 0:
            return E_SUCCESS;
        case 1:
            return E_I2C_BUFF_LEN;
        case 2:
            return E_I2C_NACK_ADDR;
        case 3:
            return E_I2C_NACK_DATA;
        case 4:
            return E_I2C_ERROR;
        default:
            return E_INVALID_ARG;
    }
}
```

**Update:** These errors seem to be board specific - see question [here][1]

  [1]: https://arduino.stackexchange.com/questions/65398/code-comilation-erros-for-101-no-errors-with-uno

title: Unresolved reference to public function from library

Answers: 1

body:
The filename `error.h` is not a good name for a library. Some compilers and cores (such as the ESP32 and Curie cores) have a file `error.h` of their own which will be used in preference to yours.

Instead, name your library something more sensible.


body: I was uploading a sketch to my Arduino Leonardo and I pressed the reset button accidentally while it was still uploading. Now the board does not get recognized by my computer. Is there something that can be done to save it or should I get a new one?

title: Leonardo soft bricked

Answers: 1

body:
You'll need to program it trough the ISP port with an AVR programmer if you can't program it through a reset or double reset anymore. This can be done using another arduino. A tutorial is on the [arduino.cc website][1].

  [1]: https://www.arduino.cc/en/Tutorial/ArduinoISP