text:
Controling 5V relay with Wemos D1 R1
I would like to control a 5V relay with Wemos D1 R1 module through webserver. I'm using the following modules:

Tongling 5vdc Relay
Wemos D1 R1

I'm using the following schematic:

and here is the code I'm using:
#include <ESP8266WiFi.h>

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

int ledPin = D3;
WiFiServer server(80);

void setup() {
 Serial.begin(9600);
 delay(10);

 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, LOW);

 // Connect to WiFi network
 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);

 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected");

 // Start the server
 server.begin();
 Serial.println("Server started");

 // Print the IP address
 Serial.print("Use this URL : ");
 Serial.print("http://");
 Serial.print(WiFi.localIP());
 Serial.println("/");

}

void loop() {
 // Check if a client has connected
 WiFiClient client = server.available();
 if (!client) {
   return;
 }

 // Wait until the client sends some data
 Serial.println("new client");
 while(!client.available()){
   delay(1);
 }

 // Read the first line of the request
 String request = client.readStringUntil('\r');
 Serial.println(request);
 client.flush();

 // Match the request

 int value = LOW;
 if (request.indexOf("/LED=ON") != -1) {
   digitalWrite(ledPin, HIGH);
   value = HIGH;
 }
 if (request.indexOf("/LED=OFF") != -1){
   digitalWrite(ledPin, LOW);
   value = LOW;
 }

 // Return the response
 client.println("HTTP/1.1 200 OK");
 client.println("Content-Type: text/html");
 client.println(""); //  do not forget this one
 client.println("<!DOCTYPE HTML>");
 client.println("");

 client.print("Led pin is now: ");

 if(value == HIGH) {
   client.print("On");
 } else {
   client.print("Off");
 }
 client.println("

");
 client.println("Click here Turn relay ON
");
 client.println("Click here Turn relay OFF
");
 client.println("");

 delay(1);
 Serial.println("Client disconnected");
 Serial.println("");

}

After uploading the code successfully and watching the monitor, the light gets turned on and I get the following message:
16:02:20.400 -> WiFi connected
16:02:20.425 -> Server started
16:02:20.479 -> Use this URL : http://192.168.100.7/
16:02:41.680 -> new client
16:02:41.680 -> GET / HTTP/1.1
16:02:41.680 -> Client disconnected

However, when I turn on or off the relay using the browser, nothing happens:
19:38:56.010 -> new client
19:38:56.010 -> GET /LED=ON HTTP/1.1
19:38:56.010 -> Client disconnected
19:38:57.016 -> new client
19:38:57.016 -> GET /LED=OFF HTTP/1.1
19:38:57.063 -> Client disconnected

However nothing happens to the light and relay doesn't click.
a few more notes:

I tried in upload the code with relay at high to disconnect the circuit but light is still on
Wemos Network name appears in my network list
It doesn't not respond to AT Commands

did you in Tools select exactly the  "D1 R1"?

@Juraj  yes the board selected is Wemos D1 R1

Your code should switch off the lamp when starting up (set the relay pin to LOW), but this is not what you experience. Are you sure you connected the lamp correctly (to COM and NO and not COM and NC)? What happens when you connect an LED (through a 200 to 330 ohm resistor) to Pin 3 instead of the relay?

The negative side of the LED should be connected to ground.

@StarCat I replaced the relay with LED with resistor and it just works perfect :)

So, now you know that the pin switches at the correct time, you have confirmation that your code works correctly and the problem is likely with your hardware. I think the fact that you're trying to switch a 5V relay with a 3.3V device might be the problem (see Duncan C's answer).

@StarCat I tried to power the 5V Relay from Arduino UNO R3 by attaching VCC and GND to it and keeping the in wired to Wemos D1 R1 D3 pin, but it didn't work either. Any suggestion to resolve the issue with additional Modules.

The relay likely needs about 5V on the "IN" pin to be able to switch the relay, which it will not get from your Wemos D1 as that is a 3.3V device. If you want to control a relay from your Wemos D1, you need a one that works with 3.3V.

@StarCat Solved by implementing 2N2222 transistor and 5K resistor. Thx for the help

The issue lies with Wemos Module where it only outputs 3.3V which insufficient to 5V Relay so this can be solved by implementing 2N2222 Transistor as per schematic below.

Is the Wemos D1 R1 a 5V board, or 3.3V? If it's a 3.3V board, that's your problem. Get a 3.3V relay.
(From a little digging it looks like the Wemos is a 3.3V device. If so, that is your problem. Those "digital relays" are available in 5V and 3.3V models. Replace your 5V relay with a 3.3V relay.)
What are the current requirements of the relay, and what is the max current you can draw out of the Wemos? How are you powering the Wemos?
I would say break this into pieces and solve them separately. Put a multimeter (OR an LED with a current limiting resistor) on pin D3 and make sure the pin is turning on and off. If it is, it is a problem with your relay. If it's not, it's a problem with your sketch or your Arduino.
If the pin is turning on and off, try attaching the IN pin on your relay directly to +5V. Does your lightbulb light then?

The led is working perfect but for the last recommendation by attaching the in pin to +5V, nothing happens except the bulb turning off as soon as I pulled the wire out of D3

You disconnect the wire from D2 and hook it directly to the +5 (or rather the +3.3v) pin, and the lightbulb does not light? What if you touch that wire to ground? (I'm wondering if you're wired your lightbulb into the normally open (NC) terminal on your relay rather than normally closed (NC).

@DuncanC I wired it to NC and the same thing no response, however light is off now. BTW, I don't hear any clicks from the relay.

A 5V relay requires 5 volts to operate. Your Wemos appears to be a 3.3V device. It only puts out 3.3V on it's logic lines. Get a voltmeter and measure the voltage on pin D3 when it is set to HIGH. I bet dollars to doughnuts it's putting out 3.3V. Buy a 3.3V of your digital relay and use that instead (Assuming your Wemos has enough current out of it's regulator to power the relay.


text:
What functions are disabled with noInterrupts()?
The Arduino page for noInterrupts() says:

Some functions will not work while interrupts are disabled, and incoming communication may be ignored.

While it may not be possible to give a complete listing of what would be disabled, having an incomplete list of functions which definitely are disabled would be useful.
I was thinking of calling a DHT sensor library which internally uses millis() while interrupts were disabled to avoid both bit banging and servicing interrupts simultaneously if possible (the interrupt would hold and be processed after).
Which functions are definitely disabled when interrupts are disabled?
What about incoming communication? My guess is that it would be slightly worse on the ATmega32U4 (eg Leonardo) as the one processor does USB and everything else as well.

The following command grep ISR *.cpp will generate a list of all ISR functions in the Arduino core. These are the HardwareSerial (UART/USB: receive, transmitt) and Timer functions (millis, micros, tone).

From what I found turning off interrupts will definitely disable incrementing values of micros() and millis() This means that also every method that uses them like delay() will also not work correctly.
Of course manually attached interrupts using attachInterrupt() will also not work as intended.
According to comments by @Juraj Things that also will not work are Serial interrupts. Also libraries like Wire require interrupts for their logic.
EDIT: @EdgarBonet You are right, I removed incorrect info.

Serial receive is handled with interrupt for hardware Serials (including native USB) and software Serials

Wire library send and receive is implemented in an interrupt

The PWM you get from analogWrite() is not handled by interrupts: it's generated by the timers themselves.

SPI.transfer() also uses interrupts - but you can 'manually' wait them out to know when to proceed.  For example, I had a program where I needed to send out 45 bytes in 50uS (20KHz refresh rate). During testing, I kept seeing jitters in the datastream - about every 4uS if I recall. The micros() interrupt time. Turned off interrupts during the data burst and the jitter went away. So the command to send out a byte looked this, repeated 45 times (no for loop):
spdr = byte0; nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
spdr = byte1; nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
spdr = byte2; nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
:
spdr = byte43; nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
spdr = byte44; nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;

So the transfer needed 17 clocks with the SPI clock set to 8 MHz.
After the transfer I turned interrupts back on to wait for the next incoming signal edge to send out the next burst.

source: Stack Exchange

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/

sort: votes


text:
Interpreting pull-up values Rp and Rpu in AM3202 (DHT22) datasheet
In the AM2302 datasheet at page 3, table 4, it gives min, typ and max values for Rpu (that the pull-up resistor as it says verbatim) being 30K, 45K and 60K ohm respectively.
Elsewhere in the same table it gives figures for Rp (no u) < 25K ohm.
What is the difference between Rpu and Rp here, and how should the two rows be interpreted?

The datasheet is as inaccurate as the sensor itself and it is even worse than you think!
In that datasheet is also written: "usually require an external about 5.1kΩ pull-up resistor".
In the datasheet is also mentioned that the output signal can go up to 8 mA for a high output and minimal 10 µA low output. The 10 µA means that the pullup resistor should be 500k or more. That is not correct.
In this datasheet is a picture with 1k.
In this Chinese datasheet is a picture with 5k.
I ran that Chinese datasheet through Google Translate, but that did not help.
Conclusion: The datasheet is not reliable.
You can do as everyone else: use a 10k resistor for short wires, and try 4k7 for longer wires.
That is not the only problem. Some DHT libraries create a shortcut on the data signal.
The AM3202 is not accurate for measuring the humidity. If you want to avoid trouble, then use a better sensor.
A sensor from Bosch is a good choice (for example the BME280 or BME680). Bosch makes datasheets that are reliable. Those sensors from Bosch operate at 3.3V, if you are going to connect that to a 5V Arduino board, then you need level shifters for the signals.

Thanks @Jot. Any idea on the accuracy on the figure of 2s between reads?
Instead of the "default" Adafruit library, I've looked at this and this which have much cleaner code for my project which is not accuracy critical.

Any reason not to use INPUT_PULLUP instead in this case? Actually, I asked this as a separate question: Using a pull-up resistor vs INPUT_PULLUP / OUTPUT_PULLUP

@TomHale Those sensors are not getting better with more than 2 seconds in between. You should ignore everything that is written in the datasheet about accuracy, it is a bunch of nonsense. It is the sensing element that is unreliable. As far as I can tell, both of the libraries that you mention do not put the data line as output and high (that creates a shortcut). So both are okay. The internal pullup resistor of the arduino is between 30k and 50k, that value is too high to be reliable, and the value is different for different arduino boards. You can use it for a test, it will probably work.

Cheers. FYI, the datasheet says 20K-50K, as shown the pull-up question.

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/


text:
Digital I/O threshold voltages for ATmega32u4
What are the voltages on a ATmega32u4 (eg Arduino Leonardo) such that a HIGH or LOW is read?
I want to ensure that I'm reading the chip reference manual correctly, where Table 29-1 says:

VIL: Input Low Voltage, Except XTAL1 and Reset pin

Min: -0.5
Max: 0.2VCC-0.1V (LVTTL)

VIH: Input High Voltage, Except XTAL1 and RESET pins

Min: 0.2VCC+0.9V (LVTTL)
Max: VCC + 0.5V

My questions:

What is LVTTL?
How do I interpret the VIL Max and VIH Min?
Is the input undefined between those two values?

LVTTL is Low Voltage Transistor-Transistor Logic. In general, the
term "TTL" is used for a digital signal.
The "Low Voltage" part is special,
the ATmega32U4 switches from low to high (and vice versa)
at low voltages.
Just as it is written. For example, when the ATmega32U4 runs at 5V,
then:
VILmax = 0.2 * 5 - 0.1 = 0.9 V.
VIHmin = 0.2 * 5 + 0.9 = 1.9 V.
Between those values the input signal is seen as low or high,
but the exact voltage when it switches from low to high may
vary.
When the microcontroller is running at 5.0 V and is used
within the specifications of the datasheet, then a voltage
below 0.9 V is always seen as low and
a voltage above 1.9 V is always seen as high (guaranteed).
Those voltages is what a electronics designer must consider.

In the datasheet of the ATmega32U4 at page 403 is paragraph 30.7 "Pin Threshold and Hysteresis" with figure 30-22 and 30-23.
The figures show that at 25°C and 5V, the input switches from low to high typical at 1.55 V and it switched from high to low at maybe a slightly lower voltage.
The hysteresis is almost none and the voltage depends on VCC and the temperature.

(Thanks to Edgar Bonet for the correction/addition)

Re “it switches from low to high at an average of 1.4 V”: the average threshold voltages are not specified. The datasheet, however, provides graphs with the typical thresholds as a function of temperature and Vcc. Both VIH and VIL are typically between 1.5 and 1.6 V at 25 °C and Vcc = 5 V.

@EdgarBonet, thanks! I found the graphs and I will add it to my answer.


text:
avrdude: Error: butterfly programmer uses avr_write_page() but does not provide a cmd() method
When trying to flash my sketch, I see many errors such as:
 ***failed;
 ***failed;
 ***failed;
avrdude: Error: butterfly programmer uses avr_write_page() but does not
provide a cmd() method.
 *** page 127 (addresses 0x7400 - 0x747f) failed to write

And also:
avrdude: error: programmer did not respond to command: set addr

How can I successfully flash my sketch?

After much frustration in trying things, I came across this forum post which advised:
sudo systemctl stop ModemManager.service

If this works, disable the service from starting next boot:
sudo systemctl disable ModemManager.service

It seems like setting ENV{ID_MM_DEVICE_IGNORE}="1" in a udev rule is insufficient.


text:
invalid conversion from 'uint16_t* error when creating an array of uint16_t items
I'd like to create an array of remote control codes provided by the raw read feature of the irlib2 library.
irlib2 provides the following uint16t arrays using the following format:
#define RAW_DATA_LEN 68

const uint16_t powerOff[RAW_DATA_LEN]={
  8550, 4306, 530, 1606, 530, 566, 502, 1610,
  530, 566, 502, 574, 506, 1630, 506, 566,
  506, 1630, 506, 566, 502, 1610, 530, 566,
  502, 1634, 506, 1606, 530, 570, 498, 1634,
  506, 570, 510, 562, 506, 566, 502, 1634,
  506, 1606, 530, 1610, 530, 562, 538, 538,
  530, 542, 538, 1598, 538, 1570, 558, 542,
  538, 538, 530, 542, 538, 1598, 530, 1578,
  558, 1578, 562, 1000};

I'd like create an array of these arrays using the code below, but get the following errors.
CODE
#define RAW_DATA_LEN 68

const uint16_t powerOff[RAW_DATA_LEN]={
  8550, 4306, 530, 1606, 530, 566, 502, 1610,
  530, 566, 502, 574, 506, 1630, 506, 566,
  506, 1630, 506, 566, 502, 1610, 530, 566,
  502, 1634, 506, 1606, 530, 570, 498, 1634,
  506, 570, 510, 562, 506, 566, 502, 1634,
  506, 1606, 530, 1610, 530, 562, 538, 538,
  530, 542, 538, 1598, 538, 1570, 558, 542,
  538, 538, 530, 542, 538, 1598, 530, 1578,
  558, 1578, 562, 1000};

uint16_t sourceCD[RAW_DATA_LEN]={
  8546, 4310, 558, 1578, 562, 538, 498, 1638,
  530, 542, 506, 570, 502, 1634, 502, 570,
  498, 1638, 534, 538, 498, 1638, 530, 542,
  506, 1606, 554, 1582, 558, 538, 510, 1602,
  554, 546, 506, 566, 502, 570, 510, 1602,
  554, 1582, 558, 538, 510, 566, 502, 1606,
  554, 546, 502, 1610, 558, 1574, 554, 546,
  502, 570, 510, 1602, 526, 1610, 526, 574,
  506, 1602, 526, 1000};

uint16_t sourceAUX[RAW_DATA_LEN]={
  8550, 4310, 526, 1634, 506, 566, 502, 1634,
  506, 566, 502, 574, 506, 1626, 510, 566,
  506, 1630, 506, 566, 502, 1634, 506, 566,
  502, 1634, 506, 1606, 530, 566, 506, 1630,
  506, 566, 502, 1634, 506, 1630, 506, 1630,
  510, 1602, 526, 570, 510, 566, 502, 1634,
  502, 570, 502, 570, 510, 566, 502, 570,
  510, 562, 506, 1630, 506, 1630, 510, 562,
  506, 1630, 510, 1000};

uint16_t sourceCDR[RAW_DATA_LEN]={
  8550, 4306, 530, 1606, 534, 566, 502, 1606,
  534, 566, 502, 570, 510, 1602, 526, 570,
  510, 1602, 534, 566, 502, 1606, 534, 566,
  506, 1602, 530, 1606, 534, 566, 502, 1610,
  530, 570, 498, 574, 506, 566, 502, 570,
  510, 1602, 526, 570, 510, 566, 502, 570,
  510, 1602, 526, 1610, 526, 1606, 534, 1602,
  534, 566, 502, 1610, 530, 1602, 534, 1602,
  526, 574, 506, 1000};

//This is the problem code:
uint16_t sources[3] = {sourceCD, sourceAUX,  sourceCDR};

ERRORS
warning: invalid conversion from 'uint16_t* {aka unsigned int*}' to 'uint16_t {aka unsigned int}' [-fpermissive]
 uint16_t sources[3] = {sourceCD, sourceAUX,  sourceCDR};
warning: invalid conversion from 'uint16_t* {aka unsigned int*}' to 'uint16_t {aka unsigned int}' [-fpermissive]
warning: invalid conversion from 'uint16_t* {aka unsigned int*}' to 'uint16_t {aka unsigned int}' [-fpermissive]

I gather this has something to do with mixing and converting invalid types, but I'm not quite sure where to go from here

sources is array of pointers to arrays so uint16_t* sources[] = {sourceCD, sourceAUX,  sourceCDR};. array variable is a pointer to first member location

That doesn't quite fix it. I still get this error 3 times: warning: invalid conversion from 'const uint16_t* {aka const unsigned int*}' to 'uint16_t* {aka unsigned int*}' [-fpermissive] I also tried uint16_t *sources[] = {sourceCD, sourceAUX, sourceCDR}. Any other ideas?

I didn't notice that your first array is const and others are not. make all const. the position of * is not important. you should read the error/warning messages.

@Juraj I noticed that too, but I'm not sure what in the warnings would have indicated that was the problem. Can you help me understand the warning message?

I think the error messages in your Question are not for the code with Question. the first error message would have const mentioned. the messages say that you try to use uint16_t* where uint16_t is expected

You have two options.

You can make an array of arrays like this:

const uint16_t sources[3][RAW_DATA_LEN] = {
    { 8546, 4310, 558, ... }, // CD
    { 8550, 4310, 526, ... }, // AUX
    { 8550, 4306, 530, ... }  // CDR
};

But then you don't have the sourceCD, etc. variables any more. If you
want, you could #define aliases for them like this:
#define sourceCD  (sources[0])
#define sourceAUX (sources[1])
#define sourceCDR (sources[2])

You can make an array of pointers, as suggested in the comments:

const uint16_t sourceCD[RAW_DATA_LEN]  = { 8546, 4310, 558, ... };
const uint16_t sourceAUX[RAW_DATA_LEN] = { 8550, 4310, 526, ... };
const uint16_t sourceCDR[RAW_DATA_LEN] = { 8550, 4306, 530, ... };
const uint16_t *sources[3] = { sourceCD, sourceAUX,  sourceCDR };

This works because, in the last line, the array identifiers such as
sourceCD implicitly “decay” to pointers to their first elements. Note
the const keyword in the last line, needed for consistency with the
types of the arrays declared above.


text:
Read nested array item from PROGMEM
I have a 2D array of uint16_t arrays (IR Remote Codes). Due to the size of this data in RAM I'm having all kinds of trouble when my array is larger than one code. Thus, I have been trying to move this whole structure into PROGMEM. Following various tutorials and this solution, I have come up with the code below for reading a single element out of the array and into a variable that I can feed to the IRLIB2 IRSendRaw function.
The variable that holds the code that is read out of PROGMEM is still huge and causing some problems. Is there a more direct way like a python list comprehension to access an arbitrary element and send it directly to the IRSender?
This PROGMEM_readAnything library looks promising, but I don't have enough C++ fu to make the mental leap to implement it in my code.
#include <IRLibSendBase.h>    //We need the base code
#include <IRLib_HashRaw.h>    //Only use raw sender

#define RAW_DATA_LEN 68
const uint16_t sources[3][RAW_DATA_LEN] PROGMEM =
  {{8550, 4306, 530, 1606, 530, 566, 502, 1610, //power on/off
  530, 566, 502, 574, 506, 1630, 506, 566,
  506, 1630, 506, 566, 502, 1610, 530, 566,
  502, 1634, 506, 1606, 530, 570, 498, 1634,
  506, 570, 510, 562, 506, 566, 502, 1634,
  506, 1606, 530, 1610, 530, 562, 538, 538,
  530, 542, 538, 1598, 538, 1570, 558, 542,
  538, 538, 530, 542, 538, 1598, 530, 1578,
  558, 1578, 562, 1000},

  {8546, 4310, 558, 1578, 562, 538, 498, 1638,  //source CD
  530, 542, 506, 570, 502, 1634, 502, 570,
  498, 1638, 534, 538, 498, 1638, 530, 542,
  506, 1606, 554, 1582, 558, 538, 510, 1602,
  554, 546, 506, 566, 502, 570, 510, 1602,
  554, 1582, 558, 538, 510, 566, 502, 1606,
  554, 546, 502, 1610, 558, 1574, 554, 546,
  502, 570, 510, 1602, 526, 1610, 526, 574,
  506, 1602, 526, 1000},

  {8550, 4306, 530, 1606, 534, 566, 502, 1606,  //source CDR
  534, 566, 502, 570, 510, 1602, 526, 570,
  510, 1602, 534, 566, 502, 1606, 534, 566,
  506, 1602, 530, 1606, 534, 566, 502, 1610,
  530, 570, 498, 574, 506, 566, 502, 570,
  510, 1602, 526, 570, 510, 566, 502, 570,
  510, 1602, 526, 1610, 526, 1606, 534, 1602,
  534, 566, 502, 1610, 530, 1602, 534, 1602,
  526, 574, 506, 1000}};

//Create a sender
IRsendRaw mySender;

//Read out one code into a variable and send to the IR transmitter
uint16_t myCode[RAW_DATA_LEN];
for (int i = 0; i < RAW_DATA_LEN; i++) {
   myCode[i] = pgm_read_word_near(sources[0] + i);
}
mySender.send(myCode,RAW_DATA_LEN,36);

you can't put a multidimensional array into PROGMEM. see "Array of Strings" in https://www.arduino.cc/reference/en/language/variables/utilities/progmem/

@Juraj Sure you can. Strings are a special case though. There's no difference in memory between a 2D array, and a single large 1D array.

It is not problem to fill the rest of the flash memory of an arduino uno with progmem data. For an arduino mega 2560 it is better te keep the progmem data section below the 64k boundery. You copy the data into a buffer of 68 uint16_t, that is 136 bytes. Is that the problem? The arduino uno has 2kbyte ram. What else is using your sram?

I had problems to read a two-dimensional array from PROGMEM. now I know. for one dimension the argument to pgm_read is arr + i. it is a pointer to the element. and for two dimensions the argument to pgm_read must be arr[i] + j

The library, as it stands, doesn't support direct sending from PROGMEM.  However it's not too hard to get around that - if your C++ is up to it.
The IRsendRaw class is a child class that extends the IRsendBase with a single function.  You just need to do the same thing to create a new class that implements the functions in a way that can read from PROGMEM.
This is the original:
class IRsendRaw: public virtual IRsendBase {
  public:
    void send(uint16_t *buf, uint8_t len, uint8_t khz) {
      enableIROut(khz);
      for (uint8_t i = 0; i < len; i++) {
        if (i & 1) {
          space(buf[i]);
        }
        else {
          mark(buf[i]);
        }
      }
      space(0); // Just to be sure
    }
};

You just need to make your own one of those in your sketch, called something different, and which uses the progmem functions instead of accessing buf[i]:
class IRsendRawPGM: public virtual IRsendBase {
  public:
    void send(const uint16_t *buf, uint8_t len, uint8_t khz) {
      enableIROut(khz);
      for (uint8_t i = 0; i < len; i++) {
        if (i & 1) {
          space(pgm_read_word_near(buf + i));
        }
        else {
          mark(pgm_read_word_near(buf + i));
        }
      }
      space(0); // Just to be sure
    }
};

Now you can define your sender object as:
IRsendRawPGM mySender;

And send the data directly:
mySender.send(sources[0], RAW_DATA_LEN, 36);

(Note: this code is untested).


text:
Arduino Coding Issue
I'm running a water pump based on ultra sonic readings. Once the Ultra Sonic Sensor reads > 10 cm , I want the water pump to start until sensor reads < 5 cm. Would you please help me adjust the following code to meet my requirements.
void loop(){
 digitalWrite(trigPin,LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin,HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin,LOW);
 duration=pulseIn(echoPin,HIGH);
 distance=(duration*0.034/2);
 if(distance>5){
   digitalWrite(motor,HIGH);
   delay(30000);
   digitalWrite(motor,LOW);
 } else {
   digitalWrite(motor,LOW);
 }
}

So what you're asking is:

If distance is greater than 10:

Turn on

If distance is less than 5:

Turn off

That's not hard to code, since you already have an "if" there that compares the distance.
 if(distance>5){
   digitalWrite(motor,HIGH);
   delay(30000);
   digitalWrite(motor,LOW);
 } else {
   digitalWrite(motor,LOW);
 }

becomes:
if (distance > 10) {
    digitalWrite(motor, HIGH);
}

if (distance < 5) {
    digitalWrite(motor, LOW);
}

See how there's a direct 1:1 correlation between the English I wrote at the top and the code below it? That's often a good way to understand code: write it out in plain English step by step what you want and what decisions you need to make - then there's usually a simple conversion from English to code.
The hardest part of learning to program is understanding the logic. Do that in English first and the writing of the code itself becomes so much easier.

Thanks. Two points, using the code above, if distance < 10, motor won't work.  I tried adding digitalWrite(motor, HIGH); before if statement but I didn't test it yet. Second, The motor keeps running on repetitive cycles I believe every time code loops. is that healthy?

If distance < 10 then the motor shouldn't work. You haven't specified that it should. You want it on for distance > 10 not < 10. If it's cycling on and off then distance is changing from greater than 10 to less than 5 repeatedly.

With Majenko's code, the motor won't turn on until the distance is > 10. Then it will stay on until the distance drops to < 5. That's what you wanted.

You might want to add a maximum time value as well, so that if your ultrasonic sensor fails with the pump on, it has an upper limit on how long the pump will run. That would require recording the millis() value at the moment you turn the pump on, and turning it off if the motor is on and the current value of millis() is far enough in the past.

@motionchannel: For reference, the behavior described by your prose and implemented in Majenko's code is called hysteretic control. Within the hysteresis band (5 – 10 cm) the pump is supposed to stay in whatever state is was previously set. Hysteresis is a very sensible way of avoiding the motor rapidly switching on and off. See for example Wikipedia: Hysteresis in engineering.


text:
Can you power the ESP8266E-12 with any of the 3.3V pins?
If I want to operate the NodeMCU on the 3.3V input with 2.5V to 3.6V; can I use any of the 3.3V pins to do so?

The 3.3V pins have this name for a reason. You need to provide 3.3V. 2.5V is a bit too low

On the nodeMCU board all the 3.3v pins have continuity. That means they all do the same thing. And yes, you can apply ~3.3v to them. I have trouble with voltages under ~2.8 though, 2.5v is likely not going to work.
Use a buck+boost to keep it 3.3v if needed. For cheap dc/dc converters, I strongly prefer ones with XL semi chips; clean enough to please the ESPs.


text:
Wrong data receiving in my Arduino by bluetooth
I have programmed a client-server between an Arduino and my mobile app in Android. I´m using a buetooth HC-06. For the moment is a very basic code just to turn ON/OFF a led, regulate its voltage, and receive the charge of the battery.
This is my Arduino code for receiving the message:
#include <SoftwareSerial.h>
#include <TimerOne.h>
SoftwareSerial hc06(2,3);

char command;
String string;
boolean ledon = false;
const byte MAX_STRING_LEN = 40;
const int  BLINK_INTERVAL = 1000;

char inputString[MAX_STRING_LEN];  // a string to hold incoming data

int BatteryPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
float BatteryCar = 0;
int PercentageBatt = 0;
int intensidad = 0;
byte strLen   = 0;                 // current length of rec'd string

#define led 11
void setup()
{
  Serial.begin(9600);
  hc06.begin(9600);
  pinMode(led, OUTPUT);
  Timer1.initialize(10000); // set a timer of length 10000 microseconds (or 0.01 sec - or 1Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  Timer1.attachInterrupt( SendVoltage );
}
void loop()
{

  sensorValue = analogRead(BatteryPin);
  BatteryCar = sensorValue * (5.0 / 1023.0);
  if (hc06.available() == 0)
  {
    analogWrite(led, 0);
    delay (10);
  }
  while(hc06.available() > 0)
  {
    command = ((byte)hc06.read());
    if((command == '\n') || (command == '\r'))
    {
      command =='\0';
      break;
    }
    else if (command == 'O')
      analogWrite(led, 255);
    else
      analogWrite(led, 0);
    delay(10);
    Serial.println(command);
  }
}

void SendVoltage ()
{

  PercentageBatt = (BatteryCar * 100)/4;
  hc06.println(PercentageBatt);
}

This is the data that I´m receiving:

It supposed that i should only receive "O" or "F". Any suggestion??
thanks in advanced,

It would appear that whatever is sending the data is sending something else.  Since we cannot see any of that code nobody can do anything but guess at why.

simply ignore unexpected data

why are you printing the value of command after you modify its value?

note:  substituting \0 for \r and for \n is kind of pointless

Does your HC-06 actually run at 9600 baud? Is your serial monitor set for 9600 baud?

Well, after a lot of research, it seems that my main problem was that I´m receiving and sending information at the same time. And with the library Softwareserial, only 1 can be active at the same time.
I found two solutions, but to be honest I'm not complete satisfy with neither of them. However, for my application will work.
The first solution: Is to used the HardwareSerial (pin 0,1) itself, instead of using the pin 2 and 3. When I used those, it didn't have any problem. Although I decided no to used because it help me debugging my code.
The second solution: Is to decrease the frecuency of what I´m sending. For me its ok I send the data every second. So my code would be like this:
#include <SoftwareSerial.h>
#include <TimerOne.h>
SoftwareSerial hc06(2,3);

char command;
String string;
boolean ledon = false;
const byte MAX_STRING_LEN = 40;
const int  BLINK_INTERVAL = 1000;

char inputString[MAX_STRING_LEN];  // a string to hold incoming data

int BatteryPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
float BatteryCar = 0;
int PercentageBatt = 0;
int intensidad = 0;
byte strLen   = 0;                 // current length of rec'd string

#define led 11
void setup()
{
  Serial.begin(9600);
  hc06.begin(9600);
  pinMode(led, OUTPUT);
  Timer1.initialize(1000000);
  Timer1.attachInterrupt( SendVoltage );
}
void loop()
{

  sensorValue = analogRead(BatteryPin);
  BatteryCar = sensorValue * (5.0 / 1023.0);
  if (hc06.available() == 0)
  {
    analogWrite(led, 0);
    delay (10);
  }
  while(hc06.available() > 0)
  {
    command = ((byte)hc06.read());
    if((command == '\n') || (command == '\r'))
    {
      command =='\0';
      break;
    }
    else if (command == 'O')
      analogWrite(led, 255);
    else
      analogWrite(led, 0);
    delay(10);
    Serial.println(command);
  }
}

void SendVoltage ()
{

  PercentageBatt = (BatteryCar * 100)/4;
  hc06.println(PercentageBatt);
}

I have read about this library (AltSoftSerial), but I couldn't get it work. I have usde the pin 8 and 9, but nothing.


text:
What Arduino and shield components are worth recycling?
I'm building up a nice collection of smoked or otherwise bricked Arduino boards. I would like to save bits that might be useful later. I've desoldered a barrel plug to use when I want to separate the power supply to a motor shield. Is there something I could do with the ATmega328 processor, and how would I verify that it is still functional? Any other bits that you rescue?

This isn't a great place for this type of question.  But I suggest you start buying DIP package Arduino board and learn how to replace the Atmel processor and program the boot loader into the brand new processor.  If you burn out the power regulator (a bit harder to do) you can brush up on your soldering and try replacing them.

Basically you can re-use any part that you salvaged.

You can reuse everything what is not damaged, but it can be tricky to properly desolder multi-pin SMD parts.
It's hard to verify if a processor is still working, but it propably is not anymore if you applied a too high voltage on one of the input pins or drew to much current.
Parts like regulators are always handy. I would skip capacitors and resistors.
But you can try to desolder everything, it will teach you how to use e.g. solder wick properly. This will come handy in the future, if you need to replace parts properly! :)

Good, complete answer. As you say, the processor itself is the thing that is most likely to be damaged/destroyed due to putting too much voltage into one of the pins or drawing too much current out of it.


text:
Connect one SD card to two devices
I have a system where an Arduino controls a DSLR camera for environmental monitoring. The Arduino can power on/off the camera and control the shutter (through the standard plug for a external shutter). The Arduino record and transmit the status of the system (battery voltaje, time of the last shot, etc.).
I want the Arduino to be able to access the camera's SD card while the camera is powered off. In order to check if the pictures were actually taken and check available memory (I don't need to write to the card).
So my questions are:

If I create a contraption to connect a single card to the camera and the arduino. The fact that the card is connected to one device (although not been used) will interfere in the operation of the other?
Should I consider the use of a multiplexer or something alike to completely disconnect the data lines from one device while the other is using the card?

An SD card has two interfaces: The SPI interface is what is used by the Arduino (it is rather slow). The second one is a parallel interface, which is capable of way bigger transmission speed. It is most likely, that the camera uses that interface, since it has to transmit rather big data packets in a short time. But as I remember some pins are used for both interfaces.
We cannot know, what happens inside the camera. But providing voltage to a digital pin of a microcontroller, while it is not powered, (as it would happen, when you drive the SD card while the camera is off) can be harmful. We must assume, that this would also happen with your camera, or you risk damaging it.
Using multiplexers (or - if you find no fitting one - some simple transistors) to disconnect the camera, when you want to connect with the Arduino, seems a good idea to me. Disconnect all lines, including the supply voltage. The ground line has to stay together, though you should check with a multimeter, if the SD slot ground is the same as the shutter connector ground (if they are not, you don't want to connect these grounds). Is most likely is, but you should check it to be sure.


text:
Power on, power off voice using fx board
I played with one of those Adafruit FX sound boards in a project a while ago, and see a lot of potential uses for it.
I would like to create “something” for my daughter, when she turns the lights on, (110v bedroom lights) a voice may say “good evening Kyndra” and when she turns the lights off the voice may say “have a good day...”.
The power “on” is not a big problem, the power off is my issue.... I could run a separate circuit and use a solid state off delay that when it sees first circuit go down it triggers a line of code.
With all the new stuff coming out all the time, just wondering if anyone else has a better idea to build a poor mans version “Jarvis 1.0”.  Perhaps using an Arduino may not even be the route I want.
This is just a thought... I’ve been tossing over for a while now.  It’s not imperative it ever gets built. Just thought it would be cool for her.

A sufficiently large capacitor may give you enough time to play the sound after loss of power if you do a good job of detecting the event.  Best to do this downstream of an off-the-shelf mains supply rather than trying to make your own.

Just have it run on independent power, and detect if the lights go on/off by measuring the light level.

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/


text:
External power vs usb power
I built a sample of my project on a breadboard using little 700mh leds and other components that easily ran off my computers usb. Now I’m building my project to full size.  This includes 12 7w leds, soundboard and amps. It’s still going to be controlled by a arduino mega, obviously, the power consumption is substantially higher than the usb or even the arduino can provide. I’m using a high amp power supply that can drive everything including the arduino.
The question is... how will I be able to alter my sketch later???  Is it feasible to use a panel mount usb cord modified to only allow tx and rx, (cut the red and black)?  Or will I have to isolate the mega each time I need to send a change to it?

700mh? Realize its not really important to the question...

You can modify a USB cable by cutting the power (red) wire. You'll still need the ground wire with RX and TX.

Using an altered USB cable and a separate power supply will work but isn't really necessary. I usually put a connector on my high-current devices like LEDs and whatnot so I can remove them from the equation while I upload code, and for debugging I leave the external power supply attached and use a regular USB cable to the PC (see What happens if I power the Arduino with both the USB and external power voltage simultaneously?).


text:
Help with clearing memory
I got the following code from Github, and have been playing with it for the last couple months.  The loop ALWAYS begins with the same LED as long as you do a complete reset to the board or power it up.  If I press the start button (I added) after running the loop previously, a different LED typically will fire.  I want to have the same led to start every loop, ALL the time (reset, power up, AND start button).
My current breadboard setup is an Arduino Mega, using a 4x4 matrix for the buttons and the LEDS to some digital outs.  I currently am using 6 LEDS but plan to expand to several more when I get everything working where I want it.
A quick rundown of how this functions...  At power up the LCD read out says Kyndras reaction timer.  If the start button is pressed a countdown begins and the first led lights up (LED 2), when the corresponding button is pressed another led lights until that corresponding button is pressed, etc.  The game runs for 30 seconds, at the end of the game your score is shown.  At that point you can simply press start to do it again.  My current issue is, I want LED "2" to be the first one to fire when the next game starts.  I know there is something retaining memory, because when you watch the last light power on a second before the timer ends, it lights the same light for a fraction of a second on the next game.
I suspect (old_butn = 0;) is my issue.  I have tried moving it all over the sketch and tried changing the digit after it, but I haven't had much luck.
I'm hoping someone can point me in the right direction.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Utility.h>
#include "tones.h" // file music, lies in the folder firmware

#define GAMETIME 30000
#define LAMP 6
#define RES_KEY 15
#define BUZZER_PIN 19
#define BLINK 40

LiquidCrystal_I2C lcd(0x3F, 20, 4);

const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
  {'<', '8', '4', '0'},
  {'=', '9', '5', '1'},
  {'>', ':', '6', '2'},
  {'?', ';', '7', '3'}
};

byte colPins[ROWS] = {39, 37, 35, 33};
byte rowPins[COLS] = {41, 43, 45, 47};
boolean lamp_on[6];
boolean steps = true, go_game = true;
int lamp_pin[15] = {2, 3, 4, 5, 6, 7};
unsigned long startTime [12] = { 0, 0, 0, 0 };
unsigned long duration [12] ;
const int buttonPin = 14;
const int startPin = 8;
long last_pressed = 0;
long nowMillis = 0;
long toSec = 0;
long gameStart = 0;
long toBlink = 0;
int score = 0;
int dlay = 1000;
int butn = 0;
int old_butn = 100;
char customKey;

int melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 };
int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 };

Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//=====================================
void(* resetFunc) (void) = 0;
//=====================================
void setup()
{
  randomSeed(analogRead(0));
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(startPin, OUTPUT);
  tone(BUZZER_PIN, 5000, 500);
  foreach (lamp_pin, LAMP, pinMode, OUTPUT);
  foreach (lamp_pin, LAMP, digitalWrite, HIGH);
  foreach (lamp_pin, LAMP, digitalWrite, LOW);
  delay(500);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(4, 0);
  lcd.print("ver 3.5");
  lcd.setCursor(0, 2);
  lcd.print("KYNDRAS REACTION");
  lcd.setCursor(6, 3);
  lcd.print("GAME");
}
//=====================================
void loop()
{
  //******* allows a reset at anytime within the loop
  customKey = customKeypad.getKey();
  if (customKey - 48 == RES_KEY) {
    go_game = true;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("RESTART!");
    delay(500);
    resetFunc();
  }
  //******* shit gets real
  if (digitalRead(startPin) == HIGH)
  {
    foreach (lamp_pin, LAMP, digitalWrite, LOW);//makes all leds go LOW after randomBlink

    countDown();
    //-------
    gameStart = millis();
    nowMillis = gameStart;
    score = 0;
    steps = true;
    go_game = true; //this line allows you to start w/o reset
    while ((nowMillis - gameStart < GAMETIME) and (go_game)) {
      nowMillis = millis();
      //**********
      if (nowMillis - toSec > 1000) {
        lcd_print();
        toSec = nowMillis;
      }
      //**********
      if (nowMillis - toBlink > BLINK) {
        if (lamp_on[old_butn]) {
          sw_led_off(old_butn);
        }
        else {
          sw_led_on(old_butn);
        }
        toBlink = nowMillis;
      }
      //**********
      if (steps) {
        while (old_butn == butn) {
          butn = random(LAMP);
        }
        sw_led_off(old_butn);
        old_butn = butn;
        sw_led_on(butn);
        steps = false;
      }
      //**********
      customKey = customKeypad.getKey();
      if (customKey) Serial.println((int)customKey);
      if (customKey - 48 == butn) {
        sw_led_off(customKey - 48);
        score++;
        steps = true;
        lcd_print();
      } else if (customKey - 48 == RES_KEY) {
        gameStart = millis();
        foreach (lamp_pin, LAMP, digitalWrite, LOW);
        score = 0;
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("RESTART!");
        delay(500);
        resetFunc();
      }
    }
    if (go_game) game_over(); // the game is over
    go_game = false;
      }
  //******** random blink

  for (int i = 0; i < 6; i++)
  {
    randomBlink(i);
  }
}

