Go Down

Topic: VirtualWire or String causes arduino to hang (Read 4 times) previous topic - next topic

Nick Gammon

Code: [Select]

  for (i = 0; i < buflen; i++)
    {           
      Sensor1CharMsg[i] = char(buf[i]);
    }


Oh yes I see. That loop is the issue. Maybe a memcpy would be simpler. And choose the length as the destination length, not the source length.

dc42

...or convert the data directly from 'buf' instead of copying it to Sensor1CharMsg.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Makkan

Thanks for all the suggestions but I'm afraid I'm still going nowhere here.


What about the Serial.print lines? If you remove all those, does the program work?


Nope that didn't work.


Oh yes I see. That loop is the issue. Maybe a memcpy would be simpler. And choose the length as the destination length, not the source length.


This I have to research more if I should go ahead and do. So I'll put this on hold if the next one is simpler.


...or convert the data directly from 'buf' instead of copying it to Sensor1CharMsg.


I tried this but that didn't work. Allthough I doubt I did it right. Am I looking for a char to integer converter or something else?
Code: [Select]
Sensor1Data = atoi(buf);

Error Message
Code: [Select]
conversion from 'uint8_t*' to 'const char*'
rgb_mixer_RF_controlled_pulse_makkan_v4_ino:115: error: initializing argument 1 of 'int atoi(const char*)'



Nick Gammon


Thanks for all the suggestions but I'm afraid I'm still going nowhere here.


Please post your amended code.

Makkan

Code: [Select]
/*
  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;

// Sensors
int Sensor1Data;
int potVal;

// RF Transmission container
char Sensor1CharMsg[5];

// 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(" potVal: ");
  Serial.print(potVal);
 
  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);

        /* Commented out for buffer to integer directly
       
        // 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);
        */
       
        // Convert buffer directly to Integer String
        Sensor1Data = atoi(buf);
       
        // 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);
}

Go Up