Assigning "buf" value to a variable

Hello!
I’m only a begginer in the arduino world. What i’m working on is a type of rc controller car which is controlled by 433MHz module, one way communication. I’ve made some code that sends the value of the position of the joystick to the arduino that is supposed to be on the car. I’ve got the position to print out on the serial monitor but i can’t take that value and assign it to a variable with which i would controll the speed of the car. At the moment i’m using Arduino mega 2560 for both ends of the build. Here is the code of the transmitter:

#include <VirtualWire.h>

#define Joystick_x A0
#define Joystick_y A1

uint8_t joystick[3];
uint8_t buf[VW_MAX_MESSAGE_LEN];

void setup() 
{
  Serial.begin(9600);

  vw_setup(2000);
}

void loop() 
{
  joystick[0] = map(analogRead(Joystick_x), 0, 1023, 0, 255);
  joystick[1] = map(analogRead(Joystick_y), 0, 1023, 0, 255);
  joystick[2] = 100;

  uint8_t len = sizeof(buf);
  
  vw_send(joystick, sizeof(joystick));
  vw_wait_tx();
  delay(100);
}

And of the receiver:

#include <VirtualWire.h>

uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

void setup() 
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);

  vw_setup(2000);
  vw_rx_start();
}

void loop() 
{
  uint8_t len = sizeof(buf);
  analogWrite(2, buf[0]);
  
  if(vw_get_message(buf, &buflen))
  {
    Serial.print("X = ");
    Serial.print(buf[0]);
    Serial.print(" Y = ");
    Serial.print(buf[1]);
    Serial.print(" Z = ");
    Serial.println(buf[2]);
  }
}

When i want to use the PWM for the speec control and use the buf[1] value it doesn’t work. I hear a slight buzzing sound in the motor but that’s it. I’ve also tried to assign it to a variable like so:

int powerValue = buf[0];

But that doesn’t work either.

The code may contain extra unneeded stuff because i didn’t really know how to send data via rf modules and i somehow made it work after a whole day of trying different stuff. And there was no good video on the internet explaining what i wanted to know, or they used a different module whereas mine is only one way communication.

Any help would be greatly appreciated

Have you tried to have your PWM assigned ONLY WHEN you get the data?

 #include <VirtualWire.h>

uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

void setup() 
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);

  vw_setup(2000);
  vw_rx_start();
}

void loop() 
{
  uint8_t len = sizeof(buf);
  
  if(vw_get_message(buf, &buflen)) {
    analogWrite(2, buf[0]);
    Serial.print("X = ");
    Serial.print(buf[0]);
    Serial.print(" Y = ");
    Serial.print(buf[1]);
    Serial.print(" Z = ");
    Serial.println(buf[2]);
  }
}

if you want to memorize the current pwm you can use a global variable that you assign also when you get the data.

J-M-L:
Have you tried to have your PWM assigned ONLY WHEN you get the data?

 #include <VirtualWire.h>

uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

void setup()
{
 Serial.begin(9600);
 pinMode(2, OUTPUT);

vw_setup(2000);
 vw_rx_start();
}

void loop()
{
 uint8_t len = sizeof(buf);
 
 if(vw_get_message(buf, &buflen)) {
   analogWrite(2, buf[0]);
   Serial.print("X = “);
   Serial.print(buf[0]);
   Serial.print(” Y = “);
   Serial.print(buf[1]);
   Serial.print(” Z = ");
   Serial.println(buf[2]);
 }
}



if you want to memorize the current pwm you can use a global variable that you assign also when you get the data.

Yes i have tried to set up an integer value at the beggining at the middle and the way you suggested but sadly it doesn’t work. Also if i tried something like this:

int powerValue = buf[0];

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println(powerValue);
}

It would always output a 0.

What do you see in the serial monitor with my version?

J-M-L:
What do you see in the serial monitor with my version?

For some reason now the variable gets the value of the buf[0] but the motor still doesn’t work. I just hear a slight buzzing sound. Also i changed the transmitter arduino to Uno. The code is the same on the transmitter. Here’s my code on the receiver end:

#include <VirtualWire.h>

uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;


void setup() 
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);

  vw_setup(2000);
  vw_rx_start();
}

