Author Topic: analogWrite bug(with RFduinoGZLL)  (Read 2684 times)

zongjiun

  • RFduino Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
analogWrite bug(with RFduinoGZLL)
« on: March 02, 2015, 10:34:25 PM »
analogWrite will break when RFduinoGZLL.h is include in a program(with  RFduino_2.2.4 & arduino 1.6 ).
The analogwrite will always make the wrong PWM signals.
please help me
Here is my
Code:

#include <RFduinoGZLL.h>

device_t role = DEVICE3;
// pin 2 on the RGB shield is the red led
int led1 = 2;
// pin 3 on the RGB shield is the green led
int led2 = 3;
// pin 4 on the RGB shield is the blue led
int led3 = 4;

int rr,gg,bb=0;
void setup() {
  //Serial.begin(57600);
  // setup the leds for output
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT); 
  pinMode(led3, OUTPUT);

  RFduinoGZLL.begin(role);
}



void RFduinoGZLL_onReceive(device_t device, int rssi, char *data, int len)
{
    if (len >= 4)
  {
    uint8_t r = data[0]-1;
    uint8_t g = data[1]-1;
    uint8_t b = data[2]-1;
    uint8_t m = data[3]-1;
if(rr!=r || gg!=g || bb!=b){
rr=(int)r;
gg=(int)g;
bb=(int)b;
/*
Serial.print("r=");
Serial.println(r);
Serial.print("g=");
Serial.println(g);
Serial.print("b=");
Serial.println(b);
Serial.print("m");
Serial.println(m);
*/
    analogWrite(led1, r);
    analogWrite(led2, g);
    analogWrite(led3, b);
    }
  }
}

void loop() {

  // switch to lower power mode
  delay(100);
  // request the state from the Host (send a 0 byte payload)
  RFduinoGZLL.sendToHost(NULL, 0);
}

tolson

  • Global Moderator
  • *****
  • Posts: 870
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: analogWrite bug(with RFduinoGZLL)
« Reply #1 on: March 03, 2015, 02:06:31 PM »
Where is you HOST code? Don't know what you are sending from the HOST.

tolson

  • Global Moderator
  • *****
  • Posts: 870
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: analogWrite bug(with RFduinoGZLL)
« Reply #2 on: March 03, 2015, 02:42:35 PM »
And you haven't explained what you don't think is working. I wrote a simple HOST sketch to send 4 bytes. And your code works as expected IF we get rid of your subtracting 1 from each of the data members. I don't know why you are doing that.

Code: [Select]

#include <RFduinoGZLL.h>

device_t role = HOST;

char ledData[4];

void setup()
{
  ledData[0] = 0x00;
  ledData[1] = 0x00;
  ledData[2] = 0x00;
  ledData[3] = 0x00;
 
  RFduinoGZLL.begin(role);
}


void loop()
{
  ledData[0] = 0x2c;
  delay(1000);
  ledData[0] = 0x00;
  delay(1000);
  ledData[1] = 0x2c;
  delay(1000);
  ledData[1] = 0x00;
  delay(1000);
  ledData[2] = 0x2c;
  delay(1000);
  ledData[2] = 0x00;
  delay(1000);
 
}

void RFduinoGZLL_onReceive(device_t device, int rssi, char *data, int len)
{

  if (device == DEVICE3)
    RFduinoGZLL.sendToDevice(device, ledData, 4);
}
« Last Edit: March 03, 2015, 02:51:48 PM by tolson »

zongjiun

  • RFduino Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: analogWrite bug(with RFduinoGZLL)
« Reply #3 on: March 16, 2015, 03:50:48 AM »
HOST
Code: [Select]
#include <RFduinoGZLL.h>

device_t role = HOST;
int a=0;
void setup() {

  Serial.begin(115200);
  RFduinoGZLL.txPowerLevel = +4;
  RFduinoGZLL.hostBaseAddress = 0x12345677;    // default host base address is 0x0D0A0704;
  RFduinoGZLL.deviceBaseAddress = 0x87654322;  // default device base address is 0x0E0B0805;
  RFduinoGZLL.begin(role);
}

void loop() {
  // put your main code here, to run repeatedly:
if(Serial.available()>0){
char c=Serial.read();
Serial.print(c);
if(c==49){
a=1;
}
if(c==50){
a=2;
}
if(c==51){
 a=3;
}
if(c==52){
a=4;
}
delay(200);
RFduinoGZLL.sendToDevice(DEVICE1,a);
}
}
void RFduinoGZLL_onReceive(device_t device, int rssi, char *data, int len)
{

}