//=====================================
void lcd_print() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("TIME:");
  int time = GAMETIME / 1000 - (nowMillis - gameStart) / 1000;
  lcd.print(time);
  if (time < 11) {
    tone(BUZZER_PIN, 3500 + time * 300, 150);
  }
  lcd.setCursor(11, 0);
  lcd.print("BTN:");
  lcd.print(customKey);
  lcd.setCursor(0, 2);
  lcd.print("SCORE:");
  lcd.print(score);
}
//=====================================
void game_over() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("   END");
  lcd.setCursor(0, 2);
  lcd.print("     SCORE: ");
  lcd.print(score);
  foreach (lamp_pin, LAMP, digitalWrite, HIGH);//all lamps light for 2sec at end of game
  delay (2000);
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(BUZZER_PIN, melody[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(BUZZER_PIN);
  }
}
//=====================================
void sw_led_on(int num) {
  digitalWrite(lamp_pin[num], 1);
  lamp_on[num] = 1;
}
//=====================================
void sw_led_off(int num) {
  digitalWrite(lamp_pin[num], 0);
  lamp_on[num] = 0;
}
//=====================================
void randomBlink(int pin)
{
  if (millis() - startTime[pin] >= duration[pin])
  {
    digitalWrite(lamp_pin[pin], !digitalRead(lamp_pin[pin]));
    duration[pin] = random(150, 1500);  //smallest and longest ON/OFF times.
    startTime[pin] = millis();
  }
}
//=======================================
void countDown() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("   GET READY");
  delay(500);
  lcd.setCursor(0, 2);
  lcd.print("   START IN 3...");
  tone(BUZZER_PIN, 4000, 200);
  delay(500);
  lcd.setCursor(0, 2);
  lcd.print("   START IN 2...");
  tone(BUZZER_PIN, 4500, 200);
  delay(500);
  lcd.setCursor(0, 2);
  lcd.print("   START IN 1...");
  tone(BUZZER_PIN, 5000, 200);
  delay(500);
  lcd.clear();
  lcd.setCursor(0, 1);
  lcd.print("  GO!");
  delay(250);
  for (int i = 0; i < 5; i++) {
    int frequency = 4000 + i * 300;
    tone(BUZZER_PIN, frequency, 150);
    delay(130);
  }
}

