Show Posts
Pages: 1 [2] 3 4 5
16  Forum 2005-2010 (read only) / Syntax & Programs / Re: Read from file on: April 28, 2008, 01:34:35 pm
I can't remember exactly, but I thought the file objects were in the header file "fstream".
17  Forum 2005-2010 (read only) / Syntax & Programs / Re: How do I write a Class in Arduino Language on: April 27, 2008, 03:18:15 pm
Here's an example of a class:

Code:
class Button {
private:
  byte down_at;
  byte down;
  
public:
  Button();
  byte downFor();
  void resetDown();
  bool isDown();
  void turnOn();
  void turnOff();
};

Button::Button()
{
  this->down_at = 0;
  this->down = 0;
}

// Find the number of beats the button has been down for. Function can compute max. 8 sec down before rolling over.
byte Button::downFor()
{
  byte time;
  if (this->down_at > sc_beat)
  {
   time = (sc_beat + 256) - this->down_at;
  }
  else
  {
    time = sc_beat - this->down_at;
  }
  return time;
}

bool Button::isDown()
{
  return (bool)this->down;
}

void Button::resetDown()
{
  this->down_at = sc_beat;
  this->down = 1;
}
  
void Button::turnOn()
{
  if (!this->down)
  {
    this->down_at = sc_beat;
    this->down = 1;
  }
  return;
}

void Button::turnOff()
{
  this->down = 0;
  return;
}
18  Forum 2005-2010 (read only) / Syntax & Programs / Embedding Assembly in C++ on: April 04, 2008, 11:46:08 pm
I'd like to use some assembler directly in my sketch for my ISRs (It's not that they're running slowly, it's just that I'd like to try it out), and have done some poking about the internet for assistance on this topic. I seem to have found the basics on how to do it syntactically, but there's a couple things that I can't seem to find.

Is it possible to reference global variables from the ASM? What would the syntax for this be? Can I use them as addresses, and load them into a register as if it was defined in the ASM part of the program?
Can anyone provide any tips/pointers for this? Thanks!
19  Forum 2005-2010 (read only) / Syntax & Programs / Re: Newb Q re. interrupts & timers on: April 19, 2008, 05:03:36 pm
You can use the External interrupts 0 and 1, available on digital pins 2 and 3 respectively. These are the two interrupts that use the attachInterrupt function. When you call it, it will automatically enable the interrupts for those two vectors.
You can set the interrupts to trigger on the falling or rising edge of the signal, or when it is low. The datasheet describes them as INT0 and INT1, starting from page 67.
20  Forum 2005-2010 (read only) / Syntax & Programs / Re: Non-Int Storage in EEPROM. on: April 17, 2008, 04:19:46 pm
You can use a union to access each byte of the 4-byte float. A union is similar to a struct, except that it only allocates enough space to contain the largest single type that is defined inside it. Here's an example of how to copy a float byte by byte from the original to another, using C++ on a PC.

Code:
#include <iostream>
using namespace std;

union bitFloat {
      float float_num;
      char byte[4];
};

int main()
{
      bitFloat test;
      test.float_num = 10.5f;
      bitFloat test2;
      test2.byte[0] = test.byte[0];
      test2.byte[1] = test.byte[1];
      test2.byte[2] = test.byte[2];
      test2.byte[3] = test.byte[3];

      cout << test.float_num << endl;
      cout << test2.float_num << endl;

      getchar();
      return 0;
}

The important thing to realise is that the bitFloat type defined is only 4 bytes, and you have two ways of accessing its data - one through the float type test.float_num or through the byte array, test.byte.
So, the practical upshot of all this is that you can store your floating point numbers in EEPROM, and read them out byte by byte, and use them to build up your whole float variable. (floats and doubles are both 4 bytes on the AVR cores.) The only thing you might have to worry about is endian-ness... which order the bytes need to be stored in. Unfortunately I don't know myself what the AVRs are. ):
21  Forum 2005-2010 (read only) / Syntax & Programs / Re: serial port and software serial!!!! on: April 14, 2008, 08:54:35 pm
It sounds like you have your pins swapped. Try connecting your modem's TX to the Arduino RX, and Arduino TX to the modem's RX.
22  Forum 2005-2010 (read only) / Syntax & Programs / Re: Debugging errors.. Returning an array from a F on: April 01, 2008, 03:24:00 pm
Heh. My long-winded version of my post had that. >_<;
23  Forum 2005-2010 (read only) / Syntax & Programs / Re: Debugging errors.. Returning an array from a F on: March 31, 2008, 07:13:51 pm
Your array syntax was not correct, that's why you the first sketch you posted didn't compile. Namely, the compiler didn't like

return dd[];

Arrays are quite a bit different than regular values in C/C++. You cannot return several of them from a function. The compiler was expecting a number between those brackets, seeing as it was looking for an integer to return.

Also keep in mind that arrays are not passed by value (using copies of its arguments) they are passed by reference (using the variables the calling function uses). That is to say, any change you make to items in the array in a function will be reflected in the same array that the calling function passed into it - the fallout of which is that you don't have to return the array at all! You can make changes to it in a function, and it gets changed as far as the calling function can tell. So we can rewrite your naughty function as this:

