Show Posts
Pages: 1 [2] 3 4 ... 7
16  Using Arduino / Programming Questions / Re: VirtualWire or String causes arduino to hang on: December 11, 2012, 02:18:09 pm
Thanks for the tip!
So now I've changed both the transitter and receiver code as below and uploaded to both boards. However the change in behaviour seems minimal. If anything I do get a few more lines to run by in Serial Monitor before things hang again.

Code:
char Sensor1CharMsg[5];

All parts of the code work if I run them separately so I can't really tell where the problem lies. Is there something I can do to get more info on what causes the crash?

17  Using Arduino / Programming Questions / Re: VirtualWire or String causes arduino to hang on: December 11, 2012, 06:51:20 am
How are you going to fit '1', '0', '2', '3' and the trailing NULL in a 4 element array?

Your array on the receiver is too small, too.


Code:
char Sensor1CharMsg[4];

Isn't the above a 5 element array (0 1 2 3 4), or have I got it wrong?


Anyway, I don't see you using the String class, so you have some other problem that fixing the String class/tool chain is not going to correct.

Ok thank you for helping me with that. Any thoughts on what might actually cause the hangs?

18  Using Arduino / Programming Questions / VirtualWire or String causes arduino to hang on: December 11, 2012, 05:57:07 am
I've written two pieces of codes that uses VirtualWire to transport a 0-1023 integer, VirtualWire_Interger_TX and RX. This code works as far as I can tell without any problems.
But when I tried to combine the RX part with another piece of code my Arduino just started hanging after a few serial messages. From googling around a bit I've seen that there is some sort of problem with the string library at the moment and basically I wonder if I should bother with this problem or just wait to have the issue fixed in an update?

From looking at the Serial Monitor output and comparing to the working example it seems that the hangs are caused when calculating the Sensor1Data/potVal to a color.



Wireless_RGB_LED_Controller (causes Arduino to hang after 5-20 messages)
Code:
/*
 RGB LED controller
 4 modes: off, color select, color pulse and random cycle/pulse
 By Markus Ulfberg 2009-05-19
 
 Updated to Version 2 - 2010-01-13 (Not publicly released)
 Updated to Version 3 - 2011-12-14
 
 Thanks to: Ladyada, Tom Igoe and
 everyone at the Arduino forum for excellent
 tutorials and everyday help.
 
 TODO:
 1. Use millis for debounce instead of delay.
 
 */

#include <VirtualWire.h>

// LED's
int ledPin = 13;

// Define and set sensors to zero   
int Sensor1Data = 0;
int potVal = 0;

// RF Transmission container
char Sensor1CharMsg[4];

// set the ledPins
int ledRed = 10;
int ledGreen = 9;
int ledBlue = 6;

// LED Power variables
byte redPwr = 0;
byte greenPwr = 0;
byte bluePwr = 0;

void setup()
{

  // sets the digital pin as output
  pinMode(ledPin, OUTPUT);     

  // VirtualWire
  // Initialise the IO and ISR
  // Required for DR3100
  vw_set_ptt_inverted(true);
  // Bits per sec
  vw_setup(2000);

  // Start the receiver PLL running
  vw_rx_start();       

  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledBlue, OUTPUT);

  // serial for debugging purposes only
  Serial.begin(9600);
}

void loop()

  dataRX();
  colorControl();


  // DEBUG
  Serial.print("Sensor 1: ");
  Serial.print(Sensor1Data);

  Serial.print(" redPwr: ");
  Serial.print(redPwr, DEC);
  Serial.print(" greenPwr: ");
  Serial.print(greenPwr, DEC);
  Serial.print(" bluePwr: ");
  Serial.println(bluePwr, DEC);

  // End debug     
  // END DEBUG

} // END loop()

void dataRX()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  // Non-blocking
  if (vw_get_message(buf, &buflen))
  {
    int i;
    // Turn on a light to show received good message
    digitalWrite(13, true);

    // Message with a good checksum received, dump it.
    for (i = 0; i < buflen; i++)
    {           
      // Fill Sensor1CharMsg Char array with corresponding
      // chars from buffer.   
      Sensor1CharMsg[i] = char(buf[i]);
    }

    // Null terminate the char array
    // This needs to be done otherwise problems will occur
    // when the incoming messages has less digits than the
    // one before.
    Sensor1CharMsg[buflen] = '\0';

    // Convert Sensor1CharMsg Char array to integer
    Sensor1Data = atoi(Sensor1CharMsg);
   
    // Turn off light to and await next message
    digitalWrite(13, false);
  }
  delay(500);
}

// lightMode 1
void colorControl() {

  // read the potentiometer position
  potVal = Sensor1Data;

  // RED > ORANGE > YELLOW
  if (potVal > 0 && potVal < 170) {
    redPwr = 255;
    bluePwr = 0;
    greenPwr = map(potVal, 0, 170, 0, 255);
  }

  // YELLOW > LIME?? > GREEN
  if (potVal > 170 && potVal < 341) {
    greenPwr = 255;
    bluePwr = 0;
    redPwr = map(potVal, 341, 170, 0, 255);
  }

  // GREEN > TURQOUISE
  if (potVal > 341 && potVal < 511) {
    greenPwr = 255;
    redPwr = 0;
    bluePwr = map(potVal, 341, 511, 0, 255);
  }

  // TURQOUISE > BLUE 
  if (potVal > 511 && potVal < 682) {
    bluePwr = 255;
    redPwr = 0;
    greenPwr = map(potVal, 682, 511, 0, 255);
  }

  // BLUE > PURPLE
  if (potVal > 682 && potVal < 852) {
    bluePwr = 255;
    greenPwr = 0;
    redPwr = map(potVal, 682, 852, 0, 255);
  }

  // PURPLE > RED
  if (Sensor1Data > 852 && potVal < 1023) {
    redPwr = 255;
    greenPwr = 0;
    bluePwr = map(potVal, 1023, 852, 0, 255);
  }

  // Display colors
  colorDisplay();
}       

// Displays the colors when called from other functions
void colorDisplay() {
  analogWrite(ledRed, redPwr);
  analogWrite(ledGreen, greenPwr);
  analogWrite(ledBlue, bluePwr);
}


VirtualWire_Integer_RX (works)
Code:
/*

Sensor Receiver
By Markus Ulfberg 2012-07-06

Gets a sensor reading 0-1023 in a char array
from RF Transmitter unit via VirtualWire
converts char array back to integer

*/

#include <VirtualWire.h>

// LED's
int ledPin = 13;

// Sensors
int Sensor1Data;

// RF Transmission container
char Sensor1CharMsg[4];

void setup() {
  Serial.begin(9600);
 
  // sets the digital pin as output
  pinMode(ledPin, OUTPUT);     
   
    // VirtualWire
    // Initialise the IO and ISR
    // Required for DR3100
    vw_set_ptt_inverted(true);
    // Bits per sec
    vw_setup(2000);
   
    // Start the receiver PLL running
    vw_rx_start();       

} // END void setup

void loop(){
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
   
    // Non-blocking
    if (vw_get_message(buf, &buflen))
    {
int i;
        // Turn on a light to show received good message
        digitalWrite(13, true);

        // Message with a good checksum received, dump it.
        for (i = 0; i < buflen; i++)
{           
          // Fill Sensor1CharMsg Char array with corresponding
          // chars from buffer.   
          Sensor1CharMsg[i] = char(buf[i]);
}
       
        // Null terminate the char array
        // This needs to be done otherwise problems will occur
        // when the incoming messages has less digits than the
        // one before.
        Sensor1CharMsg[buflen] = '\0';
       
        // Convert Sensor1CharMsg Char array to integer
        Sensor1Data = atoi(Sensor1CharMsg);
       
       
        // DEBUG
        Serial.print("Sensor 1: ");
        Serial.println(Sensor1Data);
       
        // END DEBUG
               
        // Turn off light to and await next message
        digitalWrite(13, false);
    }
}


VirtualWire_Interger_TX (works)
Code:
/*

Sensor Transmitter
By Markus Ulfberg 2012-07-06

Takes a sensor reading 0-1023
converts it to a char array and sends
to RF receiver unit via VirtualWire 

*/

#include <VirtualWire.h>

// LED's
const int ledPin = 13;

// Sensors
const int Sensor1Pin = A2;
// const int Sensor2Pin = 3;

int Sensor1Data;
//int Sensor2Data;
char Sensor1CharMsg[4];

void setup() {

 // PinModes
 // LED
 pinMode(ledPin,OUTPUT);
 // Sensor(s)
 pinMode(Sensor1Pin,INPUT);
 
 // for debugging
 Serial.begin(9600);
 
 // VirtualWire setup
 vw_setup(2000); // Bits per sec


}

