Go Down

Topic: Arduino I2C to TDA5051 to TDA5051 to another Arduino (Read 6277 times) previous topic - next topic

PaulS

Quote
but if i put into char buttonState = (LOW,DEC); the result will be 0

So don't do that. First, the comma operator may not be doing what you think it is doing. so the value stored in buttonState may not be what you think it is. If you want to store '0' or '1' in buttonState, based on LOW or HIGH, use
Code: [Select]
buttonState = HIGH + '0';
buttonState = LOW + '0';

Or explain what it is you are trying to store in buttonState.

gnusso

#16
Jul 20, 2011, 02:11 pm Last Edit: Jul 20, 2011, 02:24 pm by gnusso Reason: 1
Good Morning,
then code for the Master should like this:

Code: [Select]


#include <VirtualWire.h>
#include <Button.h>
Button sw(2, PULLUP);
#define buttonPin 2     // the number of the pushbutton pin
char buttonState = LOW + '0';   // variable for reading the pushbutton status
const int ledPin =  13;  // the number of the LED pin
const int led_pin = 11;
const int transmit_pin = 12;
const int receive_pin = 11;
const int transmit_en_pin = 3;  // What is the function ? is end code ?
void setup()
{
 vw_set_tx_pin(transmit_pin);
 vw_set_rx_pin(receive_pin);
 vw_set_ptt_pin(transmit_en_pin);
 vw_set_ptt_inverted(true); // what is the function for that ?
 vw_setup(2000); // Bits per sec
 pinMode(buttonPin, INPUT); //Initialize the button in input
}

void loop()
{
if(sw.uniquePress())
  {
    buttonState = !buttonState + '0'; // here I need to revert the value if is 0 when press the button should became 1 and viceversa
     digitalWrite(ledPin,buttonState);  
     vw_send((uint8_t *)buttonState, 1); // send out the button state ? correct ?
     vw_wait_tx(); // Wait until the whole message is gone
     delay(1000);    
  }
}


I don't know if is right ...
what i'd like to do is :

- press the button then send on --> 1
- press the button again send off --> 0
- for do that I do a "not" of my variabile but the problem if that is char and not integer ... then not of 0 is 1 but not of 1 is not 0 ...

also i don't understand that

vw_send((uint8_t *)buttonState, 1);

so vW_Send is the function for send
uint8_t * --> what does it mean ?
1 --> What does it mean ?

could explain kidly to me shortly ?

Thanks
Andrea

PaulS

First, it is important to make it clear when you are talking about the number 0 and the letter '0' (or the number 1 and the letter '1').

Second, you perhaps need to explain why you think you need to send the letter '0' or the letter '1' when the button is pressed.

Why not just make buttonState a byte variable, and send the number 0 or the number 1? As long as the receiver knows whether it is supposed to get a number or a letter, what difference does it make?

PaulS

Quote
also i don't understand that

vw_send((uint8_t *)buttonState, 1);

so vW_Send is the function for send
uint8_t * --> what does it mean ?
1 --> What does it mean ?


First, the function call should be:
Code: [Select]
vw_send((uint8_t *)&buttonState, 1);

Yes, vw_send is a function that you are calling. It takes two arguments. The first is a pointer to some bytes to send. The second is the number of bytes to send.

The &buttonState says to take the address of the variable (i.e. treat it like a pointer).

The (uint8_t *) part says that we want to cast the pointer (to int) to a pointer to byte (that's what a uint8_t is). That is, we want to treat the pointer to int as though it was a pointer to byte. Since the pointer points to the first byte of the int, we can do this.

Since the amount of data we want to send is 1 byte, that's the value that we supply for the second argument.

gnusso

then i not understood bad correct me if i wrong please ... I can declare
byte buttonState = LOW;
In this way the lenght of that I will send will be long one 1 byte ...

then when I call the function

vw_send((uint8_t *)&buttonState, 1);
uint8_t * is the cast but the cast is necessary ?

sorry it's more clear than before ...

PaulS

Quote
I can declare
byte buttonState = LOW;
In this way the lenght of that I will send will be long one 1 byte ...

The length will be 1 byte, yes.

Quote
then when I call the function

vw_send((uint8_t *)&buttonState, 1);
uint8_t * is the cast but the cast is necessary ?

Yes. The vw_send() function expects a pointer to a memory location (or an array, which is almost the same thing) of type uint8_t. You don't have a pointer to a memory location of type uint8_t.

Casts are not detrimental is used correctly (as is done here), nor do they consume extra memory, nor are they hard to understand and use. So, relax.

gnusso

"You don't have a pointer to a memory location of type uint8_t"

Yes I didn't use it, just send out via serial 1 or 0 value for turn on and off ... Then I can use my previous stament adjust with 2 things

byte buttonState = LOW;
vw_send((uint8_t *)&buttonState, 1);

and should be ok ... my expectation in this way is send out 1 when I press the button at the first time and the when I press the button again send out 0 ... correct ?

So, due the fact I don't need a cast and pointer also ...

Thanks for the support and patient :-)
Regards,
Andrea



