Show Posts
Pages: 1 2 [3] 4 5 ... 116
31  Using Arduino / Microcontrollers / Re: Pin Change Interrupt during ISR on: July 07, 2014, 02:16:03 pm
That's fine then. Just wanted to make sure the pin change interrupt wasn't a strange one (or at least no more so than it already is).
32  Using Arduino / Microcontrollers / Pin Change Interrupt during ISR on: July 07, 2014, 07:34:21 am
Something not entirely clear from the ATTiny861 datasheet (and probably similar for most AVRs) is what happens when a pin change occurs during the pin change ISR. An example might help.
Say there are two pins which are enabled for generating a pin change interrupt, and that they are part of the same bank so use the same interrupt vector:
ISR (PCINT_vect){  //so a pin change has occurred

  //<---- (1) what happens if a pin change occurs *here* (before reading PINA, but after entering the ISR)
  byte current = PINA; //lets assume that both belong to Port A
  byte changed = old ^ current; //and that 'old' is a global containing the old value for PINA, then we know which have changed.
  old = current; //back up for next time
  //So at this point we have a 'changed' variable which has ones for each bit which has changed state
  //<---- (2) what happens if a pin change occurs *here* (or for that matter any time after reading PINA)

  ... //other code - e.g. if statements to perform specific tasks depending on what changed

There are two places there where I am not entirely clear on the behaviour of the ATTiny at these points. Will the ISR be called again after it returns the first time? Or will the changes be lost?

Obviously if the interrupt flag is not set again, then it isn't an issue for (1) as that is already checked, however it causes an issue for (2) as that is not checked yet.
If the interrupt flag is set again, then that is fine for (2) as it needs checking, but it is a bit wasteful for (1) as it has already been checked [won't do any harm as we are backing up the last known value].

I'm hoping that another interrupt is triggered, but want to be sure.
33  Using Arduino / Microcontrollers / Re: ATtiny84 & <avr/fuse.h>, fuses automatically programmed? on: July 07, 2014, 02:45:38 am
The only way to program the fuses is by ISP or HVSP. It is not possible to do it from code running on the microcontroller.

You can however do it in the makefile:
#How to program the fuses
AVRDUDE_WRITE_FUSES = -U lock:w:0x3f:m -U efuse:w:0x$(EFUSE):m -U hfuse:w:0x$(HFUSE):m -U lfuse:w:0x$(LFUSE):m
#Set up your fuse values:
HFUSE = ...
LFUSE = ...
EFUSE = ...

# Program the device.  
program: $(TARGET).hex $(TARGET).eep
34  Using Arduino / General Electronics / Re: PNP on: July 06, 2014, 11:11:16 am
You should add a ~100k resistor between the base and emitter of the PNP transistor to make sure that when the switch is open, the transistor is turned off and not floating.

@peter_n The MOSFET in the circuit is correctly drawn as an N-Channel MOSFET.
35  Using Arduino / General Electronics / Re: Huge voltage drop from H-bridge (4*N-channel Mosfets) on: July 06, 2014, 10:48:50 am
For starters, use the correct circuit symbols. OP is talking about MOSFETs and yet has drawn BJTs - totally different symbol, different behaviour and it is confusing.

The problem is that your gate voltage is not high enough on the high side N-channels.

In order to turn on, you need a Vgs > Vgs(th). For the low side, that is easy as the source is connected to ground, giving you a gate voltage relative to ground. However for the high side, the source voltage is connected to the motor, so if you want 12V across the motor you need:
 Vgs = Vg - Vs = Vg - 12 > Vgs(th).

The threshold voltage for large MOSFETS is typically ~2-3V, so that means you would need around >15V on the gate.

In your circuit you have 12V on the gate, which means to turn on, the source must be ~3V lower, giving you Vs = Vg - Vgs = 12 - 3 = 9V across the motor. Of course the voltage for a given current is higher than the threshold, so for larger currents you will see less voltage across the motor. This also leads to a large power dissipation in the high side transistors as they are running with a large voltage drop across the channel.

As Peter_n states, it is possible with a voltage doubler circuit. I have successfully used the following circuit (though mine was for a 7.2V supply, its the same principal).
The signals 'A' and 'B' can be driven by +5V logic without too much problems. If you want the MOSFETs to be driven more strongly, add an amplifier stage to driven them at +12V as in your circuit.
The square wave signal can be generated from the Arduino and fed through an amplifier to boost it two +12V - though you would need the amplifier to be a push pull type to work properly (e.g. an Op-Amp). It may be possible to feed a +5V signal in, but that would only get you a boost of ~3V due to the forward drop of the diodes, so a larger input voltage would be preferable.
36  Using Arduino / Displays / Re: Tiny size displays (any) on: July 05, 2014, 03:29:08 pm
What sorts of resolution of display are you looking for?
37  Using Arduino / Microcontrollers / Re: is the DDR pinout the same as the PORT pinout? on: July 05, 2014, 03:24:53 pm
DDRx should work in the same way as PORTx in terms of bit orders. If you don't want the pullups to turn on, you should also write the corresponding PORTxn bits to 0.

One little unrelated thing though:
Why this:
portState[0] = (portState[0] & ~portFilter) | ( & portFilter);
PORTA = portState[0];
When you can just do:
PORTA = (PORTA & ~portFilter) | ( & portFilter);
38  Using Arduino / Programming Questions / Re: How to change variables in the arduino code w/o having to re-upload the code??? on: July 05, 2014, 03:16:34 pm
Wire a routine which checks for available Serial data, and if it find it, use it to set your variable.
For example say your variable (no idea what it is as you haven't been clear) is a byte:
byte someMysticalVariable;
if (Serial.available()){
  someMysticalVariable =;
That would simply set the variable to whatever value is received from the serial port.
You could write something to parse decimal strings e.g. if you type 1234 into the Arduino serial monitor, you will actually receive four bytes, ('1','2','3','4'} which you would have to convert back into an integer.
39  Using Arduino / Programming Questions / Re: How to split a byte into nibbles ? on: July 05, 2014, 02:58:14 pm
You could just use a sledge hammer to crack a nut and do:

int value = -76;

char ascii[3];
byte data = abs(value);

Woops, missed the last two posts. Never mind.

In terms of converting to ascii digits, there is no point using a lookup table. Just do something like:

int vol = -76;
vol = abs(vol);
byte tens = vol/10;
byte units = vol%10;
//assign the array new values:
SEND_VOL_LEVEL[11] = tens + '0'; //to convert decimal digits to ascii, just add '0' (0x30);
SEND_VOL_LEVEL[12] = units + '0';

40  Using Arduino / Microcontrollers / Re: Atmega328p: only able to upload one sketch on: July 01, 2014, 04:45:56 pm
Are you trying to use the internal oscillator? Or have you got an external 8MHz crystal/oscillator?

The internal osc is not tuned, so it wont be exactly 8MHz, instead it will be +/-10%. It is also temperature and voltage dependent. Basically it is unreliable for achieving accurate baud rates making programming hit and miss with a bootloader.
41  Using Arduino / Microcontrollers / Re: Atmega328p: only able to upload one sketch on: July 01, 2014, 04:11:58 am
Are you uploading via serial, or using 'Upload Using Programmer'? The latter of the two erases the chip before programming, so removes the bootloader.
42  Using Arduino / Programming Questions / Re: prototype function error on: June 30, 2014, 03:20:11 am
I think above code line is also wrong, because we are assigning char stringoutput[] to stringoutput*, which is incompatible.

Correct, and to add to the problem, stringoutput[] has been declared as a pointer to an array of length 1 - the initialiser sets the length if the length is not specified and in this case the initialiser is an empty string ("") which is just a null terminator.

You can't just concatenate strings like that without using realloc()/malloc()/dealloc() to change the amount of room in the array. Alternatively you can declare an array with enough space for your longest message to begin with and then use strcat() to append characters.

On top of that, you really shouldn't be putting all that code in the header. Put only a prototype in the header and add the rest of the code into a separate .cpp file which #includes the header.
43  Using Arduino / Programming Questions / Re: Must have more files! (Datalogging to SD card) on: June 26, 2014, 03:58:22 pm
SREG = SREG | B10000000; // Enable gobal interrupts. They should
    // already be enabled but I like to do this out of good measure.
A bit unrelated, but still good for you to know. That line of code is equivalent to:

With regards to naming the files, the simplest way would be to store a value in the EEPROM of the number of the last file used. Then next time a file is created, simply name it with that number+1 (and store the number to the EEPROM). That way you always know which filename to use, even if files are removed from the SD card.
44  Using Arduino / Programming Questions / Re: abs() becomes negative? on: June 26, 2014, 10:45:00 am
Only on the Due/Arm boards.

For 8bit Arduino boards:
a byte = 0 to 255
a char = -128 to 127
an int = -32768 to 32767
a word = a short = unsigned int = 0 to 65536

If you want to know the size of something, just run this sketch:
void setup (){
  Serial.print(sizeof(int));// change the type inside sizeof() to any of your choosing, e.g. sizeof(byte)
void loop(){}
45  Using Arduino / Programming Questions / Re: abs() becomes negative? on: June 26, 2014, 10:03:52 am
You've declared 'lastShot' as a signed 16bit integer which means it can take values of -32768 to 32767.

If you try and store a value of say 32768 (one more than the highest positive), in future calculations it will be seen as -32768 as the two numbers have the same 16bit binary representation.

Either declare last shot as an 'unsigned int' or use a larger variable type.
Pages: 1 2 [3] 4 5 ... 116