void loop() {
 
  // Read and store Sensor 1 data
  Sensor1Data = analogRead(Sensor1Pin);
 
  // Convert integer data to Char array directly
  itoa(Sensor1Data,Sensor1CharMsg,10);
 
  /*
  // DEBUG
  Serial.print("Sensor1 Integer: ");
  Serial.print(Sensor1Data);
  Serial.print(" Sensor1 CharMsg: ");
  Serial.print(Sensor1CharMsg);
  Serial.println(" ");
  delay(1000);

  // END DEBUG
  */
 
 digitalWrite(13, true); // Turn on a light to show transmitting
 vw_send((uint8_t *)Sensor1CharMsg, strlen(Sensor1CharMsg));
 vw_wait_tx(); // Wait until the whole message is gone
 digitalWrite(13, false); // Turn off a light after transmission
 delay(200);
 
} // END void loop...
19  Using Arduino / Project Guidance / Re: Human "powered" wireless indoor/outdoor bubble machine on: July 09, 2012, 03:39:37 pm
Thank you. The second link had quite a few good resources regarding home built Anemometers.
I think I even might have some or most of the parts needed.

20  Using Arduino / Project Guidance / Human "powered" wireless indoor/outdoor bubble machine on: July 08, 2012, 01:18:50 pm
I got an idea the other day when I saw an automatic bubble blowing machine.



I think it would be nice to make this remote controlled and add the fun blowing back in the game.

Concept:
Blow into fan/pipe > Arduino 1 reads sensor and sends data to Arduino 2 via wireless (RF, Wifi, bluetooth etc).
Arduino 2 starts spinning wheel with bubble-holes and a fan that focues its output through the holes. ==> Bubbles come out at a remote location.


The bubble machine shouldn't be much of an issue but how do I take a reading from someone blowing into a hole or a fan?
My first thought was to use a motor and use it's voltage output as a sensor reading but I'm a bit worried that I might break the Arduino with high currents or voltages?

My second thought is using a rotary encoder, but then I guess I would have to find one that moves freely enough to be able to move with wind force alone.

Any help on how to make a wind/blow sensor would be greatly appreciated.




21  Using Arduino / Programming Questions / Re: Converting int > String > Char* and back. For use with VirtualWire RF on: July 06, 2012, 01:42:26 pm
Well that's simple enough.
Thanks for clearing that up for me.
22  Using Arduino / Programming Questions / Re: Converting int > String > Char* and back. For use with VirtualWire RF on: July 06, 2012, 01:24:03 pm
The problem is that vw_get_message returns an array that is NOT NULL terminated. There is no reason for it to be, since it is not a char array that represents a string. It is only your interpretation of the data that makes it appear to be a string.

After the for loop that copies the data to a char array, you need to set Sensor1CharMsg[buflen] to NULL ('\0'). Then, the string functions, used by Serial.print() will know when to stop printing.

It is not necessary to set every position in the array to NULL.- only the first one after the valid data needs to be NULL.

I have tried it and does work like advertised, just one problem. I get why I do this but I don't understand how it works.
Am I right to assume that "buflen" is the size of the char array sent via VirtualWire. So wouldn't buflen be the position of the last valid data not the position after? So why doesen't the null termination overwrite the last valid data?

23  Using Arduino / Programming Questions / Re: Converting int > String > Char* and back. For use with VirtualWire RF on: July 05, 2012, 01:25:37 pm
Quote
Quote
I've managed to convert my sensor data from int to a String and then to a char array
Why use the String? You can go from int to char array (itoa()) and back (atoi()) directly.

Because I from what I read itoa() and atoi() seemed a bit complicated. But it turned out not to be that hard at all.

Quote
Code:
  Sensor1String.toCharArray(Sensor1CharMsg,(Sensor1String.length()+1));
The second argument is the number of characters that the array can hold, NOT the number of characters in the String. The String object already knows how many characters it has.

This is a bit null and void since I went with the other solution but I had problems getting only 2 digits when I were supposed to get 3 and so on. Adding +1 helped.

Quote
Code:
  for (int i = 0; i <= Sensor1String.length(); i++) {
    Serial.print(Sensor1CharMsg[i]);
  }
The array being printed here is already NULL terminated. There is no reason to print it one character at a time.
Code:
Serial.print(Sensor1CharMsg);

Also not relevant anymore but for the sake of education, thank you.

Quote
Code:
digitalWrite(13, true); // Flash a light to show transmitting
This does not flash a light. It turns it on. As anyone can see.

I know, this actually comes from the VirtualWire example code. But I've changed the comment to be more correct.

Quote
On the receiver, you need to use the atoi() function to convert Sensor1CharMsg to an int.