gnusso

Hi post to you also the SLAVE code maybe could you help what I've in mind ...
So mainly I've to intercept a message sent from the MASTER and if the value of x = 1 then turn on the led else turn of but I m not sure that I've wrote correctly the code ...
Code: [Select]

#include <VirtualWire.h>

const int led_pin = 6;
const int transmit_pin = 12;
const int receive_pin = 11;
const int transmit_en_pin = 3;
const int ledPin =  8;      // the number of the LED pin that should be turn on / off when arrive the button status


void setup()
{
    delay(1000);
    Serial.begin(9600); // Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR
    vw_set_tx_pin(transmit_pin);
    vw_set_rx_pin(receive_pin);
    vw_set_ptt_pin(transmit_en_pin);
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000); // Bits per sec
    vw_rx_start();       // Start the receiver PLL running
    pinMode(ledPin, OUTPUT);
}

int x;
int i;
void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];  // I don't understand what is doing
    uint8_t buflen = VW_MAX_MESSAGE_LEN; // I don't understand what is doing
    x = vw_get_message(buf, &buflen); // so here i want to assign the value sent from the Master , im not sure that is correct in this way ...
    if (vw_get_message(buf, &buflen)) // if is arrive the message correct ?
    {
      if (x == HIGH) {     
            // turn LED on:   
            digitalWrite(ledPin, HIGH);
            Serial.println(x);
   
                    }
     else {
           // turn LED off:
            digitalWrite(ledPin, LOW);
            Serial.println(x);
           }
     

    }
}


Thanks for your support it's important for me your teaching ...

gnusso

Ok thanks for your suggestion,
then I can test also before this is a great thinks ...And SLAVE code that I've posted could be good ?

thanks
Andrea

gnusso

Ciao you was mentioning that is necessary connect tx --> rx and rx-->tx  so what i cross the connection done previous for I2c communication ... When you sad connect directly you mean without resistor ? so I've connect on digital pin or it necessary connect on analog one ?

Now the situation is that:

digital pin 12 tx --> digital pin 11 Rx  (MASTER)
digital pin 11 rx --> digital pin 12 tx   (SLAVE)

Correct ?
Maybe I can try without resistor but I worry about the result thanks,
Adnrea

gnusso

Hi now I've connected like your indication there is in plus only a button connected on the pin the master ...

when i push the button on a master i see time to time on slave that tx blink ... I've try to print on the serial monitor the value but without success ... below the code for the master and slave could check what Im wrong inside that ... Im more or less that Im try to debug it without results ...
MASTER:
Code: [Select]

#include <VirtualWire.h>
#include <Button.h>
Button sw(2, PULLUP);
#define buttonPin 2     // the number of the pushbutton pin
byte buttonState = LOW;   // variable for reading the pushbutton status
const int ledPin =  13;  // the number of the LED pin
const int transmit_pin = 12;
const int receive_pin = 11;
const int transmit_en_pin = 3;  // What is the function ? is end code ?

void setup()
{
  // Initialise the IO and ISR
   
   
   
  vw_set_tx_pin(transmit_pin);
  vw_set_rx_pin(receive_pin);
  vw_set_ptt_pin(transmit_en_pin);
  vw_set_ptt_inverted(true); // what is the function for that ?
  vw_setup(2000); // Bits per sec
  // pinMode(buttonPin, INPUT); //Initialize the button in input
 
}

void loop()
{
 
if(sw.uniquePress())
   {
     buttonState = !buttonState;
      digitalWrite(ledPin,buttonState); 
      Serial.begin(9600); // Debugging only
      Serial.println(buttonState);
      vw_send((uint8_t *)buttonState, 1); // send out the button state ? correct ?
      vw_wait_tx(); // Wait until the whole message is gone
      delay(1000);
     
   }

}



