Show Posts
Pages: 1 ... 52 53 [54] 55 56 ... 110
796  Using Arduino / Programming Questions / Re: Read Unsigned Long from Serial on: December 28, 2012, 05:12:26 am
parseInt may look for a long, but according to its arduino page it returns an int as the op said.

For unsigned, you can use the unsigned version of strtol, called 'strtoul'
http://www.cplusplus.com/reference/cstdlib/strtoul/

Example usage:
Code:
  unsigned long ul = strtoul(inputString,NULL,base);
797  Using Arduino / Programming Questions / Re: Sloppy I'm sure, but ALMOST working! on: December 27, 2012, 07:31:41 pm
Tom you hit the nail right on the head!  NO more slightly lit LEDs for apparently no reason, NO more flickering, NO more arbitrary LEDs lighting!

Woo- HOO!

I completely didn't realize the shield used those pins!  THANKS SO MUCH!  Mystery problem #25- SOLVED!

Glad to hear it. The ethernet sheild uses the ISP programming header to maximise compatibility, as on boards such as the Mega the SPI pins are in a different location (digital 50-52). However it just so happens that on the Uno, the ISP header connects to digital pins 11 through 13. The ethernet sheild also uses pin 10 for the SS pin.
798  Using Arduino / Programming Questions / Re: Read Unsigned Long from Serial on: December 27, 2012, 07:27:51 pm
You could use atol() instead of atoi().
http://www.cplusplus.com/reference/cstdlib/atol/

strtol() is another option which is more robust, but has a larger footprint.
http://www.cplusplus.com/reference/cstdlib/strtol/
799  Using Arduino / Programming Questions / Re: Quick question about populating arrays on: December 27, 2012, 05:48:16 pm
If you swap two lines around in the struct you could just do:
sliderButton_t button[10] = { {50}, {84}, ... and so on.

Code:
  byte xEEPROM;         // EEPROM adresss for stored value
  int  xPos;             // Stored value for X position
Becomes:
Code:
  int  xPos;             // Stored value for X position
  byte xEEPROM;         // EEPROM adresss for stored value

Unless you are relying on the position of each item in the struct then the above would work fine.
800  Using Arduino / Programming Questions / Re: Sloppy I'm sure, but ALMOST working! on: December 27, 2012, 05:43:48 pm
On the Uno, the ethernet sheild uses pins 10,11,12 for SPI communication between the Ethernet chip and the arduino which means you won't be able to use your shift registers at the same time as the ethernet sheild unless you change which pins you are using (e.g. 5,6,7 rather than 10,11,12).
801  Using Arduino / Programming Questions / Re: timing problem returning a pointer to an array on: December 27, 2012, 05:37:34 pm
I wasn't suggesting passing a pointer to the struct, but rather the struct itself (as it is only 4 bytes in size it isn't massively inefficient).

Looking at the way your Create ID function works, you could even go a step further and try something like this:
Code:
typedef union{
  struct{
    //First byte in array
    unsigned name:6;
    unsigned category:2;
   
    //Second byte in array
    unsigned empty:5;
    unsigned target:3;
    //(Note first and second bytes have been swapped due to endian-ness)
   
    //remaining 2 bytes in array.
    unsigned int zero;
  };
  byte array[4]; //access bytes through array as before.
  unsigned long value; //so its full value can be seen
} ID_TYPE;

ID_TYPE CREATE_ID (byte Category, byte Name, byte Target); //needed prototype to avoid it being added automatically before the ID_TYPE definition.


void setup (){
  Serial.begin (115200);
}

void loop (){
  SEND_JOYSTICK_MESSAGE ();
  Serial.println ("");
  delay (1000);
}

void SEND_JOYSTICK_MESSAGE (){
  byte Category = B01; // Roboteq SetCommand
  byte Name = 0; // _G, but will contain data for both channels
  byte Target = B001; // Roboteq target ID
  ID_TYPE ID;
  ID = CREATE_ID (Category, Name, Target);
  Serial.println ("returned ID bytes:");
  Serial.print (ID.array[0]); // should be 64
  Serial.print ("    ");
  Serial.print (ID.array[1]); // should be 32
  Serial.print ("    ");
  Serial.print (ID.array[2]); // should be 0
  Serial.print ("    ");
  Serial.println (ID.array[3]); // should be 0
}

ID_TYPE CREATE_ID (byte Category, byte Name, byte Target) {
  ID_TYPE ID = {Name,Category,0,Target,0};
  //The line above initialises the struct in the union.
  //The same could be accomplished with:
  /*ID_TYPE ID;
  ID.name = Name;
  ID.category = Category;
  ID.target = Target;
  ID.empty = 0;
  ID.zero = 0;*/
 
  Serial.print ("Category = ");
  Serial.print (Category);
  Serial.print ("   Name = ");
  Serial.print (Name);
  Serial.print ("   Target = ");
  Serial.println (Target);
  Serial.print ("ID as uint32_t = ");
  Serial.println (ID.value); //this value is different to yours due to endian-ness, but the resulting array is the same.
  return ID;
}
802  Using Arduino / Programming Questions / Re: timing problem returning a pointer to an array on: December 27, 2012, 02:26:24 pm
You could also define a struct type which contains an array of 4 bytes, then return a struct from the function.
803  Using Arduino / Programming Questions / Re: Seems like this should work.... on: December 26, 2012, 06:36:05 pm
Firstly, I don't think this is what you were trying to do:
Code:
else;

perhaps this:
Code:
else {
and an additional closing '}' at the end of the  for loop for the else statement.
804  Using Arduino / Programming Questions / Re: Can you convert a HEX byte to a literal character array? on: December 26, 2012, 04:24:48 am
I told you that chars and strings confused me !  You did say that you wanted and array of chars in your first post.
Why are you expecting a 2 character string ?

I tried this example which does what I expected.  What do you want/need that is different ?

The original poster said:
Quote
char charArr[4] = {'3', 'D', 'F', 'F'}
That means that the 0x3D needs to become {'3','D'}. The 0xFF needs to become {'F','F'}
805  Using Arduino / Programming Questions / Re: Can you convert a HEX byte to a literal character array? on: December 26, 2012, 04:18:36 am
I'm pretty sure you can do it like this:
'\xnn'
where nn is your number, such as:

'\x3A' (note the quote marks)

but having said that, I have just realised that you are trying to convert a byte to a non-terminated string, in which case you could try this:

Code:
 #define NUMBEROFBYTES 2
  byte myArr[2] = {0x3D, 0xFF};
  char charArr[2*NUMBEROFBYTES + 1]; //Note there needs to be 1 extra space for this to work as snprintf null terminates.

  ...

  char* myPtr = &charArr[0]; //or just myPtr=charArr; but the former described it better.
  for (byte i = 0; i < NUMBEROFBYTES; i++){
    snprintf(myPtr,3,"%02x",myArr[i]); //convert a byte to character string, and save 2 characters (+null) to charArr;
    myPtr += 2; //increment the pointer by two characters in charArr so that next time the null from the previous go is overwritten.
  }
806  Using Arduino / Programming Questions / Re: Flattening a multi-dimensional char array to an char* array on: December 24, 2012, 11:02:51 am
(1) You cant just add char arrays, there is no such operator defined:
 
Code:
     strCmd += IRCodes[i][ii];      // does not work

(2) You have declared strCmd as a pointer. This means that the compiler has allocated only two bytes of memory to store a pointer to a char. This is not an array, it is just a pointer, so the compiler doesn't know to allocate a block of memory for an array
Code:
char* strCmd;
  int cc = 0;
  for (int i=0; i < 4; i++) {
    Serial.print(i);
    Serial.print(". ");
    strCmd = "";
(3)In the last part of that code snippet you try to assign a string to your pointer, this cannot be done in C. And again in this bit:
Code:
   irCommand[i] = strCmd;

Try making some adjustments, such as:
Code:

  char irCommand[4][5] = {"0000","0001","0002","0003"};

...

  int cc = 0;
  for (int i=0; i < 4; i++) {
    Serial.print(i);
    Serial.print(". ");
    strCmd[0] = '\0';
    strcpy(irCommand[i], IRCodes[i]); //copy from ircodes into ircommand

    Serial.println();
    Serial.println(strCmd);
  }
807  Using Arduino / General Electronics / Re: Higher current NPN transistor with same characteristics as a BC337 on: December 23, 2012, 08:45:47 am
Quote
You want Vbe(on)=0.7V.

For switching, you want to maximize the base current  - forget about Vbe. Simply inject as much current into the base as you can to saturate the switch as much as you can.

If you stick 5v on the base you will be running the transistor at its absolute maximum ratings which will most likely damage it in the long run. Furthermore if you do nothing to limit the base current you will damage the microcontroller from drawing too much current from its port drivers.
808  Using Arduino / General Electronics / Re: Higher current NPN transistor with same characteristics as a BC337 on: December 23, 2012, 05:17:21 am
"Max Power dissipation: 625 mW"

In otherwords, when 400mA is flowing through the load, if any more than 1.5 volts is across the transistor, it will likely overheat and be destroyed.

You should be looking for a transistor in at least a TO-220 package for high power loads like 1A+ LEDs.

Emitter base voltage is stated at 5V - does that mean the gate will be fully open 5V and maybe not fully open at anything less?

Wrong. That is the ABSOLUTE MAXIMUM. You want Vbe(on)=0.7V. As I mentioned before, NPN transistors use current to control current. You ensure that there is 0.7V at the base with respect to the emitter, and then use a resistor to control the current flowing into the base from the Arduino.



If your heart is set on a BJT, you could look towards the TIP120/TIP121/TIP122 or similar (http://www.adafruit.com/datasheets/TIP120.pdf). They are darlington pairs so have a very high current gain allowing you to control large loads with low currents. They do have a downside that the Vbe(on) is higher (~2.5v)


Alternatively, if you want to try out MOSFETs which are somewhat easier to use for high power loads (complete isolation of gate and channel offers some protection for the arduino), you could look for IRF510s (http://www.irf.com/product-info/datasheets/data/irf510.pdf) or IRF640s (http://www.vishay.com/docs/91036/91036.pdf).
809  Using Arduino / General Electronics / Re: Higher current NPN transistor with same characteristics as a BC337 on: December 22, 2012, 06:48:35 am
MOSFET = Metal Oxide Semiconductor Field Effect Transistor
BJT = Bipolar Junction Transistor

Basically, a MOSFET works by using an electric field to control current through a channel. When you apply sufficient voltage on the gate relative to the source (Vgs(th) in the datasheet), a low resistance connection between drain and source is created. The current that can flow through the channel is proportional to the voltage applied to the gate.

A BJT, such as the NPN of which you speek uses a current flowing into the base to control a current flowing into the collector. To turn a BJT on you need about 0.7v on the Base relative to the Emitter. When that is applied, the current flowing from collector to emitter is proportional to the current flowing into the base.

In summary:
BJT = Current controlled load current.
MOSFET = Voltage controlled load current.

With a MOSFET this mean that for any load current, very little current is required at the gate making them very microcontroller friendly. With a BJT you need either a transistor with very high current gain to switch a large load with a microcontroller as the Arduino for example can only source 20mA per pin.
MOSFETs are also much faster at switching the currents, so waste less power when using in systems where high frequency switching (such as PWM) is used. 
810  Using Arduino / Programming Questions / Re: SPI - Bitbang VS. SPI library on: December 21, 2012, 07:13:55 pm
Yup, you can get about 250kHz from a bitbanged software SPI using direct port writes from what I have seen. I can get 125k on an 8MHz ATTiny with the software SPI library I wrote.

I've attached the library. It is not quite finished, but it works, and has all the features of the Hardware SPI library except that you call begin() with the four arduino pin numbers for your SPI interface:
begin(SCK, MOSI, MISO, SS);
Pages: 1 ... 52 53 [54] 55 56 ... 110