Can you give a link to where you found it on Github? What is <Utility.h>? Is that the game library? Is the function foreach in that library? I'm not sure what you want and I don't know if that is possible.

look at all the commands that have anything to do with random numbers ... one of them may be the cause

The variables butn and old_butn are set to 0 and 100 before the game starts.  It's not clear to me what these value represents.  But re-assigning the variables back to 0 and 100 whenever the game restarts or resets should fix it.
Maybe at the end of the function game_over().  However it should probably also be done before the call to resetFunc() - whatever that does, its definition is not included.

Thankyou. I tried that with each one. You were right, both needed to be done.


text:
Using malloc() and free() for a stack/linkedlist
I know this question has been asked before, however it was asked two years ago and I have some other constraints to add.
(Previous question here) : Is using malloc() and free() a really bad idea on Arduino?
I understand that dynamically allocating and freeing memory can cause fragmentation, and without an operating system to manage the memory, this fragmentation can reach a point where no memory can be allocated because the current memory is a mess.
That being said, the only memory allocation I plan to do is for a linked list that I am using as a stack. This means that I will only even be adding or removing from one end of the linked list. In the case that the arduino malloc() implementation is 'next fit', is it safe to assume that I can use this linked list safely, since the only element I'll be removing is the next one that fit, and everything else will stay where it is?
tl;dr the two questions I'm asking:
Is the arduino malloc implementation 'next fit'? Where can I read up on this in detail?
AND
A single stack running in a 'next fit' environment will never cause fragmentation, correct?