void loop() 
{
  uint8_t len = sizeof(buf);
  
  
  if(vw_get_message(buf, &buflen))
  {
    int sp = buf[0];
    analogWrite(2, sp);
    Serial.print("SP = ");
    Serial.print(sp);
    Serial.print(" X = ");
    Serial.print(buf[0]);
    Serial.print(" Y = ");
    Serial.print(buf[1]);
    Serial.print(" Z = ");
    Serial.println(buf[2]);
  }
}

It outputs a value of the X. But i still can’t get the motor to working.

try this with the monitor at 115200 bauds

#include <VirtualWire.h>

uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;


void setup() 
{
  Serial.begin(115200);
  pinMode(2, OUTPUT);

  vw_setup(2000);
  vw_rx_start();
}

void loop() 
{
  uint8_t len = sizeof(buf);
    
  if(vw_get_message(buf, &buflen))  {
    int sp = buf[0];
    analogWrite(2, sp);
    Serial.println("-----------------------");
    Serial.print("SP = ");
    Serial.print(sp);
    Serial.print(" X = ");
    Serial.print(buf[0]);
    Serial.print(" Y = ");
    Serial.print(buf[1]);
    Serial.print(" Z = ");
    Serial.println(buf[2]);
    Serial.println("-----------------------");
  } else {
    Serial.println("no data");
    delay(100);
  }
}

what do you see? if you only see “no data” and never any X/Y/Z info then there is an issue with the data transmission

J-M-L:
try this with the monitor at 115200 bauds

#include <VirtualWire.h>

uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

void setup()
{
 Serial.begin(115200);
 pinMode(2, OUTPUT);

vw_setup(2000);
 vw_rx_start();
}

void loop()
{
 uint8_t len = sizeof(buf);
   
 if(vw_get_message(buf, &buflen))  {
   int sp = buf[0];
   analogWrite(2, sp);
   Serial.println("-----------------------");
   Serial.print(“SP = “);
   Serial.print(sp);
   Serial.print(” X = “);
   Serial.print(buf[0]);
   Serial.print(” Y = “);
   Serial.print(buf[1]);
   Serial.print(” Z = “);
   Serial.println(buf[2]);
   Serial.println(”-----------------------”);
 } else {
   Serial.println(“no data”);
   delay(100);
 }
}




what do you see? if you only see "no data" and never any X/Y/Z info then there is an issue with the data transmission

Ok. I have figured out what the problem is. The data is fine. When i plug the motor to the 5V and GND pins the motor starts fine. But when i plug it into PWM pin it just doesn’t want to start. So i figured that the current required to start it must be too large. Because the LEDs on the board fade out a bit when i plug it into 5V. So i figured i would get 5 PWM pins and connect them in parallel then i plugged in the motor but it still requires a kickstart to get it goin. Is there any way that i could increase the current output on the PWM pins or do i need a separate power source? Or would maybe a capacitor solve the issue? There is already one soldered on the motor but idk it doesn’t seem to solve the problem.

Ah... :o

The pwm pin should drive a mosfet or a motor driver
You should never power a motor from an arduino pin.best way to fry your arduino...

Always use a motor driver and a separate power supply for motors, and be sure to connect all the grounds.

J-M-L:
Ah… :o

The pwm pin should drive a mosfet or a motor driver
You should never power a motor from an arduino pin.best way to fry your arduino…

:cold_sweat: umm… yea… didn’t know that. I don’t have a motor driver at the moment so i thought i could power a motor this way. I thought that you only needed the driver for the motor to spin in another direction.

Before i even saw you reply i watched a video on the transistor as a switch. So i took a 2N3019 NPN power transistor connected the motor to 5V and the collector, emitter to GND and the base to a PWM signal and it works as i wanted it to work now. I probably didn’t quite get the base resistor right should’ve used 58 ohm one but i used 10 ohm because i don’t have anything that is closer in value to it.

Maybe i should’ve posted how i connected the arduino at the beggining but i thought it was the code that was at fault. Lesson learned i guess.
Anyways, thanks for the help! :slight_smile:

Glad you did not fry your arduino!
We have all been there and learnt by making mistakes

If you're using 433MHz for RC you are probably exceeding the allowable transmit duty cycle that is permitted in the radio regulations. Based on a typical packet transmit burst duration, you can only legally send a packet every 20-30 seconds or longer.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.