Show Posts
Pages: [1] 2 3 ... 116
1  Using Arduino / Microcontrollers / Re: ArduinoISP failure, device signature 0x0000, programmer is not responding on: Today at 03:42:51 am
FYI if you click burn bootloader without a crystal, it will just change the fuse settings and not actually flash the bootloader. For some reason they decided to make the IDE first erase the chip and set the fuses, then reset the chip, then try and burn the bootloader, rather than doing it all in one step. This means that after the fuse is changed, avrdude can no longer read the chip without a crystal and so cannot actually upload the bootloader.
2  Using Arduino / Programming Questions / Re: new at this... on: September 16, 2014, 04:14:21 pm
Who needs all that rubbish and the String class - sprintf() is your friend:

Code:
char filename[13];
sprintf("%04d%02d%02d.csv",year(),month(),day());

Also, you will have to make the "cFilename" variable larger if you want to fit all that in - 8 characters is not enough, you need a minimum of 13 for yyyymmdd.csv
3  Using Arduino / Programming Questions / Re: Compilation error - ISO C++ says that these are ambiguous on: September 14, 2014, 11:06:27 am
That's because it can't tell the which one of the two you want from the variables you have supplied. Essentially you have asked for this:

Code:
I2c.write(<int>,<int>,<byte>); //note: the literals are <int> by default

But that doesn't exist, so which does it pick? Instead, you can do one of the following three things:

Code:
const byte first = 0x57;
const byte second = 0x05;
I2c.write(first,second,lowByte);

or

Code:
I2c.write((byte)0x57,(byte)0x05,lowByte);

or

Code:
I2c.write(0x57,0x05,(int)lowByte);

The middle one seems the better option.
4  Using Arduino / Programming Questions / Re: Determining which board type is being used? on: September 14, 2014, 11:04:27 am
Also, functionally it makes no difference at all what dev board you have if they have the same microcontroller, the code will compile the same. The only potential difference is the pin mappings, but that is why Arduino Pin Numbers tend to be used, which are defined in the pins_arduino.h file for an associated dev board.
5  Using Arduino / General Electronics / Re: Using optocoupler for 220V signal on: August 13, 2014, 06:09:28 pm
The other option is to connect the wires directly (well almost) to the microcontroller. "WAIT!" I hear you cry... won't that destroy it? Well, yes, that is where the almost comes in. Stick a 1M resistor in series and you wont.

Here is an Atmel app note on the subject:
http://www.atmel.com/images/doc2508.pdf

Essentially what you get is a 5V square wave at mains frequency if there is a voltage, and nothing if there isn't.

--------
Alternatively, use the optocouplers for added safety. Something like the attached circuit will probably work (one copy for each signal). The output would not be quite a high or low as the voltage in is a sinusoid so you would get a square wave out so the added R-C circuit would correct this.
6  Using Arduino / Microcontrollers / Re: Some clarification needed... on: August 10, 2014, 08:57:47 pm
The Uno uses the Atmega328P-PU (I've put the -PU at the end as sometimes people get that confused the P), so no difference at all.

Programming the bootloader causes a chip erase first, so yes, everything is wiped.

In the IDE if you use the 'burn bootloader' function, it sets the fuses for you. The datasheet has all the information you could ever want about what they do. This is useful for calculating the fuse bytes: http://www.engbedded.com/fusecalc/

All your other points are correct.
7  Using Arduino / Displays / Re: New "Bubble" display! on: August 04, 2014, 07:02:45 pm
@Tom Carpenter,
Need the 24 pin version.
Ahh yes, of course. For a moment there I though each display had 10 pins!

They do seem a bit expensive.
8  Using Arduino / Displays / Re: New "Bubble" display! on: August 04, 2014, 03:24:33 am
I wonder what the comparative cost between that method and something like this is:
http://uk.farnell.com/aries/20-823-90t/socket-horizontal-display-20way/dp/1136581
9  Using Arduino / Displays / Re: New "Bubble" display! on: August 03, 2014, 08:23:56 am
I've had four of these displays sat on my desk for a couple of months just crying out to be used for something! Pretty cool be they.
10  Using Arduino / Microcontrollers / Re: How do I actually code this? on: August 03, 2014, 08:12:08 am
The zip has precompiled hex files which you can program on to the chip.
11  Using Arduino / Programming Questions / Re: Conditional using a float that is "-0" and how that works on: August 01, 2014, 04:37:19 am
Try:
Serial.print(currentDbLevel,6);

Are the numbers the same at 6dp? Or have they just been rounded by the print function.
12  Using Arduino / Programming Questions / Re: #define, int or const int on: July 19, 2014, 08:17:29 am
Not nonsense, in fact you prove the point, thanks

#define ABC 123.456 * 789
#define ABC_DEF 654.321

void setup()
{
  Serial.begin(115200);
  Serial.println(ABC);
  Serial.println(ABC_DEF);
  Serial.println(123.456 * 789_DEF);
}

void loop(){}

That doesn't compile because 789_DEF doesn't exist, and yet you have tried to use it?!?
The second print and the third print are not the same thing, nor does the pre-processor make the second one into the third one. Try compiling it without your added nonsense and it works fine! Try this:
Code:
#define ABC 123.456 * 789
#define ABC_DEF 654.321

void setup()
{
  Serial.begin(115200);
  Serial.println(ABC);
  Serial.println(ABC_DEF);
}

void loop(){}
What do you get? I'll bet you £50billion that it compiles fine and prints out exactly:
97406.78
654.32

I proved in my post the exact opposite of what you were saying, and in fact your last post just proved that what you are saying is nonsense.


It resolves, but also resolves OTHER things you might not have wanted to resolve

In my experience on my leonardo, the compiler DOES "overlap" defines, try it
Like hell it does - and yes I did try it. Plus the compiler for the Leonardo is the same for the Uno or Mega, so the board makes no difference at all.




Back to your other point, in terms of constant values, there is no difference is space consumed between a #define and a const with the exception of arrays - my point about arrays was simply that a const does under certain circumstances still use SRAM.
What I was trying to get across is that if you use a const int, when a const unsigned char would do, it is just going to produce sub optimal code and increased flash usage.
I have no doubt that using const's is better than using #defines simply for the fact that you are in control of the variable type.
13  Using Arduino / Programming Questions / Re: #define, int or const int on: July 19, 2014, 05:52:06 am

However, the arduino IDE does NOT differentiate between two defined "variables" with the same root properly
(In my way of thinking, anyway, it can get confused)
#define ABC 123.456 * 789
#define ABC_DEF 654.321
Is replaced with
#define 123.456 * 789_DEF 654.123
That isn't too bad, it does get resolved properly, but


That is total nonsense and not true!

Try running this sketch:
Code:
#define ABC 123.456 * 789
#define ABC_DEF 654.321

void setup()
{
  Serial.begin(115200);
  Serial.println(ABC);
  Serial.println(ABC_DEF);
}

void loop(){}
It prints out:
Code:
97406.78
654.32




#define ABC XYZ
#define ABCDEF QWERTY
#define XYZDEF ABCDEFG
Gives
Gives a compile error (already defined, because ABC is replaced with XYZ in the second line)

#define ABC XYZ
#define DEF ABCDEFG
means that when you use DEF you'll get XYZDEFG not ABCDEFG


And both of those are nonsense too! Definitely doesn't give a compile error at all, nor does it give you unexpected results.

The #defines will not overlap like that. For example everything with the letters "ABC" will not be replaced. Instead everything which is exactly "ABC" will be replaced. "ABCDEFG" is not exactly "ABC", it has four other letters in it. On the other hand: ABC*DEFG will be replaced with XYZ*DEFG because the two things are treated as separate entities as is expected with a multiply operation.

For example:
Code:
#define ABC 12
#define DEFG 10
#define DEF ABC*DEFG
#define XYZ ABCDEFG

void setup(){
  Serial.begin(115200);
  Serial.println(DEF);
  Serial.println(DEFG);
  //Serial.println(XYZ); will result in an error as ABCDEFG is not defined!
}
void loop(){}
Will print:
Code:
120
10



So, I started looking at consts.
Well, to my surprise,
Using const int i_variable_for_whatever does NOT use memory under ant circumstances that I have encountered so far.

As I mentioned earlier. They will not use RAM, but they will use memory! The constants get embedded into the machine code and so use Flash. If you use an int instead of a byte, it will waste one instruction (2bytes flash) adding in an unnecessary ldi instruction - it also produces sub-optimal code due to additional register usage in areas like interrupts.

However there is a circumstance where RAM is used even for const variables. Consider this:

Code:
const char bob[4] = "Hi!";

void setup(){
  Serial.begin(115200);
  Serial.println(bob);
}
void loop(){}
Code:
158: c8 01       movw r24, r16
 15a: 60 e0       ldi r22, 0x00 ; 0   //Load the address of the array in SRAM
 15c: 72 e0       ldi r23, 0x02 ; 2   //Load the upper byte of the address in the SRAM
 15e: 0e 94 01 05 call 0xa02 ; 0xa02 <_ZN5Print7printlnEPKc> //Call print, which will iterate through
This uses 4 bytes of RAM for the bob variable.

In fact anything which uses pointers (e.g. array) will generate RAM if you use a variable as the array index - if you think about it, how else does it access the data - it would have to use a series of if-elseif statements. In the above example, the print function iterates through each element in the array as it sends it to the serial port, hence you have a variable index and hence RAM is consumed.
If on the other hand a constant is used as the index at every instance of its use, then no RAM will be used as it is smart enough to replace the array access with the value at the known index. Building on the above example, this next one uses no RAM as the array is only ever accessed with a constant index. Of course it uses more flash as there are more function calls. It is probably a lot slower too.
Code:
const char bob[4] = "Hi!";

void setup(){
  Serial.begin(115200);
  Serial.print(bob[0]);
  Serial.print(bob[1]);
  Serial.print(bob[2]);
  Serial.println(bob[3]);
}
void loop(){}
Code:
 Serial.print(bob[0]);
 158: c8 01       movw r24, r16
 15a: 68 e4       ldi r22, 0x48 ; 72           ----- Load the 'H' character, ldi doesn't use ram
 15c: 0e 94 d8 04 call 0x9b0 ; 0x9b0 <_ZN5Print5printEc>
  Serial.print(bob[1]);
 160: c8 01       movw r24, r16
 162: 69 e6       ldi r22, 0x69 ; 105          ----- Load the 'i' character, ldi doesn't use ram
 164: 0e 94 d8 04 call 0x9b0 ; 0x9b0 <_ZN5Print5printEc>
  Serial.print(bob[2]);
 168: c8 01       movw r24, r16
 16a: 61 e2       ldi r22, 0x21 ; 33           ----- Load the '!' character, ldi doesn't use ram
 16c: 0e 94 d8 04 call 0x9b0 ; 0x9b0 <_ZN5Print5printEc>
  Serial.println(bob[3]);
 170: c8 01       movw r24, r16
 172: 60 e0       ldi r22, 0x00 ; 0            ----- Load the null character, ldi doesn't use ram
 174: 0e 94 00 05 call 0xa00 ; 0xa00 <_ZN5Print7printlnEc>




p.s. if you look at the ldi op code it doesn't use SRAM, you can see the constant embedded in the instruction:
61 e2 = ldi 0x21 - see the 2 and the 1?
60 e0 = ldi 0x00 - see the 0 and the 0?
68 e4 = ldi 0x48 - see the 4 and the 8?
14  Using Arduino / Programming Questions / Re: #define, int or const int on: July 17, 2014, 03:26:28 am
Its too quiet to tell you the truth, some warnings would be useful.

The IDE adds '-w' which means Suppress all warnings, even the ones you mentioned should happen do not ( I agree they should be shown ). So just cos you don't see any warnings, does not mean they never happen. Its a mistake if you ask me, some warnings will point out logic errors which will compile fine.

In 1.0.5/1.5.3 and greater, you only get error messages. I guess they base it on the fact newbies can't understand the messages spewed out. Kind of defeats the verbose output mode, the last error is always shown.
Time to switch to UECIDE! At least that way you get a proper build environment - and can even change the compiler options if you want.
15  Using Arduino / General Electronics / Re: Different A4988 stepper drivers give different results on: July 16, 2014, 02:42:13 pm
Have you definitely got the motor coils wired up right?

Remember that on the easy driver, one coil connects to A and the other to B.
On the Pololu board, one coil connects to 1A/1B, the other connects to 2A/2B
Pages: [1] 2 3 ... 116