Thanks for pointing me in the right direction. I did however get into a problem on the receiving end that I didn't describe here first. The char array I use to store the chars that I later convert to an integer will contain 4 digits once it has been given that number for instance, I send 1023 to it and when I send 906 to it it will actually show 9063, if I go directly to 0 I still have 9063 (or something similar) since the char array never got flushed. I managed to solve that in what I hope is a good enough solution.

Here is the updated transmitter code:
Code:
#include <VirtualWire.h>


const int Sensor1Pin = A2;
// const int Sensor2Pin = 3;
const int ledPin = 13;

int Sensor1Data;
//int Sensor2Data;
char Sensor1CharMsg[4];

void setup() {

 // LED
 pinMode(ledPin,OUTPUT);
 
 // Sensor(s)
 pinMode(Sensor1Pin,INPUT);
 
 // for debuggin
 Serial.begin(9600);
 
 // VirtualWire setup
 vw_setup(2000); // Bits per sec


}

void loop() {
 
  // Read and store Sensor 1 data
  Sensor1Data = analogRead(Sensor1Pin);
 
  // Convert integer data to Char array directly
  itoa(Sensor1Data,Sensor1CharMsg,10);
 
  // DEBUG
  Serial.print("Sensor1 Integer: ");
  Serial.print(Sensor1Data);
  Serial.print(" Sensor1 CharMsg: ");
  Serial.print(Sensor1CharMsg);
  Serial.println(" ");
  delay(1000);

  // END DEBUG
 
 digitalWrite(13, true); // Turn on a light to show transmitting
 vw_send((uint8_t *)Sensor1CharMsg, strlen(Sensor1CharMsg));
 vw_wait_tx(); // Wait until the whole message is gone
 digitalWrite(13, false); // Turn off a light after transmission
 delay(200);
 
} // END void loop...


And updated receiver code:
Code:
#include <VirtualWire.h>

int ledPin = 13;

// Sensors
int Sensor1Data;

// RF Transmission container
char Sensor1CharMsg[4];

void setup() {
  Serial.begin(9600);
 
  // sets the digital pin as output
  pinMode(ledPin, OUTPUT);     
   
    // VirtualWire
    // Initialise the IO and ISR
    // Required for DR3100
    vw_set_ptt_inverted(true);
    // Bits per sec
    vw_setup(2000);
   
    // Start the receiver PLL running
    vw_rx_start();       

} // END void setup

void loop(){
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
   
    // Non-blocking
    if (vw_get_message(buf, &buflen))
    {
int i;
        // Turn on a light to show received good message
        digitalWrite(13, true);

        // Message with a good checksum received, dump it.
        for (i = 0; i < buflen; i++)
{           
          // Fill Sensor1CharMsg Char array with corresponding
          // chars from buffer.   
          Sensor1CharMsg[i] = char(buf[i]);
}
       
        // Convert Sensor1CharMsg Char array to integer
        Sensor1Data = atoi(Sensor1CharMsg);
       
        // DEBUG
        Serial.print("Sensor 1: ");
        Serial.println(Sensor1Data);
       
        // END DEBUG
       
        // Clear Sensor1CharMsg char array for coming messages
        // This needs to be done otherwise problems will occur
        // when the incoming messages has less digits than the
        // one before.
        for (i = 0; i < 4; i++)
        {
          Sensor1CharMsg[i] = 0;
        }
       
        // Turn off light to and await next message
        digitalWrite(13, false);
    }
}
24  Using Arduino / Programming Questions / Converting int > String > Char* and back. For use with VirtualWire RF on: July 05, 2012, 06:04:39 am
Hi,

I'm trying to use the VirtualWire library to send sensor data via a RF transmitter/receiver pair but I've got stuck halfway through.

I've managed to convert my sensor data from int to a String and then to a char array that I've successfully sent and received from one unit to the other. But now I'm stuck at converting the char array back to an int.

Since my programming skills are quite limited you are more than welcome to suggest another solution if one exists.

Transmitter code:
Code:
#include <VirtualWire.h>


const int Sensor1Pin = A2;
// const int Sensor2Pin = 3;
const int ledPin = 13;

int Sensor1Data;
//int Sensor2Data;
char Sensor1CharMsg[8];


void setup() {

 // LED
 pinMode(ledPin,OUTPUT);
 
 // Sensor(s)
 pinMode(Sensor1Pin,INPUT);
 
 // for debuggin
 Serial.begin(9600);
 
 // VirtualWire setup
 vw_setup(2000); // Bits per sec


}