In such a simple case there's no need for a malloc() style memory allocator, and no reason to suffer the overhead.  Just eternally reserve the entire memory you want and track usage with the pointer of your custom stack.  There's also no reason to make your list linked.

The stack will need to be of modular size. I could allocate a maximum size and expire stack entries, but that would require shifting the entire stack every time an entry is expired, I'm not sure but I would assume that's a fairly large overhead.

You only have so much memory available on the chip.  You can grab all that you'd be willing to let a malloc() try to use, and track varying partial usage of it yourself with a single number.  No "shifting" of anything involved.  Or else there's something you're not telling us.  Take a minute and think about how an actual stack works - does that match what you are doing?  If not, don't call it a stack.

Linked list and stack are two different data structures and it doesn't really make sense to use a linked list to implement a stack, a LIFO queue that is accessed from one end only. I'm not saying that you couldn't do it I'm just saying that from a performance and code stand point it doesn't make much sense.

I'm voting to close this question as unclear, since the asker has failed to specify what they are really trying to acomplish by using a linked list of dynamically allocated elements to emulate a stack.  This smells strongly like an XY problem.

On my answer to the question you are referencing, I state that “If
you use the heap like a stack (last in is first out), then it will
behave like a stack and not fragment.” This seems to be exactly your
usage pattern, thus you should not fear fragmentation. Of course, the
caveat written in Michel Keijzers’ answer applies: you should make sure
that no other part of your code, including libraries, is using
malloc() and free().
Just to give some evidence, the avr-lic documentation provides some
implementation details about malloc:

When allocating memory [...] If nothing could be found on the
  freelist, heap extension is attempted. [...] When deallocating the
  topmost chunk of memory, the size of the heap is reduced.

All this can be seen in action in the source code of malloc() and
free(), which is abundantly commented and quite readable.

Edit 1: From the same section of the avr-libc documentation, we learn
about how realloc() grows a memory chunk:

If [...] the old chunk is at the top of heap, and the above freelist
  walk did not reveal a large enough chunk on the freelist to satisfy
  the new request, an attempt is made to quickly extend this topmost
  chunk (and thus the heap), so no need arises to copy over the existing
  data.

Based on this, I would suggest you use an array, rather than a linked
list, and use realloc() to adjust the size of the array. A linked list
of n objects of size m has a memory cost n × (m+4):
each objects has an overhead of 2 bytes for the linking pointer, plus 2
bytes for malloc()'s internal bookkeeping. The cost of an array would
be only (n × m)+2.
In this case, however, it is essential that no other part of your code
uses malloc(), otherwise realloc() could end up allocating a whole
new array, which would be very costly.

Edit 2: I wrote a small test program that randomly grows and shrinks
a memory buffer. Running the test on an Uno shows that the allocated
memory chunk never changes address even though its size varies wildly.
void setup()
{
    Serial.begin(9600);
}

void loop()
{
    static void *p;
    static size_t sz;
    sz = random(max(sz, 22) - 20, min(sz + 20, 1536) + 1);
    p = realloc(p, sz);
    Serial.print((uintptr_t) p);
    Serial.print("  ");
    Serial.println(sz);
}

You really, really, really don't want to use realloc() for something of substantial size.  In the case where it's safely exclusive, it's unnecessary, and in the case where it's unsafe it fails horribly since the chip likely can't fit a new copy of the whole thing.  This really does not seem to be a situation where dynamic allocation is called for, but if it's going to be used and the allocation grown then a linked list should be used so that it doesn't have to be contiguous.  Otherwise it should be a statically allocated array deterministically reserving the maximum needed size.

Probably it will not be a problem, since when you free memory, those memory sizes will be equal than the memory size you malloc. E.g. suppose one element of a linked list is 24 bytes, if this is freed, the next time when 24 bytes are requested by malloc, it will use that space.
So my assumption it works when you never use free or malloc, EXCEPT when using it for one single linked list with elements that are always equal size.
However, you have to make sure:

You do not free memory elsewhere (with different size)
You do not malloc memory elsewhere (with different size)
All libraries you use do not call a malloc/free under water.

Also, you can do an easy check by malloc/free and fill one item a lot of times one element (thus adding and removing it), and checking the memory usage is similar (or it does not crash to have a very simple test).
Btw, afaik Arduino pointers use 2 bytes, so this can cause quite some memory. If you use max. 256 elements, consider using indices, but in this case you have to write your own 'linked list' library (e.g. when removing, you have to move all subsequent indices).

That's a good point about the libraries, and I'll keep that in mind as I develop.

I haven't checked Arduino libraries, but I assume these do not use malloc/free, but in your case it might be important.

The standard Arduino String class uses realloc().

There are many implementations of malloc() and AFAIK, none is specified in the standard. So, while you could get lucky and and find that your preferred environment uses a malloc() algorithm compatible with your intended use, your code will not be known portable to another, unspecified, environment, and even a compiler or library revision to your preferred environment could break you code.
You'd be better off to write an allocator that works as you need it to work. Whether you need a linked-list or an array of fixed size blocks to accomplish your stack structure, using a getStackFrame(), freeStackFrame() pair of functions, written and controlled by you, will avoid some potential nasty surprises.

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/


text:
Setting a number inside a struct causes timer to misbehave
The following code:
struct l{
    uint8_t pin;
    uint8_t sensePin;
    float brightnessTarget;
    uint8_t PWMvalue;
    uint8_t inc;
};
typedef struct l led;

led* redLed;
led* whiteLed;
unsigned long secondsOfDay = 79140;
unsigned long minutesOfDay = 0;
unsigned int hoursOfDay = 0;
uint8_t interrupted=0;
uint8_t runPID;
int pin13=0;

void setup() {
  cli();
  TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
  TCNT1  = 0;//initialize counter value to 0
  // set compare match register for 1hz increments
  OCR1A = OCR1A_val;// = (16*10^6) / (1*1024) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS10 and CS12 bits for 1024 prescaler
  TCCR1B |= (1 << CS12) | (1 << CS10);
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);
  pinMode(13,OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  sei();
}

void loop() {
  if (runPID){
    PIControl(redLed);
    PIControl(whiteLed);
    pin13 = (~pin13 & 1);
    digitalWrite(13, pin13);
    runPID=0;
  }
}

SIGNAL(TIMER1_COMPA_vect){ //1 second timer fire
  runPID = 1;
}

Toggles a pin. However, if I add:
redLed->pin = 3;
whiteLed->pin = 4;
redLed->sensePin = 0;
whiteLed->sensePin = 1;

In the setup anywhere (before cli(), after sei(), or within the block), the interrupt no longer toggles.
I'm absolutely baffled, and I have no idea what's going on.
Edit: everything but whiteLed->sensePin is fine. That line is what is causing the issue (and I still don't understand)

This:
led* redLed;

defines a global variable implicitly initialized to zero (i.e. a NULL
pointer). This is an invalid pointer you are not allowed to use until
you properly initialize it.
Here:
redLed->pin = 3;

you are writing to the memory pointed to by this invalid pointer. This
invokes what the C and C++ standards call undefined behavior. What
that means is that, according to the definition of the C++ language, the
behavior of your program is not defined. In other words, anything can
happen. Typically you get a program crash, but even that behavior is not
guaranteed.
Do not ever access memory through an invalid pointer.

Ugh. I knew it was a dumb oversight. Thank you!


text:
What is the lowest resistance pot that is useful with an analog input?
Background
I've dabbled in electronics since my parents gave me a 150-in-1 kit in the 1970's but never had formal training. My dad repaired Honeywell mainframes so taught me to solder when I was 8. I have a decent set of equipment like irons, parts, meters, etc. and realize I know enough to be dangerous, which is why I avoid mains-powered stuff.  I'm not a newbie but am all too aware of the many holes in my knowledge.  I'm doing this for fun and in a way that allows for failures to be expected, cheap, and educational.
Project
I have a WS2812b LED strip connected to an Arduino Uno and am using various animation libraries to drive it. The goal is to move to a pro mini once the hardware is more settled and use it for creative lighting inside my house or garage.  I started with just the strip and let it display different animations in a sequence based on time but then wanted to attach a button which would advance to the next animation.  After implementing debouncing it worked fine and I moved on to adding a potentiometer to select the speed of the current animation; increasing or decreasing the delay between each 'frame'.  Of interest here is I was using old pots (panel mount not for typical board use - hence I had to solder wires to them just to use them in any configuration) and not doing any ohms law calculations, just hooking up the hardware and tuning the values for mapping the detected voltage to the desired delay by checking printed output from the serial monitor.
I'm happy with the general setup when using a breadboard but when I went to solder something more permanent on perfboard I ran into a problem; the speed control wouldn't work.  I'm sure the first pot I used was 10k but when I checked it with the meter it registered as completely open so I figured I had blown it. Thinking that I needed to control the current I read posts like this one and this one about limiting current I felt better and proceeded to replace the pot with a 25 ohm version thinking that the only difference would be in calibrating the values for mapping the detected voltage to the desired delay.  The behavior I got was randomly fluctuating values for the delay and when I hooked up the prototype using USB to my laptop I could see that the analogread() values were fluctuating wildly within a range even when I wasn't manipulating the pot yet nothing I did could get it to be consistent.  My first thought was that there was a problem with ground so I rechecked the connections but they all tested fine, including still reading 25 ohms along the outside terminals and varying values on the wiper consistent with rotating it along its path from end to end.  When I gave up on my protoboard and returned the circuit back to the breadboard I replaced the full size pot with a 10k small one meant for breadboards and the speed control works again.  This leads to my question.
Question
Is there a minimum value for a potentiometer connected to an analog input of an arduino, or is it more likely that there was an issue with my soldering job on the protoboard?  I don't have a picture of the board because I've already desoldered components from it but I can tell you that it was my first attempt at perfboard soldering and it would not win any prizes, sort of the 'face only a mother could love' sort of thing. If it matters, I was using analog input A2.

if you have all 3 legs connected (vcc, gnd, a2) anything above 100r should work within the current limits of the MCU, though 10k is probably ideal in terms of accuracy and low current draw. 5v into 25r is 200ma; 10X what the MCU wants to provide.

Assuming you wired it correctly (ie. you put the wiper to the analog input) your first setup looked like this:

Your big problem here is the current flow through the pot. Using Ohms Law the current would be:
I = V / R
I = 5 / 25
I = 0.2A  (200 mA)

That is too much to be drawing from the 5V pin and it could easily drag down the voltage to the rest of the board. Also the power would be:
W = I * V
W = 0.2 * 5
W = 1W

One watt is a lot to put through a pot, and you may well have burned the track inside it.

So, if I included a 10k resistor on either side of the pot that should address the current issue but it leaves the question of whether the remaining 25-ohm range is too small for the ADC to register distinct values, ...

The 10k resistor will stop the current problem, but now the range in the voltage divider will be ridiculously small. The setup will now look like this:

Unfortunately the range you are going to read with the analog input is going to be (voltage divider formula):
Vout = Vin * (R2 / (R1 + R2))

With the wiper at one end R1 will be 10000 and R2 will be 10025 and with the wiper at the other end it will be vice-versa. So the voltage you get will be:
Vout = 5 * (10000 / (10025 + 10000))
Vout = 5 * 0.499
Vout = 2.497

Or:
Vout = 5 * (10025 / (10025 + 10000))
Vout = 5 * 0.501
Vout = 2.503

In other words you will always be reading about 2.5V and therefore your analog read will always return around 512, which won't be much use to you.

I'm going with a larger value pot (10k or 25k) ...

That sounds like a sensible value. A 10k pot will only draw:
I = V / R
I = 5 / 10000
I = 0.5 mA

That is a reasonable current draw and the analog input will still read it fine.

As Majenko points out in his answer you don't want a whole lot more than that resistance because of the optimal input impedance of the ADC converter (10k). A very high resistance would mean only a small current would flow into the analog input pin.

How to use your 25 ohm pots
If you want to use your existing 25 ohms pots because of their physical appearance, then a  small resistor in series could reduce the current to a reasonable amount and still give a good reading range. For example, 330 ohms on the "high" side (and nothing between the low side and ground).
Vout = 5 * (330 / (355 + 330))
Vout = 5 * 0.193
Vout = 0.963

Or:
Vout = 5 * (355 / (355 + 330))
Vout = 5 * 0.581
Vout = 2.591

That is now going to give you a reading between 0.96V and 2.59V which, whilst not the full range of 0V to 5V should give you reasonable differentiation as you turn the pot. And the current through it would be:
I = V / R
I = 5 / 355
I = 0.014A (14 mA)

Thus the power would be:
W = I * V
W = 0.014 * 5
W = 0.070 (70 mW)

References

Voltage Dividers tutorial
Ohms Law

That is a such a good and thorough answer @Nick Gammon. Thanks for taking the time!

Most potentiometers take the form of a 3-terminal device, where the two outside terminals are a fixed resistor and the middle terminal adjusts the tap from one extreme to the other.

simulate this circuit – Schematic created using CircuitLab
In this configuration, you end up creating a simple voltage divider. You should pick the potentiometer's voltage such that the fixed portion does not draw more current than necessary, regardless of the tap position. (The arduino's analog input itself does not draw any significant current really.)
If you use a 1k pot, then the current always flowing from 5V to ground is 5mA, which should not tax the Arduino's voltage regulator too much. If you used a 100 ohm pot, then the current would be 50mA -- not likely to damage things, but certainly more than is necessary just to operate a voltage divider.
If you use a 1M-ohm pot, then you are working with 5 micro-Amps, and at that low current, noise from the power supply starts to interfere with your analog readings.
As for your actual setup, it really depends on how you had it wired, etc.

There is no lower limit on the value of a potentiometer that you can use with the Arduino, however the lower the resistance the more current it will pull from the power supply and, if it's too low a resistance and isn't rated for a high enough power, could burn out and catch on fire.
There is, however, an upper limit on the resistance of a potentiometer that you can use.
As a general rule of thumb the output impedance of what you connect to an ADC should be no more than around a tenth of the input impedance of the ADC.  On the Arduino the ADC has around a 1MΩ input impedance, so you shouldn't connect anything with more than around 100kΩ output impedance.
For a potentiometer (well, for any voltage divider) the output impedance is the value of the two halves of the resistance in parallel.  For a 10K potentiometer with the wiper set right in the middle that's 5K for each half, so the formula is:
     R1 * R2   5000 * 5000
Ro = ------- = ----------- = 2500Ω
     R1 + R2   5000 + 5000

As you can imagine that's the maximum output impedance at that point. For example, if you adjust it so it's 9000Ω and 1000Ω it works out as:
     R1 * R2   1000 * 9000
Ro = ------- = ----------- = 900Ω
     R1 + R2   9000 + 9000

So from that you can interpolate that the maximum value potentiometer you can reliably use would have an output impedance of 100kΩ, which is (100000/2500) 40 times more than the 10kΩ one, so 400kΩ is really the maximum you can use.
The main reason for this limit is that the ADC's front-end is a capacitor, and that capacitor has to charge up to the voltage of the input. That charge time is limited by the current available to flow into the pin, and with too high an output impedance the current available is too low and the capacitor can't charge to the right voltage in the limited acquisition time of the ADC.
The datasheet itself speaks of the output impedance limit:

The ADC is optimized for analog signals with an output impedance of approximately 10 kΩ or less. If such a source is used, the sampling time will be negligible.

So they don't recommend anything more than 10kΩ, which equates to a maximum of 40kΩ for the potentiometer's resistance.

The Analog Digital Converter (ADC aka analog input) has only a small capacitance, which has to be loaded to do a measurement. This is normally no problem, even with high resistance potentiometers. Also - if you are using the potentiometer correctly - it is very unlikely, that you have blown it (with 5V supply voltage there is only a current flow of 0.5mA through a 10k potentiometer). The current that flows in the ADC is also very small.
The point in using a potentiometer for providing analog input is, that the total resitance is always the same, regardless of the position. See this schematic:

simulate this circuit – Schematic created using CircuitLab
Both voltage dividers have the same total resistance (10kOhm). If you set the position of the potentiometer to the middle, they have exactly the same output: 2.5V. Moving the slider of the potentiometer is the same as taking some of the resistance from one of the resistors on the right and adding it to the other. So a potentiometer is a variable voltage divider. Normally you want the current, that flows through this voltage divider to be small, because it would be only wasted to heat the resistor strip. If you are using a potentiometer with 25 Ohm total resistance while having a 5V power supply, the current trough it would be 200mA, which is pretty much. And if your power supply cannot give you that much, your application will get really instable.
So I see 2 possible problems:

You used the potentiometer the wrong way, or
You made a soldering mistake

EDIT:
From your comment I read, that you are not familiar enough with the rules, how a voltage divider really works. So I will try to explain a bit more.
A potentiometer looks like this on the inside. It consists of one strip of a resistive material. Each side of the strip is connected to one of the outer pins. The full length of the strip has a constant resistance, say 10kOhm for now. The knob of the potentiometer is connected to a contact, that slides over this resistive strip, making electrical contact with it. When you measure the resistance between one of the outer pins and this middle contact, you get a resistance that is dependent on the position of the contact.
Let's say, we want to measure the resistance between the left contact and the middle contact (the slider). If the slider is very close to the left pin, the resistance we measure is very small, since there is only very few resistive material between the contacts. As we move farer away from the left side of the strip, the measured resistance gets bigger.
At the same time (when we are moving away from the left side of the strip) we are moving towards the right side of the strip, essentially decreasing the resistance between the middle contact and the right side of the strip.
Now we have to do some maths: Ohm's Law states that voltage is resistance multiplied by current
U = R * I

We connect the outer pins of the potentiometer to our 5V power supply and to ground. So U = 5V. And we know the total resistance between these pins: 10 kOhm. To get the current, that flows through our potentiometer, we reform the above formula to
I = U / R

Using the above values we calculate the current through the potentiometer to be 0.5mA. The current through the first part of the resistor strip (say from left to the middle contact) is the same as in the second part (from middle contact to right), as the current has nowhere else to go.
Now we position the middle contact exactly in the middle of the resistive strip. This means that you have half of the total resistance on each side: 5 kOhm. In electrical circuits the voltage is always measured in reference to ground. So here only the ground side matters for the rest of the calculation. We again need Ohm's Law:
U = R * I = 5kOhm * 0.5mA = 2.5V

As we want to read this with an Arduino (which uses a 10 bit ADC --> values in the range of 0 to 1023) we get about 512. (This may vary a bit because of fluctuations and distortion)
When we position the middle contact at the ground end of the resistive strip, we have a ground-side resistance of about 0 Ohm. So we get 0V at the middle pin and a ADC value of about 0.
When we position the middle contact at the 5V end of the resistive strip, we have a ground-side resistance of about 10 kOhm. Using Ohm's Law we get 5V at the middle pin, which translates to an ADC value of about 1023.
Changing the position also changes the ratio of the resistance on ground-side and 5V-side of the strip. But when you (as said in your comment) just add big resistors to both sides of the 25 Ohm potentiometer, you will only we able to control this really small part of the total resistance. So this will not work.
Note: Not every potentiometer has a linear changing resistive strip. Some have a logarithmic or exponential changing strip.

So, if I included a 10k resistor on either side of the pot that should address the current issue but it leaves the question of whether the remaining 25-ohm range is too small for the ADC to register distinct values, or is the ADC still sensitive enough?  I'm going with a larger value pot (10k or 25k) anyway but I'm still interested in understanding what happened when I used the 25r one.


text:
USB power on arduino digitalRead is normal, but, gives error when using buck(step down) converter
I am using arduino for digitalRead(). i've been using INPUT_PULLUP, and when using USB power, all seems normal. but, when i switched to  power supply and buck converter(12v to 5v), the digitalRead() randomly change. why is that?