SLAVE:
Code: [Select]

#include <Button.h>

#include <VirtualWire.h>


const int transmit_pin = 12;
const int receive_pin = 11;
const int transmit_en_pin = 3;
const int ledPin =  8;      // the number of the LED pin that should be turn on / off when arrive the button status


void setup()
{
    delay(1000);
    Serial.begin(9600); // Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR
    vw_set_tx_pin(transmit_pin);
    vw_set_rx_pin(receive_pin);
    vw_set_ptt_pin(transmit_en_pin);
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000); // Bits per sec
    vw_rx_start();       // Start the receiver PLL running
    pinMode(ledPin, OUTPUT);
}

byte x;
int i;
void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];  // I don't understand what is doing
    uint8_t buflen = VW_MAX_MESSAGE_LEN; // I don't understand what is doing
    x = vw_get_message(buf, &buflen); // so here i want to assign the value sent from the Master , im not sure that is correct in this way ...
    if (vw_get_message(buf, &buflen)) // if is arrive the message correct ?
    {
      if (x == HIGH) {     
            // turn LED on:   
            digitalWrite(ledPin, HIGH);
            Serial.println(x);
   
                    }
     else {
           // turn LED off:
            digitalWrite(ledPin, LOW);
            Serial.println(x);
           }
     

    }
}


Could you give a tips ?

Thanks a lots ...
Andrea



gnusso

#26
Jul 22, 2011, 04:06 pm Last Edit: Jul 22, 2011, 04:11 pm by gnusso Reason: 1
Hi,
now I've listen your advice and then now I can work with a simple example that send "hello" ...

Now I'd like to do this 2 modify ...

-push button
    - Send 1 or 0 each time the button is pressed ...

due the fact I've note that this kind of trasmission is working with char ... what I what to send is byte ... when I press the button before I was a not of the variable like below reported

buttonstate = ! buttonstate
but now I've tried to modify the type but is not accept ...

could give some indications for to that ?

thanks and have nice week end,
Andrea

gnusso

#27
Jul 23, 2011, 10:42 am Last Edit: Jul 23, 2011, 11:08 am by gnusso Reason: 1
Sorry have you reason yesterday i didn't post the code because I was little bit tired and I've wrote the post at the fly ... so now with the code below I can send out the message "hello" only when I press the button ... what I'd like know how explain yesterday send out 1 when the button is pressed at the first time and when it's pressed again send out 0 ... when is pressed again 1 and then again 0 ... etc etc ... but I'd like to send how "byte" because I do that when the button is pressed ...

buttonState = !buttonState;

Code: [Select]

#include <VirtualWire.h>
#include <Button.h>
Button sw(2, PULLUP);
#define buttonPin 2  
byte buttonState = LOW;

void setup()
{
vw_setup(2000); // Bits per sec

pinMode(buttonPin, INPUT);
}

void loop()
{
if(sw.uniquePress())
  {
    buttonState = !buttonState;
   
    const char *msg = "hello";
    vw_send((uint8_t *)msg, strlen(msg));
    delay(400);
   }
}


but the problem is that the function send out a char ...
I've did also this test putting

vw_send((uint8_t *)buttonState, buttonState); 

but the is that is send always 0 and not alternatively 1 and 0 ...




Thanks Andrea

gnusso

Good Morning,
if you see I've tried also this

vw_send((uint8_t *)buttonState, buttonState);

this should send the buttonState that flip each time correctly ?
So right now with the receiver I've this output when I push the button:

Setup
Got: 0
Got: 0
Got: 0
Got: 0

my expectation is
Setup
Got: 0
Got: 1
Got: 0
Got: 1


and appears after each the button pressed ...

Thanks
Andrea

gnusso

Hi,
the entire code is below reported:
Code: [Select]

#include <VirtualWire.h>
#include <Button.h>
Button sw(2, PULLUP);
#define buttonPin 2 
byte buttonState = LOW;

void setup()
{
vw_setup(2000); // Bits per sec

pinMode(buttonPin, INPUT);
}

void loop()
{
if(sw.uniquePress())
   {
     buttonState = !buttonState;
   
     
     vw_send((uint8_t *)buttonState , buttonState);
     delay(400);
    }
}



The lenght of what I send should be long as the buttonState then 1 byte if i not undestand bad ....

Regards,
Andrea

Go Up