integrating shiftPWM and Wire library

dear all,
this is the first post on this forum,
I'm designing a lamp with some rgb led (using shiftPWM library and shift registers) and using the TEA5767 FM radio module (using the Wire library);
I've wrote separately the software to control the lights and the radio tuning,
but if I combine both the radio component doesn't works:
it is like if it receives at every cicle commands to change frequency, so the only output is white noise...

can you please help me?

thank you

Gianpaolo

can you please help me?

Only if you post your code in code tags.

sure ,
sorry it was my first post :slight_smile:

here is the code:
if I remove the part related to shiftPWM the code works and the I can hear the radio,
with shiftPWM I can hear only white noise

const int ShiftPWM_latchPin=8;
#define buttonSET 7
#define buttonMode 2
#define trim1 A3
#define trim2 A2
#define audioLeft A1
#define audioRight A0
unsigned char frequencyH = 0;
unsigned char frequencyL = 0;
unsigned int frequencyB;
double frequency = 103.4;

const bool ShiftPWM_invertOutputs = true;
const bool ShiftPWM_balanceLoad = true;

#include <ShiftPWM.h> // include ShiftPWM.h after setting the pins!
#include <Wire.h>

unsigned char maxBrightness = 255;
unsigned char pwmFrequency = 100;
int numRegisters = 3;
int numRGBleds = numRegisters*8/3;
int audioVal=0;

void setup(){
Serial.begin(9600);
Wire.begin();
frequency = 93.0; //starting frequency
setFrequency();

// Sets the number of 8-bit registers that are used.
ShiftPWM.SetAmountOfRegisters(numRegisters);

// SetPinGrouping allows flexibility in LED setup.
// If your LED's are connected like this: RRRRGGGGBBBBRRRRGGGGBBBB, use SetPinGrouping(4).
ShiftPWM.SetPinGrouping(1); //This is the default, but I added here to demonstrate how to use the funtion

ShiftPWM.Start(pwmFrequency,maxBrightness);

//setup button and triggers
//define analog inputs
pinMode(trim1,INPUT);
pinMode(trim2,INPUT);
pinMode(buttonSET,INPUT);
pinMode(buttonMode,INPUT);
pinMode(audioLeft,INPUT);
pinMode(audioRight,INPUT);
//digitalWrite(A4,LOW);
//digitalWrite(A5,LOW);

digitalWrite(buttonSET,LOW);
digitalWrite(buttonMode,LOW);
trim1Val=analogRead(trim1);
trim2Val=analogRead(trim2);
audioLeftVal=analogRead(audioLeft);
audioRightVal=analogRead(audioRight);

buttonSETVal=digitalRead(buttonSET);
buttonModeVal=digitalRead(buttonMode);
buttonModeState=0;

buttonSETState=0;

mode=0;
attachInterrupt(0, changeMode, RISING);
newmicros=micros();
ShiftPWM.PrintInterruptLoad();

}

void loop()
{

ShiftPWM.SetAllRGB(200,200,200);

Serial.println("radio");

if (trim1Val==1)
frequency=frequency+0.05;
if (trim2Val==1)
frequency=frequency-0.05;
if (frequency>108)
frequency=108;
if (frequency<87.5)
frequency=87.5;

delay(50);
if (oldFrequency!=frequency)
setFrequency();
Serial.println(frequency);
}

void setFrequency()
{
frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
frequencyH = frequencyB >> 8;
frequencyL = frequencyB & 0XFF;
delay(100);
Wire.beginTransmission(0x60);
Wire.write(frequencyH);
Wire.write(frequencyL);
Wire.write(0xB0);
Wire.write(0x10);
Wire.write((byte)0x00);
Wire.endTransmission();
delay(100);
}

Grumpy_Mike:
code in code tags.

sure ,
sorry it was my first post :slight_smile:

here is the code:
if I remove the part related to shiftPWM the code works and the I can hear the radio,
with shiftPWM I can hear only white noise

