Show Posts
Pages: 1 [2] 3 4 ... 115
16  Using Arduino / Programming Questions / Re: why do we use int instead of long on: July 14, 2014, 05:31:52 pm
Traditionally, the "int" type is the "natural" type for the processor - i.e., it's the size of the processor's registers.  This can be 4, 8, 16, 32... bits, but usually the processor can handle it as fast as possible.

Type "long" is for when you need more precision, even at the expense of slower code.  Type "byte" is good when you need to control the size, regardless of the processor's register size, or if memory space is at a premium.  In classic C, type "char" is usually 8 bits long and hence is used a a form of "byte".

Then there's type "short"....

Logically, many things would actually be well suited to type "bool" --- i.e., only two possible values.  I don't know how type "bool" is implemented, but I expect it takes one byte of storage per variable.

However with avr-gcc, and int isn't the native size for 8bit uCs, its 16bit (presumably because a byte was considered too small, not sure). A short is the same size as an int in this environment.
Also, a long doesn't really give more 'precision' per say, it gives a greater 'range'. All of the integer types have a precision of 1, hence they are integers.
Now if you were doing fixed point maths with them, then yes you could use greater precision, or greater range, or a bit of both.
17  Using Arduino / Programming Questions / Re: why do we use int instead of long on: July 13, 2014, 06:25:34 pm
Thanks for fast reply smiley
Then why dont we use byte ledPin=13; instead of int ledPin=13; ?

Because large parts of the Arduino core and many of the examples supplied are either badly written or hacked together without consideration for efficiency.
18  Using Arduino / Programming Questions / Re: Why arduino "ignore" % on: July 09, 2014, 10:36:16 am
The loop() will happen tens of thousands of times a second in that code, so unless you can see a light blinking that quickly, it will look like the light is always on.
Just to prove a point, add the following line at the end of your loop:
Code:
delay(100);
Is it doing what you expect now?
Try reducing the delay time. Notice what is happening?


As a side note, don't compare the return value of digitalRead() to 1 or 0, compare it to HIGH or LOW. For all you know, one day Arduino might make digitalRead() return 2 and your code won't work anymore, whereas if you use HIGH, it probably would still.
19  Using Arduino / General Electronics / Re: Circuit doesn't power off after disconnecting it's power supply on: July 07, 2014, 06:05:29 pm
The attached diagram might help.
20  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).
21  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:
Code:
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.
22  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:
Code:
#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
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FUSES) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
23  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.
24  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.
25  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?
26  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:
Code:
portState[0] = (portState[0] & ~portFilter) | (Serial.read() & portFilter);
PORTA = portState[0];
When you can just do:
Code:
PORTA = (PORTA & ~portFilter) | (Serial.read() & portFilter);
27  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:
Code:
byte someMysticalVariable;
...
if (Serial.available()){
  someMysticalVariable = Serial.read();
}
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.
28  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:

Code:
int value = -76;

char ascii[3];
byte data = abs(value);
sprintf("%02d",data);
Serial.print(ascii);

Edit:
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:

Code:
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';

29  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.
30  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.
Pages: 1 [2] 3 4 ... 115