Impossible to say without more details. Provide a circuit diagram including the models of components, info about where you feed the input power, and the code for a minimal application that exhibits the behavior.

Is the 12V to 5V output being applied to the barrel jack connector? That is too low a level for the onboard 5V regulator to work from. Connect that 5V to the 5V pin instead.

If you read the docs they advise against feeding 5V into the 5V pin. What I've done is to cut a USB cable and feed my regulated 5V into that. That way you get the benefits of the on-board power source selection and protection circuits. (The Arduino intelligently manages choosing between USB power and barrel connector power. If you feed 5V into the 5V pin directly then you have to remember to disconnect that before plugging in a USB cable to program the Arduino.)


text:
FlexyStepper: set current position leads to reach the old target
I'm using the FlexyStepper library.Unfortunately there's no way to write an issue to the author so I'm going to ask my question here.
The documentation says about the setCurrentPosition*() functions:

set current position of the motor in millimeter, this does not move the motor

But I see a different behavior.
Looking at the code, it seems it just update the current position, leaving unchanged the target position. This leads to an expected movement of the motor. Example:

set the target position to, say: 1000
the motor reach that position
set the current position to, say: 1500
now the motor come back to 1000

Instead, I want the current position from now is 1500, but because it has already reached the target, it shouldn't move further.
Of course a workaround is to set both current position and target to the same value.
I wonder if I'm wrong about the documentation or this might be considered a bug.

Use the workaround--stepper1.setSpeedInRevolutionsPerSecond(0); setCurrentPositionInSteps(1500); setTargetPositionInSteps(1500);

The point of setCurrentPosition() is to calibrate the stepper. It would normally be used in association with a limit switch of some form.
If you set the motor to go to 1000 and then tell it that the motor is actually at 1500 then of course it will then try and go back to 1000 - you have told it that is where you want it, but you also told it that's not where it is.
The normal usage would be:

Step the motor constantly in one direction until it hits a limit switch or similar
Set the current position to whatever that actual position is
Start going to actual positions that are relative to that reference point.

For example

Move the motor anti-clockwise
Motor triggers limit switch
Stop moving the motor
Set the current position to 0
Start using the motor normally

Thanks, but not all projects use motors in such a "normal" way. I need, sometimes, to set a different current position rather than the actual one. I've ended to set both target and current positions. It seems to work. The misleading sentence here is "does not move the motor" until a target was defined before.

What exactly are you trying to achieve by changing what the system thinks the position is all the time?

where did you read all the time? I said sometimes :-) I want to achieve exactly what it literally means: I need the system changes the references on-the-fly. It's a set up for theatrical effects that moves balloons and other stuff. There are no homing switchs, etc...


text:
Refreshing a web-page hangs after few times
I'm using a Nucleo F429ZI board and I wrote a sketch that enables a simple web-server:
#include <LwIP.h>
#include <STM32Ethernet.h>

const int WEB_PORT = 80;
EthernetServer server(WEB_PORT);
char buffer[1024];

void setup()
{
    Serial.begin(115200);
    // define variables
    Ethernet.begin(localIP, netmask, dns, gateway);
    server.begin();
}

void loop()
{
    processServer();
}

void processServer()
{
  String readString;
  EthernetClient client = server.available();
  if (client)
  {
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        readString += c;

        if (c == '\n')
        {
          if (readString.startsWith("GET"))
          {
            readString.remove(0, 4);
            int pos = readString.indexOf(' ');
            String path = readString.substring(0, pos);

            if (path == "/status") pageStatus(client);
            break;
          }
        }
      }
    }
    delay(1);
    client.stop();
  }
}