void loop() {
 
  // Read and store Sensor 1 data
  Sensor1Data = analogRead(Sensor1Pin);
 
  // To be able to send the data over VirtualWire RF we need to convert
  // the numeric data to a String and then a char array.
  // First Integer to String...
  String Sensor1String(Sensor1Data, DEC);
 
  // ...then string to charArray
  // BUT! We have to make room for the null-termination char.
  // So we add +1 to the length of the string, when converting it.
  Sensor1String.toCharArray(Sensor1CharMsg,(Sensor1String.length()+1));
 
  // DEBUG
  Serial.print("Sensor1 Integer: ");
  Serial.print(Sensor1Data);
  Serial.print(" Sensor1 String: ");
  Serial.print(Sensor1String);
  Serial.print(" Sensor1 Length ");
  Serial.print(Sensor1String.length());
  Serial.print(" Sensor1 CharMsg: ");
  for (int i = 0; i <= Sensor1String.length(); i++) {
    Serial.print(Sensor1CharMsg[i]);
  }
  Serial.println(" ");
  delay(1000);

  // END DEBUG
 
 digitalWrite(13, true); // Flash a light to show transmitting
 vw_send((uint8_t *)Sensor1CharMsg, strlen(Sensor1CharMsg));
 vw_wait_tx(); // Wait until the whole message is gone
 digitalWrite(13, false);
 delay(200);
 
} // END void loop...

Receiver Code
Code:
#include <VirtualWire.h>

int ledPin = 13;

int Sensor1Data;
char Sensor1CharMsg[8];

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000); // Bits per sec

    vw_rx_start();       // Start the receiver PLL running

} // END void setup

void loop(){
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
int i;

        digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
Serial.print("Got: ");

for (i = 0; i < buflen; i++)
{
    Serial.print(buf[i], HEX);
            Sensor1CharMsg[i] = char(buf[i]);
    Serial.print(" ");
}
Serial.println("");
        String Sensor1String =(Sensor1CharMsg);
        Sensor1String.trim();
        Serial.print("String: ");
        Serial.println(Sensor1String);
        Sensor1String = "         ";
        digitalWrite(13, false);
    }
}
25  Using Arduino / General Electronics / Re: How to calculate what capacitors to use? on: January 16, 2012, 07:36:48 am
Ok, I think I better check with you guys anyway before I actually try and do some estimating on my own. But I think I got the general idea now.

Thank you!
26  Using Arduino / General Electronics / Re: How to calculate what capacitors to use? on: January 16, 2012, 03:06:48 am
Thanks for the quick reply. Just to see if I got things straight, I'm looking at my 78XX datasheet from ST and on page 28 there is an example circuit "Fixed output regulator " using with two caps. The input bypass capacitor is specified at 0.33µF and the output as 0.1µF. The thing that confuses me is that these value seem very far from the two 10µF caps in Tom Igoes example.

You state that the values are not that critical but really 10µF and 0.33µF seems like quite a stretch!
27  Using Arduino / General Electronics / How to calculate what capacitors to use? on: January 16, 2012, 02:14:30 am
Hi,

I'm just about to make my first standalone Atmel/Arduino application and there's one thing I'd like to know before I go ahead and solder things in place.
In Tom Igoe's example on standalone Atmega/Arduino and in the book "Making things talk" he uses capacitors togheter with the 7805 voltage regulator and in a number of other places. These are referred to as "smoothing" out voltage drops. When I've googled around I've seen a 7805 voltage regulator with a 10µF cap across input and ground and 10µF between output and ground. But there has also been an instance with a 1µF between output and ground. And in the 5v circuits all sorts of small caps are used.

So my questions is how do I calculate what capacitor to use when I want to smooth things out?

Preemptive thanks to everyone who can help me or point me towards good info elsewhere.

28  Forum 2005-2010 (read only) / Scandinavia / Re: KonungR on: October 10, 2009, 03:37:23 am
Och Götlaborg såklart!
29  Forum 2005-2010 (read only) / Interfacing / Re: Controlling IKEA's DIODER on: May 10, 2010, 07:21:44 am
I think I'll have leave that to someone else. I haven't really had the time to fiddle with electronics since the birth of my daughter in late december so I'm way to rusty to even start thinking about that.

Good luck!
30  Forum 2005-2010 (read only) / Interfacing / Re: Controlling IKEA's DIODER on: April 17, 2010, 03:52:13 am
Yes you could, but you shouldn't. The problem is, if I recall correctly, that the Arduino might not be able to handle the current needed by the LED-strip.

However you can connect the arduino to the same power source as the LED-strip as I described previously.

Pages: 1 [2] 3 4 ... 7