Code:
//takes an array and appends the current distance at the end of it, moving all values and deleting the first
void AppendDistance (int dd[], int arrayVals){
// dd is being passed by reference - any change we make here will be reflected in the calling function.
      int i = 0;

      for(i=0;i<arrayVals;i++){                  //should this be i<arrayVals or i<=arrayVals
            if (i=1){
                  dd[1]=dd[2];
            }
            else if (i=arrayVals){
                  dd[i] = GetDistance();
            }
            else{
            dd[i]=dd[i+1];
            }
       }
         // Don't return anything, since we've already changed dd, and the calling function will know about it.
}

I hope that helps you understand why it didn't quite work the first time!  smiley-wink  If you're new to C(++), it might be a good idea to find a couple tutorials on arrays, and pointers as well. They're actually quite closely related, and knowing a bit about pointers will help you return arrays from functions if you need to. (:
24  Forum 2005-2010 (read only) / Syntax & Programs / Re: Syntax Error causing boolean / byte to be unkn on: April 01, 2008, 11:45:59 pm
Try replacing 'boolean' with 'bool'.
25  Forum 2005-2010 (read only) / Syntax & Programs / Re: create a 'wave' with 5 Leds on: April 02, 2008, 12:00:25 am
After reading through your code (albeit rather quickly), I'm surprised that any light beside the one on pin 5 comes on at all.
I did something similar with only 3 LEDs, and a potentiometer to control the speed a while back. I'll post up the code, but be warned, it doesn't contain many comments. : |

Code:
int led_blue = 9;
int led_green = 10;
int led_red = 11;

int pot_pin = 0;

int blue_val = 0;
int green_val = 0;
int red_val = 255;

int fade_val = 0;
int temp = 0;

void setup()
{
  pinMode(led_blue, OUTPUT);
  pinMode(led_green, OUTPUT);
  pinMode(led_red, OUTPUT);
  
  //Serial.begin(9600);
}

void loop()
{
  analogWrite(led_blue, blue_val);
  analogWrite(led_green, green_val);
  analogWrite(led_red, red_val);
  
  temp = analogRead(pot_pin);
  
  temp = temp / 20 + 1;
  
  if (temp != fade_val)
  {
    //Serial.print("Fade changed from ");
    //Serial.print(fade_val, DEC);
    //Serial.print(" to: ");
    //Serial.println(temp, DEC);
    
    fade_val = temp;
  }
  
  if (red_val == 255)
  {
    if (blue_val > 0)
    {
      blue_val--;
    }
    else if (green_val < 255)
    {
      green_val++;
      if (green_val == 255)
      {
        red_val--;
      }
    }
  }
  else if (green_val == 255)
  {
    if (red_val > 0)
    {
      red_val--;
    }
    else if (blue_val < 255)
    {
      blue_val++;
      if (blue_val == 255)
      {
        green_val--;
      }
    }
  }
  else if (blue_val == 255)
  {
    if (green_val > 0)
    {
      green_val--;
    }    
    else if (red_val < 255)
    {
      red_val++;
      if (red_val == 255)
      {
        blue_val--;
      }
    }
  }
  
  delayMicroseconds(fade_val * 10);
}

Basically what it's doing is every iteration of the loop, it will write the values computed last go-round to the PWM pins, and then figure out which value needs to increment/decrement. Hopefully it'll help you figure out what's going on with yours. smiley-kitty;
26  Forum 2005-2010 (read only) / Syntax & Programs / Re: How to send a decimal to a 16X2 LCD on: March 31, 2008, 06:55:54 pm
This probably won't work for huge/tiny values, but what I would do would be similar to:

Cast the float to an int (truncate anything past the decimal) and write that to the LCD.
Print a period '.' to the LCD.
Subtract off the integer part of the float, and multiply by whatever number to get the desired precision.

Hope that helps!
27  Forum 2005-2010 (read only) / Syntax & Programs / Re: non volatile data on: February 23, 2008, 08:04:40 pm
Could you not use a battery-backed SRAM chip or something as opposed to EEPROM?

The other thing you may consider is only saving the position after a more substantial delay, say once a minute, and that would easily lengthen the expected life of your EEPROM chip.
28  Forum 2005-2010 (read only) / Syntax & Programs / Re: Problem measuring acceleration on: February 16, 2008, 10:32:53 pm
I would probably do something similar to what you suggest as your 'initial thought'. If all you're looking to do is measure acceleration, that is probably your best route as long as you can guarantee that for the start-up time that the device will be still.
Quote
The problem is, what if the device stops and is on an incline.  It will assume that there's movement.
Is this going to cause a problem? Maybe you're after something like a gyroscope? If the thing stops moving, it doesn't matter what orientation it's in, it's still not going to have any motion... Perhaps what you should do is during your initial setup time, figure out what sort of readings the accelerometer is giving, and set that as 1G (You might have to do this across both axis using a bit of trig). Any time the total reading is out with this amount is your acceleration.

Hope that helps some.  :-X
29  Forum 2005-2010 (read only) / Interfacing / Re: LCD4Bit lib on: April 13, 2008, 03:55:04 am
I found a couple strange bugs in the LCD4Bit library that was posted on the playground... It was a while ago though, but I think one of the bits got stuck somewhere along the line... though that might have been the version for using an I/O expander as well. Hmm...
30  Forum 2005-2010 (read only) / Interfacing / Re: Arduino with c sharp ? on: March 25, 2008, 12:47:46 pm
You'll have to upload a sketch to the Arduino that will be able to recognize the serial commands being sent to it as well. Unfortunately, I'm not very familiar with C#, so I can't help with that.
Pages: 1 [2] 3 4 5