void loop()
{
  ledData[0] = 0x2c;
  delay(1000);
  ledData[0] = 0x00;
  delay(1000);
  ledData[1] = 0x2c;
  delay(1000);
  ledData[1] = 0x00;
  delay(1000);
  ledData[2] = 0x2c;
  delay(1000);
  ledData[2] = 0x00;
  delay(1000);
 
}

void RFduinoGZLL_onReceive(device_t device, int rssi, char *data, int len)
{

  if (device == DEVICE3)
    RFduinoGZLL.sendToDevice(device, ledData, 4);
}
[/quote]

zongjiun

  • RFduino Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: analogWrite bug(with RFduinoGZLL)
« Reply #4 on: March 16, 2015, 03:54:10 AM »
the analogWrite always make the wrong color
I don't know where is the problem...


DEVICE

Code: [Select]



#include <RFduinoGZLL.h>

device_t role = DEVICE1;
int g1=0;
int r1=60;
int r2=125;
int g2=150;

int r3=120;
int g3=150;
int b3=150;

int change3 = 10;

int change2 = 5;
int change22 =2 ;
int change1 = 2;
int change11 = 2;
int sw=1;
// pin 2 on the RGB shield is the red led
int led1 = 2;
// pin 3 on the RGB shield is the green led
int led2 = 3;
// pin 4 on the RGB shield is the blue led
int led3 = 4;

int rr,gg,bb=0;
void setup() {
  Serial.begin(57600);
  // setup the leds for output
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT); 
  pinMode(led3, OUTPUT);
 RFduinoGZLL.txPowerLevel = +4;
  RFduinoGZLL.hostBaseAddress = 0x12345677;    // default host base address is 0x0D0A0704;
  RFduinoGZLL.deviceBaseAddress = 0x87654322;  // default device base address is 0x0E0B0805;
  RFduinoGZLL.begin(role);
}



void RFduinoGZLL_onReceive(device_t device, int rssi, char *data, int len)
{
  if (len > 0)
  {
    // update the Green led with the current state on the Host
    int state = data[0];
    leddd(state);
  }
}


void leddd(int state){
if(state==1){
    r1=r1-change1;
    g1=g1+change11;
    analogWrite(led1, r1);
    analogWrite(led2, g1);
    analogWrite(led3, 255);
if (r1 == 0 || r1 == 60){
    change1 = -change1;
    change11 = -change11;
   
  }
}
if(state==2){
    r2=r2+change2;
    g2=g2-change22;
    analogWrite(led1, r2);
    analogWrite(led2, g2);
    analogWrite(led3, 10);

    if (r2 == 125 || r2 == 250){
    change2 = -change2;
     change22 = -change22;
   
  }
}
if(state==3){
  switch(sw){
    case 1:
    r3=r3+change3;
      if (r3 == 220){
         change3 = -change3;
         }
      if (r3 == 120){
         change3 = -change3;
         sw=2;
         } 
    break;
    case 2:
     r3=r3+change3;
     g3=g3+change3;
      if (g3 == 250){
         change3 = -change3;
         }
      if (g3 == 150){
         change3 = -change3;
         sw=3;
         } 
    break;
    case 3:

     b3=b3+change3;
      if (b3 == 250){
         change3 = -change3;
         }
      if (b3 == 150){
         change3 = -change3;
         sw=1;
         } 
     break;
  }
Serial.println(sw);
Serial.print("r=");
Serial.println(r3);
Serial.print("g=");
Serial.println(g3);
Serial.print("b=");
Serial.println(b3);

    analogWrite(led1, r3);
    analogWrite(led2, g3);
    analogWrite(led3, b3);
 
}
if(state==4){
    int r4=random(10,255);
    int g4=random(10,255);
    int b4=random(10,255);
    analogWrite(led1, r4);
    analogWrite(led2, g4);
    analogWrite(led3, b4);
}
}


void loop() {
  // switch to lower power mode
  delay(200);
  // request the state from the Host (send a 0 byte payload)
  RFduinoGZLL.sendToHost(NULL, 0);
}
[/quote]

quasio

  • RFduino Newbie
  • *
  • Posts: 5
  • Karma: +1/-0
    • View Profile
Re: analogWrite bug(with RFduinoGZLL)
« Reply #5 on: March 25, 2015, 04:08:14 PM »
I've also found this analogWrite bug. (i'm not using RFduinoGZLL)

It seems that going to 255, then to another number, say 254, the actual PWM out will be significantly smaller. 

This is when 3 analogWrites are enabled, i think the bug solves itself when you use 2 or less analogwrites.

I believe there is a timer usage issue in the analogWrite code, when moving from full on to something else. ( haven't looked into the libs deeply)

I've sent this directly to mkay but haven't received any response as yet.
A workaround i've found is to never use 255, and just use 254