void pageStatus(EthernetClient client)
{
    sectionHeader(client, 2);
    client.println("");
    client.println("");
    client.println("
");
    client.println("
Servo	Position	Running	Initialized
");

    for (int i = 0; i < getParamUInt8(P_SERVO_SIZE); i++)
    {
        sprintf(buffer, "
%u	%d	%s	%s
", i + 1, (int16_t) _servo[i].current, _servo[i].ended ? "NO" : "YES", _servo[i].initialized ? "YES" : "NO" );
        client.println(buffer);
    }
    client.println("
");

    client.println("

");

    client.println("
");
    client.println("
Led	PWM	Running
");

    for (int i = 0; i < getParamUInt8(P_LED_SIZE); i++)
    {
        sprintf(buffer, "
%u	%d	%s
", i + 1, (int16_t) _led[i].pwm, _led[i].ended ? "NO" : "YES");
        client.println(buffer);
    }
    client.println("
");

    client.println("

");

    client.println("
");
    client.println("
Stepper	Position	Running
");

    for (int i = 0; i < getParamUInt8(P_STEPPER_SIZE); i++)
    {
        sprintf(buffer, "
%u	%d	%s
", i + 1, (int16_t) _stepper[i].stepper.getCurrentPositionInMillimeters(), _stepper[i].stepper.motionComplete() ? "NO" : "YES");
        client.println(buffer);
    }
    client.println("
");
    client.println("");
}

void sectionHeader(EthernetClient client, int refresh)
{
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("Connection: close");
    if (refresh) client.println("Refresh: 2");
    client.println();
    client.println("<!DOCTYPE HTML>");
}

As you can see in the sectionHeader() I want to refresh the page every 2 seconds. It works, but after 3 or 4 refreshes it hangs and I have to reset the whole board.
Of course with an embedded system I would use Ajax... here I thought reloading the page should be enough.
What might cause such a hang?

I recommend https://www.stm32duino.com

Welcome to the shockingly bad world of the String object. Ditch it as a first priority.

@Majenko, I used the String object only to parse the incoming GET request. Do you think it's worth to remove it, about my problem?

You may want to look at the HTTP conversation with Wireshark. 2. What does your server do when the client requests "GET /favicon.ico"? 3. What if it instead requests "HEAD /favicon.ico"?

@EdgarBonet, it just doesn't answer. Is this a bad behavior? Should I provide an answer anyway?

The code you have that says readString += c has to allocate a whole new string whenever it runs out of space for more characters. All those allocations are filling up memory.
Instead of the String class, use a single C character array, e.g.
char s[128];
that is big enough for whatever you put in it.
If it helps, I wrote a simple arduino web server for my own project, and if you look at the code here that may give you ideas for how to implement this.

Or just set readString = "" after processing the request, I guess.


text:
Latest news about alternative Arduino IDEs
I simply hate the default Arduino "IDE".
On Windows I use Visual Studio Code with the related plugin.
Now I have a Ubuntu machine and I'm looking for the definitive IDE.
Questions:

I'm not sure whether to use VSC I need to install the default IDE as well
is there something better?

By the way, I also have QtCreator, PHPStorm, MPLAB X, STM32Cube (Eclipse based).

I use Eclipse Sloeber. It is an Eclipse plugin (or product - customized Eclipse). It adds Arduino toolchain to Eclipse C/C++ development tools. https://eclipse.baeyens.it/

I have my own that I release for people to use if they want. UECIDE.

@Juraj, FYI I tried Sloeber. It seems a great project but it has too may bugs for me. After two days I still not able to compile a project. Perhaps in the future it might be a suitable option!

it works for me. the future of Sloeber is now. it is not actively developed anymore

Under Ubuntu 19.10 is unusable. And if it's not developed anymore it's another reason to avoid it :-)

I use the Arduino IDE to 'build' everything.
However, in case of a bigger project, I use a 'decent' code editor (Visual Studio in my case) to edit, and the Arduino IDE to build.
Also, because I prefer testing all non-Arduino related code on the PC (not on the Arduino), the Visual Studio (C++) project uses some stub classes I created (specifically for my project), and I can run the application on my PC.
When some part is finished, I can build the same code in Arduino IDE, but without the stubs (because these are acquired from Arduino.h and related Arduino files).
What IDE is best to 'edit' code is very subjective I think. But in the above way I can see many classes/files at a glance on the left side (and not in the top like Arduino IDE where only like 5 tabs fit on a screen), and to test lots of code on the PC already before sending it to an Arduino.
The GitHub repository can be found here: https://github.com/michelkeijzers/Mestra
Project inside is DmxLightShow.

Unfortunately, for my specific needs, I do the opposite :) I have to test the code on the target. For this reason I'm looking for a complete IDE, where I can manage libraries/boards, download software etc... like in the default one.

@Mark Even in that case you can make two projects: one on your favorite editor, where you can open multiple files and store them in different folders, and use Arduino IDE to build.

Arduino creators toughed of you.
Personally I use Geany, but you can use eclipse, but I find the Arduino plugin rather hostile, especially when it comes to editing default libraries (to add printf) and pointlessly complex serial monitor settings default above the NANO's range.
Although its missing testing feature without upload
Although most other editors would be quite happy with arduinos file too, but  you have to always create new project with native IDE.
You will find use external editor feature in Arduino-ide's settings in which case every time file is saved it gets immediately updated in Arduino's IDE, so you can use it for your tasting as a sub-window.


text:
IDE finds multiple libraries
I don't understand Arudino IDE. Ever.
This is a part of the compilation output:
Alternatives for ArduinoJson.h: [ArduinoJson@6.14.0]
ResolveLibrary(ArduinoJson.h)
  -> candidates: [ArduinoJson@6.14.0]
/home/mark/.arduino15/packages/STM32/tools/xpack-arm-none-eabi-gcc/9.2.1-1.1/bin/arm-none-eabi-g++ -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb @/tmp/arduino_build_952252/sketch/build_opt.h -c -Os -w -std=gnu++14 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -I/home/mark/OSC -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino/avr -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino/stm32 -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino/stm32/LL -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino/stm32/usb -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino/stm32/usb/hid -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino/stm32/usb/cdc -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/Drivers/STM32F4xx_HAL_Driver/Inc -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/Drivers/STM32F4xx_HAL_Driver/Src -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/STM32F4xx -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src -w -x c++ -E -CC -DSTM32F4xx -DARDUINO=10810 -DARDUINO_NUCLEO_F429ZI -DARDUINO_ARCH_STM32 "-DBOARD_NAME=\"NUCLEO_F429ZI\"" -DSTM32F429xx -DHAL_UART_MODULE_ENABLED -I/home/mark/.arduino15/packages/STM32/tools/CMSIS/5.5.1/CMSIS/Core/Include/ -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/Drivers/CMSIS/Device/ST/STM32F4xx/Include/ -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/gcc/ -I/home/mark/.arduino15/packages/STM32/tools/CMSIS/5.5.1/CMSIS/DSP/Include -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/variants/NUCLEO_F429ZI -I/home/mark/Arduino/libraries/STM32duino_LwIP/src -I/home/mark/Arduino/libraries/STM32duino_STM32Ethernet/src -I/home/mark/Arduino/libraries/ArduinoJson/src /tmp/arduino_build_952252/sketch/OSC.ino.cpp -o /dev/null
Alternatives for FlexyStepper.h: [FlexyStepper@1.0.0]
ResolveLibrary(FlexyStepper.h)
  -> candidates: [FlexyStepper@1.0.0]
/home/mark/.arduino15/packages/STM32/tools/xpack-arm-none-eabi-gcc/9.2.1-1.1/bin/arm-none-eabi-g++ -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb @/tmp/arduino_build_952252/sketch/build_opt.h -c -Os -w -std=gnu++14 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -I/home/mark/OSC -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino/avr -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino/stm32 -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino/stm32/LL -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino/stm32/usb -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino/stm32/usb/hid -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino/stm32/usb/cdc -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/Drivers/STM32F4xx_HAL_Driver/Inc -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/Drivers/STM32F4xx_HAL_Driver/Src -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/STM32F4xx -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src -w -x c++ -E -CC -DSTM32F4xx -DARDUINO=10810 -DARDUINO_NUCLEO_F429ZI -DARDUINO_ARCH_STM32 "-DBOARD_NAME=\"NUCLEO_F429ZI\"" -DSTM32F429xx -DHAL_UART_MODULE_ENABLED -I/home/mark/.arduino15/packages/STM32/tools/CMSIS/5.5.1/CMSIS/Core/Include/ -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/Drivers/CMSIS/Device/ST/STM32F4xx/Include/ -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/system/Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/gcc/ -I/home/mark/.arduino15/packages/STM32/tools/CMSIS/5.5.1/CMSIS/DSP/Include -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/cores/arduino -I/home/mark/.arduino15/packages/STM32/hardware/stm32/1.8.0/variants/NUCLEO_F429ZI -I/home/mark/Arduino/libraries/STM32duino_LwIP/src -I/home/mark/Arduino/libraries/STM32duino_STM32Ethernet/src -I/home/mark/Arduino/libraries/ArduinoJson/src -I/home/mark/Arduino/libraries/FlexyStepper/src /tmp/arduino_build_952252/sketch/OSC.ino.cpp -o /dev/null
Alternatives for Tasker.h: [Tasker@2.0]
ResolveLibrary(Tasker.h)
  -> candidates: [Tasker@2.0]

Più di una libreria trovata per "ArduinoJson.h"
Usata: /home/mark/Arduino/libraries/ArduinoJson
Più di una libreria trovata per "FlexyStepper.h"
Usata: /home/mark/Arduino/libraries/FlexyStepper
Più di una libreria trovata per "Tasker.h"
Usata: /home/mark/Arduino/libraries/Tasker

Più di una libreria trovata in English means more than one library found.
And Usata means Used.
It's telling me it finds multiple libraries and selects one among them. Cool. But why it doesn't tell me where are the others?
Ok, I do:
$ sudo find / -name Tasker.h
/home/mark/Arduino/libraries/Tasker/Tasker.h

the same happens with the others.
So, where are the multiples libraries it finds?

it is a known bug of Arduino IDE 1.8.10. ignore it. you don't have multiple libraries if it only lists one. https://github.com/arduino/Arduino/issues/9601

There are 3 locations for your libraries. Arduino IDE checks on the Arduino IDE (installation path) library folder. On your sketch folder. And finally in your board folder. As you can see in your log, you have a custom board STM32 installed, and the IDE is taking the library located in .arduino15, not the one located in Arduino. Tutto chiaro?

When you install your Arduino, 3 locations are used for your libraries:

Installation folder
Personal folder
Boards folder

Installation folder
This is the location where you installed Arduino. Normally they are:
On Windows: %ProgramFilesDir%\Arduino\libraries
On Linux: /home/{USERNAME}/arduino-{VER}/libraries
There, you can find 5-10 base libraries.
Personal folder
Since, on each update, the installation folder is deleted, the sketches and custom libraries are stored in your personal data folder:
On Windows: %userprofile%\documents\Arduino\libraries
On Linux: /home/{USERNAME}/Arduino/libraries/
There, you can find all libraries that you downloaded from the IDE.
Boards folder
Some libraries comes with your board and are board optimized. Arduino IDE will always check this folder, before it search for other libraries.
On Windows: %LOCALAPPDATA%\Arduino15\packages\{BOARD}\hardware\{MODEL}\{VERSION}\libraries
On Linux: /home/{USERNAME}/.arduino15/packages/{BOARD}/hardware/{MODEL}/{VERSION}/libraries

As I can see, you have a custom board selected. So the Arduino IDE search on the boards folder. If it can find a library it will take this one. If not, it will search on your personal folder and finally in the installation folder. I am not sure, but it could also get the newer one, if there is a version on it. Can't say how the right library is selected by the IDE.
If more than 1 are found, a warning message will appear. But it is just a message, you can just check if the right library was taken.

I know where the libraries are located, but it's not the point here. As you can see I searched from the root, so there are no more than one library. It's a bug, as Juraij said.

You're right. sorry... I answered without read it completly.


text:
LiquidCrystal issue with HD44780 display
Here my sample code for an Arduino Leonardo-based custom board:
#include <LiquidCrystal.h>

const int rs = 3, en = 0, d4 = 4, d5 = 30, d6 = 12, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup()
{
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);

  Serial.begin(115200);

  lcd.begin(16, 2);
  lcd.print("Hello world");
}

void loop()
{
}

And here my schematic:

The MCU is an ATmega32U4.
Running the code above the display is completely empty, I also tried to change the values of R4 and R5 but nothing changes. I don't see anything, not even the background of each character.
The display is a common HD44780 compatible one.
I retrieved the pin numbers from here:

/opt/arduino-1.8.19/hardware/arduino/avr/variants/leonardo/pins_arduino.h

Am I missing anything?

R4/5 should be a potentiometer not fixed resistors.

is the backlight lit? .... please post pictures of front and back of the LCD module

The schematic pin assignments do not match the Arduino Pin definitions you used in your code. Please see Arduino PinMapping32u4 or read the correct pin mapping for Leonardo from the source code.

@hcheung gosh, I used exactly those two documents to retrieve the pin assignment! Would you please tell me what is wrong? PD0=3, PD2=0, PD4=4, PD5=30, PD6=12, PD7=6. This is what I read from the header file.

@hcheung I confirm the assignments are correct! There was only a hardware problem as "the busybee" pointed out

Sorry, my bad, I didn't realise that your d5 is means PD5 and 30 means D30.

The error is, that V0 has VDD level by the way you connected the resistors. The "lower end" of your ladder needs to be at GND, pin 1.
The pin 15 (A) is the anode of the backlight, and is not connected to the LCD driver circuit. BTW, to lit the backlight, you need to connect pin 16 (K), too. See the data sheet for details.
Use a potentiometer for V0, wiper to V0 and the ends to VDD and GND, respectively.
Just a side note: From the back of my memory, V0 can be commonly connected by a single resistor or directly to GND, since the wiper of the potentiometer was mostly at the GND end. It will give some contrast, depending on the temperature.

I'm trying right now and I'll write back soon. I assumed Vee was the negative rail, my fault. I don't want to put a pot otherwise I need to tune each board. I'm going to experiment and find the correct resistors ratio at room temperature.

It works. With Vo = GND the contrast is perfect.


text:
Best way to "hack" a library without forking it
I'm using Visual Studio Code and PlatformIO.
I added some libraries to my project using platformio.ini:
lib_deps =
    olikraus/U8g2@^2.34.4
    crankyoldgit/IRremoteESP8266@^2.8.4
    plerup/EspSoftwareSerial@^6.16.1
    rlogiacco/CircularBuffer@^1.3.3
    https://github.com/me-no-dev/ESPAsyncWebServer.git
    makuna/NeoPixelBus@^2.7.0
    adafruit/Adafruit BusIO@^1.13.2
    bblanchon/ArduinoJson@^6.19.4
    adafruit/Adafruit PN532@^1.2.2
    khoih-prog/ESPAsync_WiFiManager@^1.15.1
    https://github.com/guestisp/ESP32AsyncDNSServer.git

Now, let's say I have to "hack" few lines of code inside a library to fit my needs.
Since it's a very specific change (not something that can be useful to others) I can easily change directly the source code without forking it on github.
The downside is every time I download again the libraries (or set up another dev machine) I lose all my changes.
I can also move the downloaded library to the lib/ subfolder in the project tree, removing the dependency from lib_deps.
I wonder if there is a more reliable way to do this, i.e. applying a patch after downloading the code (yocto-like).

Use a local source control and repository of your choice. Obtain the latest published rev of the library and make your changes to it (I assume you have already done this, probably a number of times). Check in your changed revision of the library to your local repository. When a new rev becomes available, obtain it and merge it into your local repository.
Update:

One problem of this approach is I will end up with nested
repositories.

I'm coming from old-school source control - file management. rcs, in my case (don't laugh). I'm guessing your local modifications affect only one or a few of the library files. Mightn't something that simple - a file-level merge - solve your issue (regardless of which tool you choose to do it)?

One problem of this approach is I will end up with nested repositories. My project directory is already a git repository. If I clone the library repository inside, say, the lib folder it won't pushed inside mine since it's nested. At least, I was not able to do this.

@Mark: You may clone the library outside of your project's directory, commit your changes there in a dedicated branch, and then include it into your project as a git submodule. Alternatively, you may consider using a git subtree.

@EdgarBonet yep, git subtree did the trick (and for me is the actual solution to my question). I only knew git submodules that are tricky for this kind of scenario.


text:
How to use the ESP32 WROOM GPIOS above #18 with arudino ide?
I am using the Arduino IDE version 1.8.8 I am trying to control an ESP32-wroom. The gpios for pins 16 and 17 are working. I have some relays on GPIO's 18 and 19 that I'd also like to be able to control. I also have no control over GPIO's that are over 17 I've tried 18 through 27 and I can't toggle them.
#define relayPin3 18
#define relayPin4 19

int relayflag = 0;
void loop() {
  delay(1000);
  if (relayflag == 0){
    digitalWrite(relayPin3, LOW);
    digitalWrite(relayPin4, HIGH);
    relayflag =1;
  }
  else
  {
    digitalWrite(relayPin3, HIGH);
    digitalWrite(relayPin4, LOW);
    relayflag = 0;
  }

}

I noticed that GPIO's 18 and 19 are also shared with spi ports.
Does the digitalWrite command work for any GPIO or is it in use by another library? Is there some other command to get access to the GPIO's I need to use? Some library I need to modify.

Found the answer, albeit a simple one. This is my first go around with arudino so it will look ridiculous to those who program microprocessors (including myself)
You need to declare the pin to be an output before use. The inconsistent thing is, this only happens sometimes. For some of the pins you don't need to use pinMode for them to work.
  pinMode(relayPin1, OUTPUT);

We all have to start somewhere in learning new things. Well done posting your solution.

If you don’t set a pin’s mode, the results are undefined. It might already be in the mode you want, and might not.