Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.
22

Your question has 2 parts actually. 1/ How can I declare the constant size of an array outside the array? You can either use a macro #define ARRAY_SIZE 10 ... int myArray[ARRAY_SIZE]; or use a constant const int ARRAY_SIZE = 10; ... int myArray[ARRAY_SIZE]; if you initialized the array and you need to know its size then you can do: int myArray[] = {1, ...


20

No, the code doesn't "wear out" the MCU. In general no matter what you are doing roughly the same level of activity is being performed. Even delay() does lots of work while it's "idling". There are commands to make the CPU do less - place it in an IDLE or SLEEP mode - but these are used to save power rather than to reduce "wear" on the MCU. Of course there ...


10

Your assumption is quite wrong: The majority of libraries use OOP. The majority of in-built device drivers use OOP. The core is filled with many OOP helper objects and classes. 99.99% of all sketches use those OOP objects. The sketches themselves may not be written as a class or set of classes for a number of reasons: There is no point The programmer ...


9

The screenshot shows a base-2 (i.e. binary) result of calculating the constant part of a mapping. You can approximate this multiplication by a constant by a number of shifts added together. You need to break down each multiplication in the result first: x * 0.0012 = x >> 3 x * 0.00012 = x >> 4 x * 0.00000012 = x >> 7 Then add these together. You are ...


8

Can we read and get C codes from compiled codes from Arduino hardwares? While it is possible, even trivial, to disassemble machine code, it is very difficult to convert the assembly code into a higher-level language, and essentially impossible to turn it back into an exact copy of the source code it came from. There simply isn't enough information in the ...


7

The other answer mentioned some general ideas; here are a couple of more-specific notes. • You can direct your writes of single bytes through a routine that reads the EEPROM cell before writing to it, and if its value isn't changing, doesn't write. • For load-leveling, you can divide the EEPROM address space into k buckets, where k =⌊E/(n+1)⌋, with n = ...


6

Enable your IDE's verbose of the compile via the preferences. From there you can see exactly what and how it is being compiled. In short the IDE (and toolchain) will compile everything and anything it can find into an object. Where only the used code is compiled in the linking phase, as garbage collection is enabled with the "--gc-sections" command. Note ...


6

Yes you can, and the solution is very simple. It's in two parts, and they make up part of the absolute fundamental building blocks not just in C, but in almost any programming language. You can read good information on arrays here, and 'for loops' here (it's about half way down the page). Without wishing to offend, it seems like you are missing some really ...


6

One approach is to look at the assembly listing of the program. After compiling the program (via the Verify or Download button) locate the directory where the .cpp, .hex, and .elf files for the program have been stored. (If you have trouble finding them, briefly turn on verbose compile options in the arduino IDE and look at the directory names in the ...


6

You can't, and there's other things wrong with your code. int sensorValue1 = analogRead(A0); int sensorValue2 = analogRead(A1); Calling functions from the global scope is an incredibly bad idea. Those are run before the board has been properly initialized, and the results you may get from it are not guaranteed in any way, shape, or form. Instead you ...


6

This all gets very much trickier than just an array of function pointers. Your functions are methods in unknown objects. As such you can't have one single type that stores pointers to the functions in different objects. The simple reason is that C++ changes the function prototype for you. For instance, it changes: class MyClass { public: int ...


6

It's not. Well, it might slowly wear out if you run it like 20 years....(like most other physical products)? At least it does not rely on code complexity but how many writing operation done in the same memory section. Moreover, when it wear out it will just get bricked and it won't become a simpler code like blinking the LED. An Arduino (Uno) has three ...


6

For a hobbyist: About the only life limiting software related issue might be writing to the FLASH memory as fast as you can from inside a program. But few programs make use of variables that need to survive events like changing batteries. About the only life limiting hardware related issue is over loading the outputs with low resistance loads (...


6

This is a variation on the struct idea with an object-oriented flavor. If you put the current, low and high values of any sensor inside a struct, you could also morph this struct into a class and give it an update() method that, in addition to updating the current value, keeps the low and high updated: class LowHigh { public: LowHigh() : current(0), ...


5

This is not a direct answer to your question, as I have no such answer, and I believe the answer you are seeking would be of no use to you. The problem with your question is that your method of testing memory usage is heavily flawed. By removing the switch statement, you most likely have allowed the linker to remove lots of methods referenced by it, leading ...


5

... (or generally any other micro-controller) ... The other answers are great, but there is one small exception. Ferroelectric RAM (FRAM) is a form of memory that combines the non-volatility of flash and EEPROM, the writability of SRAM, and the density of DRAM. However, read operations on FRAM are destructive to the data stored in the FRAM, and write ...


5

You don't need to abandon the Arduino IDE. Let me quote from my forum post about sketch sizes. Introduction Every now and again this subject pops up on the Arduino forum. Why does it take 1000 bytes to blink an LED? Why, oh why? It is obviously very bloated, eh? Example: blink Let's check that claim first. The "blink" sketch, as shipped with the IDE (...


5

It.... depends A call to function involves a few things Save the context Move the parameter(s) to the appropriate registers Enter the function Execute Eventually save the return value to a register Restore the context Eventually copy the returned value to the variable As you can see, quite a lot of things. The bold points are the ones executed also if you ...


4

Think about it "logically" (pun intended...): You read a character. Say it's "A". You then compare it to both '\r' and '\n'. char c = Serial.read(); if (c != '\r' || c != '\n') { // <- This line interesting mAnswer[mAnswerLength] = c; mAnswerLength++; } That is saying "If A is not CR or A is not LF then add it". Since it's A and not LF or ...


4

How about this, mapping to 50..249? output = ((input * 25) >> 7)+50; Your input range was 1024 (0..1023). Output range is 200 (In the original specification, it would have been 201, which does not divide as neatly). These have a gcd of 8, so output = (input * (200/8)) / (1024/8) + 50 will do, and the division by a power of 2 can be expressed with a ...


4

The word you are looking for is array. An array is like a list of values all of the same type. For instance, your LED pins could be specified as: int leds[14] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 45, 46 }; You can then access the LEDs by number. Note that arrays start counting from zero not from one, so you would have: leds[0] leds[1] leds[2] ......


4

Why do you feel you need to use either method? Neither are needed. Just print the individual parts: Serial.print('X'); Serial.print(first); Serial.print('Y'); Serial.print(second); Serial.println('T'); No intermediate buffers, just sending the data direct out to the UART.


4

The standard Arduino way of doing this is to use pinMode(). It's not fast, but it's easy to understand, especially for beginners. And it's portable across architectures. If you want to go fast, then you should skip the Arduino functions and use port manipulation, and avr-libc. The standard avr-libc way of doing what you want is: DDRF = _BV(PF2) // set ...


4

I'm not sure if it will not blow your memory limit as every pointer takes up 16 bits. But in C you usually do it this way: void func1() { puts("Hello"); } void func2() { puts("World!"); } void func3() { puts("Array of Size 4"); } void func4() { puts("with Pointer"); } void func5() { puts("to a Function with no arguments"); } void ...


4

digitalWrite expects the pin and value to be uint8_t type. So you can certainly pass LOW or HIGH to functions. For example: void runMotor(int _dir, int _step, int wait, uint8_t value){ digitalWrite(_dir, value);


4

I can think of a few: don't power it on unless you have to; don't write to it unless you have to; write as little data to you as you can - compress the data; only write to it during brown out or power down, ...; level the writes to as many cells as possible - increment the write address with subsequent writes; use a big eeprom; use fram; use sram + battery ...


4

This question is not really about the Arduino. It is about GNU C/C++ compiler optimizations. By adding the keyword const the compiler can do a lot of optimization. One is that the value does not need to be stored in SRAM. The value can end up as part of an instruction, become part of an expression that is evaluated at compile-time, etc. In the Arduino-...


3

You are perfectly right, a class would help simplify your code. Here is a tentative class that should work for what you are doing: class Motor { public: Motor(const char *_name, Adafruit_DCMotor *_motor, unsigned long _runTime) : name(_name), startNow(false), running(false), runTime(_runTime), motor(_motor) { } void start() { ...


3

The problem is that you are using delay(). delay() will wait, from the moment delay() is called, for the given number of milliseconds. That is, in more structured terms: Do something Do something else Do something more Wait 30 minutes That will only take precisely 30 minutes if steps 1 to 3 take absolutely no time at all. And that will never be the case....


Only top voted, non community-wiki answers of a minimum length are eligible