Show Posts
Pages: 1 [2] 3 4 ... 110
16  Using Arduino / Programming Questions / Re: SPI at 2.500kHz? on: April 07, 2014, 05:41:52 pm
No, you can't divide by 6.4

There are only a set number of divisors which can be found by looking at either the SPI.h file or the datasheet.

You won't be able to divide down to 2.5kHz from 16MHz. For something that slow you are best doing bit-banging.
17  Using Arduino / Programming Questions / Re: Sscanf ruins my code :( on: April 07, 2014, 05:40:09 pm
Google "char array c++". You will instantly (first result) find an explanation of what a char array is.
Then google sscanf. The first result is an explanation and sample code of how to use the function.

Ditch the "String" class and use char arrays.
18  Using Arduino / Programming Questions / Re: Array Basics (sizeof) on: April 07, 2014, 05:31:26 pm
the size of a pointer is 4bytes (I'm assuming you are using a Due or something like if you got sizeof(int)=4).

sizeof() is not so much a function as it is a way of getting a "compile time" constant value of a size of an object in bytes.

You cannot use sizeof() in a function to get the size of an array passed to that function because at compile time the size isn't known - think about it, if you pass an array with 10 elements, then pass another array of 40 elements, how can the value of sizeof() be known at the time of compiling, and how if it is compiled in to the code can it change value during execution.

If you were to do this:

void someFunc(int array[10]){
}

Then sizeof(array) = 10 * sizeof(int). This is because you are specifying that the function can only take arrays of 10 integers - not 9, not 11, not 5232342.
19  Using Arduino / Microcontrollers / Re: Atmega pin designation on: April 07, 2014, 01:02:32 pm
If you write 'int LED = 3', the *compiler* (not IDE) has no idea what in hardware you are referring to, just that you have a variable called LED which is assigned the value 3.

If you do this:
digitalRead(3);
Then the compiler still has no idea what hardware you are referring to, it simply calls the function digitalRead(). The function then reads a pointer from the flash which it uses to write to the correct hardware register.

The pointer in the flash is where the magic happens, and this is all defined in the pins_arduino.h file.
20  Using Arduino / Programming Questions / Re: Why == works in an if statement? on: April 07, 2014, 08:01:54 am

"==" means "does it equal?"

"=" means "set this to that".

Example:
   X = 3;
    if (X == 4) {
        print ("It's not the same!"); <-- this gets printed because 3 is not equal to 4
    } else {
        print ("It matches!");
    }


Now watch what happens here:
   X = 3;
    if (X = 4) { <-- this SETS X to a value of 4
        print ("It's not the same!");
    } else {
        print ("It matches!"); <-- this gets printed because setting X to anything returns boolean "true"
    }


Make sense?
Eeeerm... I think you have the meaning of *true* and *false* mixed up.
What you probably meant is this:
Code:
    X = 3;
    if (X == 4) {
        print ("It matches!");
    } else {
        print ("It's not the same!"); <-- this gets printed because 3 is not equal to 4
    }

Code:
    X = 3;
    if (X = 4) { <-- this SETS X to a value of 4
        print ("It matches!"); <-- this gets printed because setting X to anything non-zero returns boolean "true"
    } else {
        print ("It's not the same!");
    }
21  Using Arduino / Programming Questions / Re: Problem assigning to structures under IDE 1.0.5 on: April 06, 2014, 07:43:47 am
PeterH has pointed out the fundamentals. C++14 will allow compound literals, and are currently supported by GCC, you can use:
Though Arduino does not use C++14 - the version of GCC used is ancient, it doesn't even support C++11.
22  Using Arduino / Microcontrollers / Re: attiny wired to pot input, but what if i use "pinMode(ButtonPin, INPUT_PULLUP)" on: April 06, 2014, 07:17:26 am
The danger is that if you have to the pot turned all the way in one direction such that you have a 0 Ohm resistance between the +5v line and the input, if you push the button you would short the power supply.

What you can do is use only two pins of the potentiometer - the wiper to the pin, and one of the other terminals to ground. Then have a 10k resistor from the pin to VCC. That way you won't short the supply and you don't need to use INPUT_PULLUP.
However, if you have the pot turned all the way in this situation, the pin will be connected directly to ground - meaning that you would never be able to register the button press as it would be indistinguishable from the pot.


This leads to a third circuit which would allow you to use both. I have attached a schematic.
Basically, if you do analogRead() you will get a number between 636 and 988 depending on where you turn the pot. If the button is pressed, analogRead() will return 0.
If you do digitalRead() when the button is pressed you will get LOW. If you do digitalRead() when the button is not pressed, you will get HIGH - because the potentiometer will never take the voltage at the pin below 3V which is the threshold for a logic 1.
23  Using Arduino / Programming Questions / Re: Data Logging Stops After Losing GPS Signal on: April 05, 2014, 04:25:15 pm
Code:
if(SD_date_time != "invalid")
    digitalWrite(GPSIndicate, HIGH);
  else (SD_date_time = "invalid");
  digitalWrite(GPSIndicate, LOW);
  {
  }

Could you rewrite that code in some meaningful fashion... because I doubt it is doing what you think it is.

This is what it does:
Code:
if(SD_date_time != "invalid"){
    digitalWrite(GPSIndicate, HIGH);
} else {
    SD_date_time = "invalid";
}
digitalWrite(GPSIndicate, LOW);

Perhaps this is what you meant:
Code:
if(SD_date_time != "invalid"){
    digitalWrite(GPSIndicate, HIGH);
} else {
    digitalWrite(GPSIndicate, LOW);
}


ALWAYS USE '{' and '}'. To not do so is only likely to lead to confusion and making the code difficult to read.




Next thing is to do with checking the 'dataFile' variable is actually valid:
Code:
if (hasrun == false)
{
  ...
  ... //you write to the file here, but haven't checked whether or not it is a valid file handle.
}

if (dataFile)
{
  ...
  ...//here you also write to file, but this time you have correctly checked it isn't a null pointer.
}

Although to be honest, I don't know why you don't open the file in the setup() and write the headers, rather than having a variable controlling an if statement which causes the code to only execute on the first pass of loop().



Code:
 if (val == invalid)
    strcpy(sz, "*******");
  else
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0)
    sz[len-1] = ' ';
Again, use the {}, they are there to make the code readable.
Secondly, I don't think you mean '++i', I think you are looking for 'i++'. Otherwise there is little point adding the spaces as sz[strlen(sz)] is a null termination that you never remove, meaning the spaces will never be included as part of your string. It also explains why you have to have the line sz[len-1] = ' '; which would not be necessary if you use the correct ++ operator. There is a big difference between "pre-increment" and "post-increment"
The same is true in your print_float function.



Code:
static void print_str(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  feedgps();
}
That seems like a very convoluted way of doing it. Try this:
Code:
static void print_str(const char *str, int len)
{
  int slen = strlen(str);
  Serial.print(str);
  for (int i=slen; i<len; i++) {
    Serial.print(' ');
  }
  feedgps();
}



Next thing to do is to ditch the 'String' class completely. It will just fragment the RAM which after extended periods of time will become a big problem. There is nowhere in your current code that you actually need to use the String class.

For example:
Code:
char SD_date_time [32];
void setup(){
   SD_date_time[0]  = '\0'; //invalid if the first character is a null termination (zero-length string)
   ...
}
...
void loop(){
...
if (SD_date_time[0] != '\0') {
  ...
}
...
...
static void print_date(TinyGPS &gps)
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
  {
    Serial.print("*******    *******    ");
    SD_date_time[0] = '\0'; //set first character to null to indicate invalid.
  }
  else
  {
    sprintf(SD_date_time, "%02d/%02d/%02d %02d:%02d:%02d   ",
    month, day, year, hour, minute, second);
    Serial.print(SD_date_time);
  }
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  feedgps();
}
...

Then this one:
Code:
String dataString;
Can be removed completely and replaced with 5 separate calls to dataFile.print(...):
Code:
//dataString = SD_date_time + "," + SD_lat + "," + SD_lon; <-- remove
 ...
   // dataFile.println(dataString); <-- remove
    dataFile.println(SD_date_time);
    dataFile.println(",");
    dataFile.println(SD_lat );
    dataFile.println(",");
    dataFile.println(SD_lon);
24  Using Arduino / Project Guidance / Re: ABC - Arduino Basic Connections on: March 31, 2014, 02:30:31 pm

Pin 8 of the MC14050B is not GND, but VSS. Does it matter?
Second, if MC14050B is not available here, which is the easyest way to connect an sd card to an Arduino (primarily not for a prototyping project).


GND = VSS. They are the same thing.

If you can't get an MC14050, look for any other Hex Buffer. One example is a CD4504. Alternatively you could use something like the TXB0104. There are many.
25  Using Arduino / Programming Questions / Re: Use ATmega328 as USB driver on: March 30, 2014, 12:14:54 pm
You can do, but it is a bit of a hack.

Google search for something called 'V-USB'.
26  Using Arduino / Programming Questions / Re: 3x3x3 LED matrix Programm (doesnt work yet) on: March 30, 2014, 12:13:39 pm
Ah, missed that.

Anyway, if you want to reduce memory consumption (both Flash, and RAM) change EVERYTHING in your code which says 'int' to be 'byte'. All of the places in the code you posted where you have used an int would be fine as a byte, but would be far less wasteful.


The next step is to forget about the Arduino functions when it comes to a microcontroller with very limited resources. Dump the digitalWrite and pinMode and digitalRead functions and use direct port manipulation instead.

I wrote a relatively simple chunk of code for an attiny24 one time and using the Arduino functions it compiled to 3kB, using direct port manipulation and dropping millis() and other Arduino rubbish, it compiled to <512bytes.
27  Using Arduino / Programming Questions / Re: 3x3x3 LED matrix Programm (doesnt work yet) on: March 29, 2014, 03:01:14 pm
An Arduino Uno has 32k of Flash memory - program space.
It has 2k of RAM - variables/heap/stack

I think you are getting the two confused again.
28  Using Arduino / Programming Questions / Re: How can I tell when a pin is receiving current? on: March 27, 2014, 02:54:00 pm
Writing a 1 to a bit in the PIN register will TOGGLE the associated bit in the PORT register - the zeros will have no effect on the rest of the bits in the PORT register.
29  Using Arduino / Programming Questions / Re: 3x3x3 LED matrix Programm (doesnt work yet) on: March 27, 2014, 02:51:39 pm
Hey, thanks for your feedback!

1. Yeah I know that integers are a waste of space. I haven't found a good solution yet though. Afaik there is no bit type in gcc/arduino, so I cant use arrays of bits or something. And a byte (which exists) has one bit less then I need. So I could use 2 bytes per level, but that would be too much too. Any solution here?
No, but a byte would contain enough bits (1) to store a number 1 or a number 0. Granted it still wouldn't be as efficient as using bitmasks for space, but changing 'int' to 'byte' would half your memory usage.

I did write a new sketch though, which (successfully) circles through the leds. It doesn't have different modes yet though, and its already 1,3k from my 2k Bytes big, cause I haven't found a good solution to store the values of the leds yet. As I said above I could use a hint here smiley
How is that sketch using 1.3kB? You know there is a difference between SRAM and Flash? (The IDE tells you about Flash usage)
30  Using Arduino / Microcontrollers / Re: program for three phase inverter on: March 23, 2014, 10:13:24 pm
Of course the PWM outputs are low-pass filtered and used to drive linear power transistors, each of which sinks current through half of a center-tapped transformer (designed as the secondary, although it's the primary here), with the center tap (fused) to the battery positive terminal.  The secondary outputs the voltage rating of the transformer "primary", so long as the "secondary" is rated for, say 25.2V or so.

Not entirely advisable, but as long as you go for a transformer rated for higher than you are using by at least a few %, you should be fine.
Pages: 1 [2] 3 4 ... 110