const int ShiftPWM_latchPin=8;
#define buttonSET 7
#define buttonMode 2
#define trim1 A3
#define trim2 A2
#define audioLeft A1
#define audioRight A0
unsigned char frequencyH = 0;
unsigned char frequencyL = 0;
unsigned int frequencyB;
double frequency = 103.4;


const bool ShiftPWM_invertOutputs = true; 
const bool ShiftPWM_balanceLoad = true;

#include <ShiftPWM.h>   // include ShiftPWM.h after setting the pins!
#include <Wire.h>


unsigned char maxBrightness = 255;
unsigned char pwmFrequency = 100;
int numRegisters = 3;
int numRGBleds = numRegisters*8/3;
int audioVal=0;

void setup(){
  Serial.begin(9600);
  Wire.begin();
  frequency = 93.0; //starting frequency
  setFrequency();
 
  // Sets the number of 8-bit registers that are used.
  ShiftPWM.SetAmountOfRegisters(numRegisters);

  // SetPinGrouping allows flexibility in LED setup. 
  // If your LED's are connected like this: RRRRGGGGBBBBRRRRGGGGBBBB, use SetPinGrouping(4).
  ShiftPWM.SetPinGrouping(1); //This is the default, but I added here to demonstrate how to use the funtion
  
  ShiftPWM.Start(pwmFrequency,maxBrightness);
  
  //setup button and triggers
  //define analog inputs
   pinMode(trim1,INPUT);
   pinMode(trim2,INPUT);
   pinMode(buttonSET,INPUT);
   pinMode(buttonMode,INPUT);
   pinMode(audioLeft,INPUT);
   pinMode(audioRight,INPUT);
   //digitalWrite(A4,LOW);
   //digitalWrite(A5,LOW);
   
   digitalWrite(buttonSET,LOW);
   digitalWrite(buttonMode,LOW);
   trim1Val=analogRead(trim1);
   trim2Val=analogRead(trim2);
   audioLeftVal=analogRead(audioLeft);
   audioRightVal=analogRead(audioRight);

   buttonSETVal=digitalRead(buttonSET);
   buttonModeVal=digitalRead(buttonMode);
   buttonModeState=0;

   buttonSETState=0;

   mode=0;
   attachInterrupt(0, changeMode, RISING);
   newmicros=micros();
 ShiftPWM.PrintInterruptLoad();
 
}


void loop()
{ 

ShiftPWM.SetAllRGB(200,200,200);

 Serial.println("radio");
         
         if (trim1Val==1)
           frequency=frequency+0.05;
         if (trim2Val==1)
           frequency=frequency-0.05;
         if (frequency>108)
           frequency=108;
         if (frequency<87.5)
           frequency=87.5;
           
           
           
           delay(50);
           if (oldFrequency!=frequency)
             setFrequency();
           Serial.println(frequency);
}


void setFrequency()
{
  frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
  frequencyH = frequencyB >> 8;
  frequencyL = frequencyB & 0XFF;
  delay(100);
  Wire.beginTransmission(0x60);
  Wire.write(frequencyH);
  Wire.write(frequencyL);
  Wire.write(0xB0);
  Wire.write(0x10);
  Wire.write((byte)0x00);
  Wire.endTransmission();
  delay(100);  
}

Thanks for posting in tags.
Now the important thing is to post ALL your code. In that way we can copy it and try it on our own systems.

You have not posted all your code. There are at least two lines missed out, the ones that include the libraries. Without those we can't compile and it is a waste of time guessing that you might have done it correctly.

One thing to test with your system.
Include the shiftPWM code but disconnect the shift register itself. Does it still fail then?

Hi,
sorry ,
I've wrote a simplified version but forgot something :slight_smile: here is the full simplified code

I'll try to disconnect the shift registers too, thanks for your suggestion

