PWM output run in background?

Hello everyone,

I start with a simple application using cheap rf module from ebay.
http://www.ebay.fr/itm/433Mhz-RF-Wireless-Transmitter-Receiver-Link-Kit-Modul-for-Arduino-/141253072327?pt=FR_YO_MaisonJardin_Bricolage_InstallationElectrique&hash=item20e356d5c7

I used library VirtualWire. I have 1 arduino to send 1 commande, et 1 to receive, and control the motor

The communication work well, but I have problem with the motor. I used PWM output with analogWrite, to open/close un transistor 2N2222. The motor does run, but very slowly, I guess may be the analogWrite stop when arduino listen to the command? Because I tried run directly the arduino -> transistor-> motor and it seem work well.

Thanks for any help

Post your code so that we can see what you are doing.
Do you want to control the speed of the motor or just turn it on and off ?

My code for receiver:
// receiver.pde
//
// Simple example of how to use VirtualWire to receive messages
// Implements a simplex (one-way) receiver with an Rx-B1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

#include <VirtualWire.h>

const int led_pin = 13;
const int receive_pin = 12;
const int mot1 = 3;
const int mot2 = 5;
const int sens=10;
const int centerx=127;
const int centery=128;
const int vol_pin=0;
int pwm1=0;
int pwm2=0;
void setup()
{
delay(1000);
Serial.begin(9600); // Debugging only

Serial.println("Init");
// Initialise the IO and ISR
vw_set_rx_pin(receive_pin);
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
pinMode(led_pin, OUTPUT);
}

void loop()
{
int vraw=analogRead(3);
if (vraw>740) {
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
pwm1=buf[0];
pwm2=buf[1];
if (pwm1<0){
pwm1=0;
}
if (pwm2<0){
pwm2=0;
}
digitalWrite(led_pin, HIGH); // 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*);*

  • Serial.print(' ');*

  • }*

  • Serial.println();*

  • digitalWrite(led_pin, LOW);*

  • }*

  • analogWrite(mot1,pwm1);*

  • analogWrite(mot2,pwm2);*

  • }*

  • else{*

  • digitalWrite(led_pin,HIGH);*

  • delay(10)*

  • digitalWrite(led_pin, LOW);*

  • Serial.print("Low voltage")*

  • }*
    }
    And for the transmitter:
    #include <VirtualWire.h>
    const int led_pin = 11;
    const int transmit_pin = 11;
    const int receive_pin = 2;
    const int transmit_en_pin = 3;
    const int readstick_x=1;
    const int readstick_y=0;
    const int sensivity=10;
    const int centerx=127;
    const int centery=128;
    void setup()
    {

  • // Initialise the IO and ISR*

  • Serial.begin(9600); // Debugging only*

  • Serial.println("Transmitter");*

  • 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*

  • pinMode(led_pin, OUTPUT);*
    }
    void loop()
    {

  • int rawx=analogRead(readstick_x);*

  • int rawy=analogRead(readstick_y);*

  • int valxr = map(rawx, 0, 1023, 0, 255);*

  • int valyr = map(rawy, 0, 1023, 0, 255);*

  • int valx=255-valxr;*

  • int valy=valyr-3;*

  • if (valy<0) {*

  • valy=0;*

  • }*

  • int pwm1=(valx-centerx)+(valy-centery)/sensivity;*

  • int pwm2=(valx-centerx)-(valy-centery)/sensivity;*

  • if (pwm1>255){*

  • pwm1=255;*

  • }*

  • if (pwm2>255){*

  • pwm2=255;*

  • }*

  • if (pwm1<0){*

  • pwm1=0;*

  • }*

  • if (pwm2<0){*

  • pwm2=0;*

  • }*

  • Serial.print("X=");*

  • Serial.print(pwm1);*

  • Serial.print("---Y=");*

  • Serial.println(pwm2);*

  • Serial.print("rawx");*

  • Serial.println(rawx);*

  • int aux=1;*

  • char msg[7] = {pwm1,pwm2,0,0,aux};*

  • digitalWrite(led_pin, HIGH); // Flash a light to show transmitting*
    vw_send((uint8_t *)msg, 5);

  • vw_wait_tx(); // Wait until the whole message is gone*

  • digitalWrite(led_pin, LOW);*

  • delay(100);*
    }

