Hot answers tagged

26

As is my wont, I'm going to provide a bit of background technical information into the whys and wherefores of this this error. I'm going to inspect four different ways of initializing C strings and see what the differences between them are. These are the four ways in question: char *text = "This is some text"; char text[] = "This is some text"; const char ...


20

As a sort of prologue to this overly long answer... This question got me deeply captivated with the problem of interrupt latency, to the point of loosing sleep in counting cycles instead of sheep. I am writing this response more for sharing my findings than for just answering the question: most of this material may actually not be at a level suitable for a ...


17

The short answer: You don't. With enough know-how, you could probably extract the executable binary from the Arduino, but the source code is not installed on the device. You would need to run a decompiler on the binary. (Or read the machine code directly.) The output of a decompiler is usually pretty ugly however, and will look quite different from the ...


16

Thanks to the help of Charlie Hanson and jantje I found the universal solution! Because it is a pity that every Arduino programmer has to go through the same pain to figure out how to get the board name, I will offer my code to the public so everybody can just copy and paste it. Here it is: #if defined(TEENSYDUINO) // --------------- Teensy --------...


13

There is a function in the standard Arduino library called dtostrf(). I think of it as "Decimal to String Float". You pass in the float, how wide you want the whole number to be (if it will fit), the number of decimals of precision - and the buffer you want it to fill. Note that! You need to provide the buffer, and you need to be careful to provide more ...


11

In terms of the core language features, there's virtually no difference. In the background, the Arduino IDE and Atmel/AVR Studio both use GCC to compile the code. GCC is an excellent toolset which supports all the core language features of C++. Admittedly, the version which currently comes with the Arduino IDE is a little old, so it doesn't support C++11 ...


11

The problem is that you are passing the value of the ports to the function, not the address of the ports. You need to either pass "by reference" or as a pointer, so that modifying the port variable modifies the actual port variable not the copy of the value that you pass. The simplest way is to modify your function to be: void shiftOutFast(volatile ...


10

You want strlen. char foo [100] = "bar"; Serial.println (strlen (foo)); // --> prints 3


9

You can extern "C" #include like the following: extern "C"{ #include "crc16.h" }; void setup(){ } void loop(){ CalculateCRC16("<09M", 4); } And the crc16.h file could be (some minor fixes, the #pragma once, a cast): #pragma once #include <stdio.h> #include <stdint.h> uint16_t crctable[2] ={ 0x0000, 0x1189}; uint16_t CalculateCRC16( //...


9

Replace the line: if (A0 == HIGH) { by this one: if (digitalRead(A0) == HIGH) { If you plan on using the pin back as an analog input, use something like this: if (analogRead(A0) > 512) { The explanation is what Ignacio said: you were comparing the pin "name" A0 directly to HIGH instead of reading from the input named A0. To make it clearer, ...


9

This schematic shows a complete connection of a blinking led. Red cable = +5V (pin 8) White cable = Ground (pin 4) Resistor = 1 end at pin 5 (IO 0) and the other in the LED anode (+) Led anode (+) in the resistor, cathode (-) (small leg) in the ground Resistor = 330 Ohms The arduino in the picture is not necessary, just used to give power. But in the video ...


8

Putting them all in a single condition like that is absolutely fine. C/C++ uses "short circuit evaluation" which means it will stop as soon as it's determined the outcome. If everything is combined using logical AND operators (&&) then that means it will stop as soon as one of them evaluates to false. For example: if (blah() && foo()) { ......


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 ...


8

That is an incredibly hard question to give a definitive answer to. In general using OO consumes more memory, but that is the price you pay for greater flexibility and ease of use. The difference is usually minor though. Variables within an object take the same amount of space as variables outside an object. It's just the overhead of instantiating the ...


8

You cannot set pin states in multiple ports simultaneously, if that's what you're asking. At the instruction set level, setting pin states is a single that writes to an 8-bit register which corresponds to the 8 pins in a port. There is no way to write to two different registers in the same instruction, so if you need all eight bits to change exactly at the ...


8

Loop runs as long as loop needs to run. Instructions in a CPU run sequentially. The more instructions there are the longer it takes to run. The more code you put in loop the longer loop will run. There are two ways to know how long each iteration of loop will take: Profiling: Actively time each iteration of loop, though be warned, the act of timing ...


8

A cheap trick to round a number to one decimal place (in "chop-off mode") is to multiply it by 10, convert it to an integer, and divide by 10.0f again: float myVal = 123.89f; myVal = (long)(myVal * 10) / 10.0f; // = ((long)1238.9) / 10.0f // = 1238 / 10.0f // = 123.8 You can then print this modified number to the serial monitor. This method can also be ...


8

You never can get the actual code back ... by reading the flash you might be able to get the executable/runtime code, but this doesn't look at all similar as your code, although it's functionally the same. For example, all function names, all variable names will be just numbers (or non recognizable names), and all C statements are converted in machine ...


7

I certainly can't duplicate what you are saying. The small code snippet: void setup() { PORTD |= _BV(0); PORTD |= _BV(1); PORTD &= ~(_BV(1)); } void loop() { } compiles into: 000000a6 <setup>: a6: 58 9a sbi 0x0b, 0 ; 11 a8: 59 9a sbi 0x0b, 1 ; 11 aa: 59 98 cbi 0x0b, 1 ; 11 ac: 08 95 ...


7

If you have a read of section 31 of the Datasheet, available from here, things may come a little clearer for you. Here's a summary of what I know: PIO stands for Parallel Input/Output and offers the functionality to read and write multiple register ports at a time. Where the datasheet mentions a register, for example PIO_OWER, the Arduino library has ...


7

If you have a C string containing ASCII numbers with a common delimiter (comma in this case) you can use the strtok() function to split it into individual strings. Then you can use atoi() to convert those individual strings into integers: char array[] = "10,11,12,1,0,1,0"; int intArray[7]; // or more if you want some extra room? int ipos = 0; // Get the ...


7

Passing messages across computers and/or microcontrollers with different architectures can be a real pain (endianness...). So do not fall into NIH / reinventing the wheel traps but keep standing on the shoulders of giants! Several message serialization protocols exist. BSON MessagePack Protocol Buffers CBOR (Concise Binary Object Representation) ... ...


7

It's worth exploring how PORTD is defined in the actual AVR header file. The relevant file is iom328p.h, and it's defined as: #define PORTD _SFR_IO8(0x0B) _SFR_IO8 is defined as #define _SFR_IO8(io_addr) _MMIO_BYTE((io_addr) + __SFR_OFFSET) and #define _MMIO_BYTE(mem_addr) (*(volatile uint8_t *)(mem_addr)) This shows that 0x0b is not the address of ...


7

You read the contents of flash and save it to a file. It can then be re-flashed if your need is not to edit, but duplicate. This reads all flash, including the bootloader, and also PROGMEM data. Use avr-objdump (or similar) to disassemble the instructions. Again, PROGMEM data is going to be interpreted as opcodes, and may not be valid opcodes. You may ...


6

Ignacio Vazquez-Abrams has already outlined the schematics. There must be a C program compiled and uploaded to it, just like an Arduino board would have. There are however some differences between a fully equipped Arduino board with an ATMega328P and a bare ATtiny85 you would have to care for in your code: A bare ATtiny85 runs at 1 MHz (8 MHz internal ...


6

Without knowing your exact implementation details... You should be OK as long as you don't have multiple clients connected at the same time. You may think that there are multiple clients connected to your server, but the connection is closed after the resource is obtained by the client over a protocol.* The standard flow of a server on Arduino: A client ...


6

If you're working with the standard Arduino environment and libraries then printf() won't display anything anywhere. The reason is that microcontrollers could be connected to any number of different output/communication devices in any way. stdio would have no idea how or where to send the data. The most common approach to outputting text from an Arduino (...


6

The interrupt is being set to fire on a change, and your test_func is set as the Interrupt Service Routine (ISR), called to service that interrupt. The ISR then prints the value of the input. At first glance you would expect the output to be as you have said, and alternating set of high lows, as it only gets to the ISR on a change. But what we're missing ...


6

You should declare volatile the port parameters of your function. Otherwise, to the compiler, their write accesses seem useless, and they get optimized out.


6

It's no different to working with Make and any other form of GCC. Just set your CC variable and CFLAGS variable accordingly and work as per normal. For instance, I just knocked this one up: CC=avr-gcc OBJCOPY=avr-objcopy CFLAGS=-Os -DF_CPU=16000000UL -mmcu=atmega328p PORT=/dev/ttyACM0 led.hex: led.elf ${OBJCOPY} -O ihex -R .eeprom led.elf led.hex ...


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