const int ShiftPWM_latchPin=8;
#define buttonSET 7
#define buttonMode 2
#define trim1 A3
#define trim2 A2
#define audioLeft A1
#define audioRight A0
unsigned char frequencyH = 0;
unsigned char frequencyL = 0;
unsigned int frequencyB;
double frequency = 103.4;
double oldFrequency=frequency;
int trim1Val=0;
int trim2Val=0;


//digital button variables
int buttonSETVal=0;
int buttonSETState=0;
int buttonModeVal=0;
int buttonModeState=0;
int buttonModeChanged=0;
float newmicros;

int mode=0;
int scala=0;
int hue=0;
int freq=0;

const bool ShiftPWM_invertOutputs = true; 
const bool ShiftPWM_balanceLoad = true;

#include <ShiftPWM.h>   // include ShiftPWM.h after setting the pins!
#include <Wire.h>


unsigned char maxBrightness = 255;
unsigned char pwmFrequency = 100;
int numRegisters = 3;
int numRGBleds = numRegisters*8/3;
int audioVal=0;

void setup(){
  Serial.begin(9600);
  Wire.begin();
  frequency = 93.0; //starting frequency
  setFrequency();
 
  // Sets the number of 8-bit registers that are used.
  ShiftPWM.SetAmountOfRegisters(numRegisters);

  // SetPinGrouping allows flexibility in LED setup. 
  // If your LED's are connected like this: RRRRGGGGBBBBRRRRGGGGBBBB, use SetPinGrouping(4).
  ShiftPWM.SetPinGrouping(1); //This is the default, but I added here to demonstrate how to use the funtion
  
  ShiftPWM.Start(pwmFrequency,maxBrightness);
  
  //setup button and triggers
  //define analog inputs
   pinMode(trim1,INPUT);
   pinMode(trim2,INPUT);
   pinMode(buttonSET,INPUT);
   pinMode(buttonMode,INPUT);
   pinMode(audioLeft,INPUT);
   pinMode(audioRight,INPUT);

   
   digitalWrite(buttonSET,LOW);
   digitalWrite(buttonMode,LOW);
   trim1Val=analogRead(trim1);
   trim2Val=analogRead(trim2);


   buttonSETVal=digitalRead(buttonSET);
   buttonModeVal=digitalRead(buttonMode);
   buttonModeState=0;

   buttonSETState=0;

   mode=0;
   
   newmicros=micros();
 ShiftPWM.PrintInterruptLoad();
 
}


void loop()
{ 

ShiftPWM.SetAllRGB(200,200,200);

 Serial.println("radio");
         
         if (trim1Val==1)
           frequency=frequency+0.05;
         if (trim2Val==1)
           frequency=frequency-0.05;
         if (frequency>108)
           frequency=108;
         if (frequency<87.5)
           frequency=87.5;
           
           
           
           delay(50);
           if (oldFrequency!=frequency)
             setFrequency();
           Serial.println(frequency);
}


void setFrequency()
{
  frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
  frequencyH = frequencyB >> 8;
  frequencyL = frequencyB & 0XFF;
  delay(100);
  Wire.beginTransmission(0x60);
  Wire.write(frequencyH);
  Wire.write(frequencyL);
  Wire.write(0xB0);
  Wire.write(0x10);
  Wire.write((byte)0x00);
  Wire.endTransmission();
  delay(100);  
}

ciao

Gianpaolo

ciao,
I've tried to disconnect the shift register, but with no results...
any other suggestion is welcome :slight_smile:

I've tried to disconnect the shift register, but with no results...

What does that mean?

You always get results when you do something. What results did you get?

Try dropping the value in:-
unsigned char pwmFrequency = 100
and see if you can find a value that works, without physically attaching the shift register.

Hi,
I've tryed using low values such as 10 and high values such as 150, but still I get white noise instead of audio from fm radio;
without the shiftpwm code the radio works well.

disconnecting the shift registers I obtain white noise on the radio...

thank you

Gianpaolo