Thank you. Now can you please do it again using code tags so that much of the program does not go into italics because of an array subscript using i like this
for (i = 0; i < buflen; i++)
{
Serial.print(buf*);*

  • Serial.print(' ');*

  • }*

I expect that you missed that advice when you read Read this before posting a programming question ... - Programming Questions - Arduino Forum

My code for receiver:

// receiver.pde
//
// Simple example of how to use VirtualWire to receive messages
// Implements a simplex (one-way) receiver with an Rx-B1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

#include <VirtualWire.h>

const int led_pin = 13;
const int receive_pin = 12;
const int mot1 = 3;
const int mot2 = 5;
const int sens=10;
const int centerx=127;
const int centery=128;
const int vol_pin=0;
    int pwm1=0;
    int pwm2=0;
void setup()
{
    delay(1000);
    Serial.begin(9600);   // Debugging only
    

    Serial.println("Init");
    // Initialise the IO and ISR
    vw_set_rx_pin(receive_pin);
    vw_setup(2000);    // Bits per sec
    vw_rx_start();       // Start the receiver PLL running
    pinMode(led_pin, OUTPUT);
}

void loop()
{
    int vraw=analogRead(3);
    if (vraw>740) {
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    
    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
   int i;
        pwm1=buf[0];
        pwm2=buf[1];
        if (pwm1<0){
          pwm1=0;
        }
        if (pwm2<0){
          pwm2=0;
        }
        digitalWrite(led_pin, HIGH); // 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);
       Serial.print(' ');
            
   }


   Serial.println();

        digitalWrite(led_pin, LOW);
    }
    analogWrite(mot1,pwm1);
    analogWrite(mot2,pwm2);
    }
    else{
      digitalWrite(led_pin,HIGH);
      delay(10)
      digitalWrite(led_pin, LOW);
      Serial.print("Low voltage")
    }
}

And for the transmitter:

#include <VirtualWire.h>

const int led_pin = 11;
const int transmit_pin = 11;
const int receive_pin = 2;
const int transmit_en_pin = 3;
const int readstick_x=1;
const int readstick_y=0;
const int sensivity=10;
const int centerx=127;
const int centery=128;
void setup()
{
    // Initialise the IO and ISR
    Serial.begin(9600);   // Debugging only
    Serial.println("Transmitter");
    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
    pinMode(led_pin, OUTPUT);
}

void loop()
{
  int rawx=analogRead(readstick_x);
  int rawy=analogRead(readstick_y);
  int valxr = map(rawx, 0, 1023, 0, 255);
  int valyr = map(rawy, 0, 1023, 0, 255);
  int valx=255-valxr;
  int valy=valyr-3;
  if (valy<0) {
    valy=0;
  }
  int pwm1=(valx-centerx)+(valy-centery)/sensivity;
  int pwm2=(valx-centerx)-(valy-centery)/sensivity;
  if (pwm1>255){
    pwm1=255;
  }
  if (pwm2>255){
    pwm2=255;
  }
  if (pwm1<0){
    pwm1=0;
  }
  if (pwm2<0){
    pwm2=0;
  }
  
  Serial.print("X=");
  Serial.print(pwm1);
  Serial.print("---Y=");
  Serial.println(pwm2);
  Serial.print("rawx");
  Serial.println(rawx);
  int aux=1;
  char msg[7] = {pwm1,pwm2,0,0,aux};
  
  digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
  vw_send((uint8_t *)msg, 5);
  vw_wait_tx(); // Wait until the whole message is gone
  digitalWrite(led_pin, LOW);
  delay(100);
}
if (pwm1<0){
          pwm1=0;
        }

How is pwm1 going to hold a negative value?

Ah just un precaution in previous version, sorry, these mean nothing, and doesnt effect the function of arduino

What range of values do you see for pwm1 and pwm2 when you print them ?

Hello,

I got from 0 to 255 when I print them. Everything seem work fine, exept that the thrust given by motor is much weeker than expected.

My first though is that in every cycle, the arduino put some time to listen through the radio and stop the motor.

I want to make the pwm output run continuously with a given value, and I can change it when a new message arrive.

The communication is one way, and I dont care about loss package, so I'm thinking about set a timer, and only listen (for exemple) every 1 ms? What do you think?

I think you should use an oscilloscope to look at your output.

But I dont have one :frowning: ...

Circuit?
Schematic.
pencil, paper; draw it out; snap a pic and attach it