Pages: [1]   Go Down
Author Topic: Question regarding pointers and char.  (Read 801 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 213
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1) What is the difference between:

vw_send((uint8_t *)&msg, strlen(&msg));
vw_wait_tx();

AND

vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();

2) What is the difference between:
const char *msg  AND char msg

Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 74
Posts: 2210
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

1.
Code:
vw_send((uint8_t *)&msg, strlen(&msg));
msg is an object, '&' returns the address of the object ( pointer ), then the pointer is cast to a uint8_t type pointer.
strlen must accept a pointer.

The second version:
  the msg variable is already a pointer, it is just cast to the uint8_t type.

2.

char msg;
This creates an object of type char accessed by its alias msg
It will reside in the stack if used in a function ( automatic variable ), or in SRAM quota if in file or global scope.

char *msg;
This creates a pointer that 'can' point to a char object, however at creation it doesn't point to anything valid.

char *msg = new char[ 4 ];
This is creating the same pointer, but also assigning a newly allocated memory location to it.

Once your pointer has data, its current location can be accessed using the '*' dereference operator.
or as you can see the 'new' allocation creates 4 objects in an array.

Pointers and arrays are alike ( an array is a pointer managed by the system ).

*msg = 0; //Set first item to 0
msg[ 0 ] = 0 //Set first item to 0

*(msg + 1 ) = 5; //Set second item to 5
msg[ 1 ] = 5 //Set second item to 5

const char *msg;
This is a pointer to a constant character. You cannot use this pointer to change the value being pointed.

EDIT:

char msg;
char *msgptr = &msg;

This creates an object msg and sets its address as the pointer location for msgptr
« Last Edit: March 04, 2012, 04:28:32 am by pYro_65 » Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 213
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks alot. Is there an error in the usage of char and pointer in the code given.

Code:
//--------------------------------------------------------------------------------------
// Tx Code
// Wireless Force Sensor
// By Yaameen Faisal
//--------------------------------------------------------------------------------------

#include <VirtualWire.h>              // Include VirtualWire library for wireless transmission
int fsrPin = 1;       // define pin A0 as FSR pin
int analog[8];        // define array to get multiple analog readings
int voltage[8];       // define array to get multiple voltage readings
int j;
int awakeCount = 0, restlessCount = 0, asleepCount = 0 ;  // define and initialise counters

void setup()
{
  Serial.begin(9600);        // setup serial communication with baud rate (bits per second) to diplay on serial monitor
  vw_setup(2000);
  pinMode(fsrPin, INPUT);    // set pin A0 as input
}

void loop()
{
  for(j=0; j<=7; j++)
  {
    analog[j] = analogRead(fsrPin);                  // read pin A0, 8 times
    Serial.print("Analog Reading = ");
    Serial.print(analog[j],DEC);
   
    voltage[j] = map(analog[j], 0, 1023, 0, 5000);   // convert analog reading (0-1023) to voltage (0-5V)
    Serial.print(", Voltage in mV = ");
    Serial.print(voltage[j],DEC);
   
    if (voltage[j] >= 3500)                            //check if voltage >= 3500, if so restless counter = +1
    {
      restlessCount = restlessCount + 1;
    }
    else if (voltage[j] > 1500 && voltage[j] < 3500)  //check if 1500 < voltage < 3500, if so awake counter = +1
    {
      awakeCount = awakeCount + 1;
    }
    else if (voltage[j] > 100 && voltage[j] <= 1500)  //check if 100 < voltage <= 1500, if so asleep counter = +1
    {
      asleepCount = asleepCount + 1;
    }
   
    Serial.print(", Awake count = ");
    Serial.print(awakeCount, DEC);
    Serial.print(", Restless count = ");
    Serial.print(restlessCount, DEC);
    Serial.print(", Asleep count = ");
    Serial.println(asleepCount, DEC);
   
    delay (3000);
  }
   
    int asleepcountLed = asleepCount;      // store asleep counter value in alseepcountLED
    int awakecountLed = awakeCount;     // store awake counter value in awakecountLED
    int restlesscountLed = restlessCount;   // store restless counter value in restlesscountLED
    Serial.println("-----------------------------------------------------------------------");
   
    if (asleepcountLed > awakecountLed && asleepcountLed > restlesscountLed)         // if asleep > awake & asleep > restless, led = green colour
    {
      char data = 'g';
      vw_send((uint8_t *)&data, strlen(&data));
      vw_wait_tx();                                               
    }
   
    asleepCount = 0;       // reset asleep counter
    awakeCount = 0;       // reset awake counter
    restlessCount = 0;    // reset restless counter
   
    delay (500);
}

Code:
//--------------------------------------------------------------------------------------
// Rx Code
// Wireless Force Sensor
// By Yaameen Faisal
//--------------------------------------------------------------------------------------

#include <VirtualWire.h>                // include Virtualwire library for wireless transmission

void setup()
{
  Serial.begin(9600);        // setup serial communication with baud rate (bits per second) to diplay on serial monitor
  pinMode(4, OUTPUT);
  vw_set_rx_pin(8);
  vw_setup(2000);
  vw_rx_start();
}

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

  if (vw_get_message(buf, &buflen))
  {
    if (buf[0] == 'g')
    {
digitalWrite(4, HIGH);
    }
    else if (buf[0] != 'g')
    {
        digitalWrite(4, LOW); 
    }
  }
}
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 74
Posts: 2210
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The syntax is fine, its the subtleties that are hard to find,

strlen counts a string, which is null terminated. You pass it a single char.
Replace the strlen with a literal 1 as you know the length.

To use the strlen function you will need something like

Code:
char data[] = { 'g', '\0' };
vw_send((uint8_t *)data, strlen(data)); //no & operator as data is an array and direct array access is a pointer to the first object.
Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 213
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It doesn't seem to work either. When I use this simple code it turns the led ON. But when I use the previous code it still doesn't work. Any suggestion or reason.

Code:
//transmitter Code

#include <VirtualWire.h>

void setup()
{
  Serial.begin(9600);
  Serial.println("Activated");
  vw_setup(2000);
  vw_set_tx_pin(8);

}

void loop()
{
    char data[] = { 'b', '\0' };
    vw_send((uint8_t *)data, strlen(data));
    vw_wait_tx();
}


Code:
//receiver code

#include <VirtualWire.h>

void setup()
{
  //vw_set_ptt_inverted(true);
  vw_set_rx_pin(8);
  vw_setup(2000);
  vw_rx_start();
  pinMode(4, OUTPUT);
}

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

  if (vw_get_message(buf, &buflen))
  {
    if (buf[0] == 'a') {
digitalWrite(4, HIGH);
    }
    if (buf[0] == 'b') {
digitalWrite(4, LOW);
    }
  }
}
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 74
Posts: 2210
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

vw_get_message(buf, &buflen);

The function may expect an int for buflen, you pass its address, when converted to an integer it could be quite a large number.
« Last Edit: March 04, 2012, 06:01:31 am by pYro_65 » Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 213
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, what correction should I make? I'm not very pro in programming. At the moment I am pretty much lost and stuck. So, if you can, can you please let me know what correction I should make.

Thanks
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 213
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But that simple code does work, why is it?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 74
Posts: 2210
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

the code will fail when the buffer size is less than &buflen and the data received is more than the buffer size.
pass the actual value, not the address.

Also what isn't working, if the code you had worked.
« Last Edit: March 04, 2012, 08:28:20 am by pYro_65 » Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 213
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@pYro_65: Can you please take a look at this thread and help me out.

http://arduino.cc/forum/index.php/topic,94944.15.html
Logged

Pages: [1]   Go Up
Jump to: