Multiple serial connections (SPI & I2C at the same time) in Nano IOT 33

Hi,

I am trying to use two programs as a single program on Arduino nano 33 IOT. One of them uses I2C protocol (Piezoelectric pump) and the other uses SPI protocol (Pressure sensor). There is no error in the program while compiling. But the pressure sensor output is not correct (there is no change in the pressure and just showing 0). The piezoelectric pump works fine. Please find the code below:
Any help would be of great and highly appreciated. Thanks.

#include <Wire.h>
#include <SPI.h>

#define Addr (0xF6>>1)
#define TASTER 3
#define LED 13
#define I2C_DEVICEID 0x00
#define I2C_POWERMODE 0x01
#define I2C_FREQUENCY 0x02
#define I2C_SHAPE 0x03
#define I2C_BOOST 0x04
#define I2C_PVOLTAGE 0x06
#define I2C_P1VOLTAGE 0x06
#define I2C_P2VOLTAGE 0x07
#define I2C_P3VOLTAGE 0x08
#define I2C_P4VOLTAGE 0x09
#define I2C_UPDATEVOLTAGE 0x0A
#define I2C_AUDIO 0x05
// pressure_sensor
int SlaveSel = 10; //Define slave select as 10

//Transfer Variables
float Output_max = 15099494;//for psi___counts(90% of 2^24 counts or 0xE6666)
float Output_min = 1677722;//for psicounts (10% of 224 counts or 0x19999A)
int P_max = 300;//for mmHg;
int P_min = 0;
int Byte1,Byte2,Byte3,Byte4;
float Pressure;
float Result;
unsigned long Output;
long x2;
long x1;
long x0;

int debounce = 0 ;

int mode = 0;
#define MODE_OFF 0
#define MODE_50 1
#define MODE_100 2
#define MODE_200 3
#define MODE_400 4
#define MODE_800 5
#define MODE_MAX 6

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
Wire.begin();
Serial.begin(9600);
//Serial.println();
SPI.begin(); //Begin SPI protocol
pinMode(SlaveSel, OUTPUT); //Define pin 10 as an output

pinMode(LED, OUTPUT);
pinMode (TASTER,INPUT_PULLUP);
digitalWrite(SlaveSel, HIGH); //Pull Slave select line high

Wire.beginTransmission(Addr);
Wire.write(I2C_POWERMODE); // write address = 0x01
Wire.write(0x00); //Adress 0x01 = 0x01 (enable)
Wire.write(0x40); // Adress 0x02 = 0x40 (100Hz)
Wire.write(0x00); //Adress 0x03 = 0x00 (sine wave)
Wire.write(0x00); //Adress 0x04 = 0x00 (800KHz)
Wire.write(0x00); //Adress 0x05 = 0x00 (audio off)
Wire.write(0x1F); // Adress 0x06 = 0x00 (EL1)
Wire.write(0x1F); // Adress 0x07 = 0x00 (EL2)
Wire.write(0x1F); // Adress 0x08 = 0x00 (EL3)
Wire.write(0x1F); // Adress 0x09 = 0x00 (EL4)
Wire.write(0x01); // Adress 0x0A = 0x00 (update)
Wire.endTransmission();
}

// the loop routine runs over and over again forever:
void loop()
{
if (digitalRead(TASTER)==LOW) {
if (!(debounce&0x01)) {
mode=(mode+1)%MODE_MAX;
switch (mode) {
case MODE_OFF:
Wire.beginTransmission(Addr);
Wire.write(I2C_POWERMODE); // start adress
Wire.write(0x00); // disable pumps
Wire.endTransmission();
digitalWrite(LED,LOW);
break;
case MODE_50:
Wire.beginTransmission(Addr);
Wire.write(I2C_POWERMODE); // start adress
Wire.write(0x01); // enable pumps
Wire.write(0x00); // frequency 50 Hz
Wire.endTransmission();
digitalWrite(LED,HIGH);
break;
case MODE_100:
Wire.beginTransmission(Addr);
Wire.write(I2C_FREQUENCY); // start adress
Wire.write(0x40); // 100 Hz
Wire.endTransmission();
break;
case MODE_200:
Wire.beginTransmission(Addr);
Wire.write(I2C_FREQUENCY); // start adress
Wire.write(0x80); // 200 Hz
Wire.endTransmission();
break;
case MODE_400:
Wire.beginTransmission(Addr);
Wire.write(I2C_FREQUENCY); // start adress
Wire.write(0xC0); // 400 Hz
Wire.endTransmission();
break;
case MODE_800:
Wire.beginTransmission(Addr);
Wire.write(I2C_FREQUENCY); // start adress
Wire.write(0xFF); // 800 Hz
Wire.endTransmission();
break;
}
debounce|=0x01;
}
} else {
if (debounce&0x01) {
debounce&=~0x01;
}
}
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); //Begin comm with sensor while using the SPI settings: 800kHz, MSB first, SPI mode 0
digitalWrite(SlaveSel, LOW); //Pull slave select pin low to start data transfer

Byte1 = SPI.transfer(0xAA);
delay(25);
Byte2 = SPI.transfer(0x00);
delay(25);
Byte3 = SPI.transfer(0x00);
delay(25);
Byte4 = SPI.transfer(0x00);
delay(25);

   digitalWrite(SlaveSel, HIGH);  //Pull slave select pin high to stop data transfer
   SPI.endTransaction();  //End comms with sensor

x2 = (long)Byte2<<16;
x1 = (long)Byte3<<8;
x0 = (long)Byte4;
Output = x2 | x1 | x0;
// Transfer function

Result = Output*5.8/15099494;
Serial.print("mmHg");
Serial.println(Result);

delay(100);
}

Code tags, please.

#include <Wire.h>
#include <SPI.h>

#define Addr (0xF6>>1)
#define TASTER          3
#define LED             13
#define I2C_DEVICEID 0x00
#define I2C_POWERMODE 0x01
#define I2C_FREQUENCY 0x02
#define I2C_SHAPE 0x03
#define I2C_BOOST 0x04
#define I2C_PVOLTAGE 0x06
#define I2C_P1VOLTAGE 0x06
#define I2C_P2VOLTAGE 0x07
#define I2C_P3VOLTAGE 0x08
#define I2C_P4VOLTAGE 0x09
#define I2C_UPDATEVOLTAGE 0x0A
#define I2C_AUDIO       0x05
// pressure_sensor
int SlaveSel = 10;    //Define slave select as 10

//Transfer Variables
float Output_max   = 15099494;//for psi___counts(90% of 2^24 counts or 0xE6666)
float Output_min   = 1677722;//for psicounts (10% of 224 counts or 0x19999A)
int   P_max = 300;//for mmHg;
int   P_min = 0;
int   Byte1,Byte2,Byte3,Byte4;
float Pressure;
float Result;
unsigned long Output;
long x2;
long x1;
long x0;

int debounce = 0 ;

int mode = 0;
#define MODE_OFF  0
#define MODE_50   1
#define MODE_100  2
#define MODE_200  3
#define MODE_400  4
#define MODE_800  5
#define MODE_MAX  6

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  Wire.begin();
  Serial.begin(9600);
  //Serial.println();
  SPI.begin(); //Begin SPI protocol
  pinMode(SlaveSel,  OUTPUT);  //Define pin 10 as an output
  
  pinMode(LED, OUTPUT);
  pinMode (TASTER,INPUT_PULLUP);
  digitalWrite(SlaveSel, HIGH);  //Pull Slave select line high

  Wire.beginTransmission(Addr);
  Wire.write(I2C_POWERMODE);             // write address = 0x01
  Wire.write(0x00); //Adress 0x01 = 0x01 (enable)
  Wire.write(0x40); // Adress 0x02 = 0x40 (100Hz)
  Wire.write(0x00); //Adress 0x03 = 0x00 (sine wave)
  Wire.write(0x00); //Adress 0x04 = 0x00 (800KHz)
  Wire.write(0x00); //Adress 0x05 = 0x00 (audio off)
  Wire.write(0x1F);                       // Adress 0x06 = 0x00 (EL1)
  Wire.write(0x1F);                       // Adress 0x07 = 0x00 (EL2)
  Wire.write(0x1F);                       // Adress 0x08 = 0x00 (EL3)
  Wire.write(0x1F);                       // Adress 0x09 = 0x00 (EL4)
  Wire.write(0x01);                       // Adress 0x0A = 0x00 (update)
  Wire.endTransmission();
}

// the loop routine runs over and over again forever:
void loop()
{
  if (digitalRead(TASTER)==LOW) {
    if (!(debounce&0x01)) {
      mode=(mode+1)%MODE_MAX;
      switch (mode) {
   case MODE_OFF:
Wire.beginTransmission(Addr);
Wire.write(I2C_POWERMODE); // start adress
Wire.write(0x00); // disable pumps
Wire.endTransmission();
digitalWrite(LED,LOW);
break;
          case MODE_50:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_POWERMODE);   // start adress
          Wire.write(0x01);            // enable pumps
          Wire.write(0x00);            // frequency 50 Hz
          Wire.endTransmission();
          digitalWrite(LED,HIGH);
          break;    
        case MODE_100:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_FREQUENCY);   // start adress
          Wire.write(0x40);            // 100 Hz
          Wire.endTransmission();
          break;
        case MODE_200:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_FREQUENCY);   // start adress
          Wire.write(0x80);            // 200 Hz
          Wire.endTransmission();
          break;
        case MODE_400:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_FREQUENCY);   // start adress
          Wire.write(0xC0);            // 400 Hz
          Wire.endTransmission();
          break;
        case MODE_800:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_FREQUENCY);   // start adress
          Wire.write(0xFF);            // 800 Hz
          Wire.endTransmission();
          break;
      }
      debounce|=0x01;
    }
  } else {
    if (debounce&0x01) {
      debounce&=~0x01;
    }
  }
    SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); //Begin comm with sensor while using the SPI settings: 800kHz, MSB first, SPI mode 0
 digitalWrite(SlaveSel, LOW); //Pull slave select pin low to start data transfer

Byte1 = SPI.transfer(0xAA);
delay(25);
Byte2 = SPI.transfer(0x00);
delay(25);
Byte3 = SPI.transfer(0x00);
delay(25);
Byte4 = SPI.transfer(0x00);
delay(25);

       digitalWrite(SlaveSel, HIGH);  //Pull slave select pin high to stop data transfer
       SPI.endTransaction();  //End comms with sensor
x2 = (long)Byte2<<16;
          x1 = (long)Byte3<<8;
          x0 = (long)Byte4;
          Output = x2 | x1 | x0;
           // Transfer function
           
Result = Output*5.8/15099494;
           Serial.print("mmHg");
           Serial.println(Result);

  delay(100);
}

There isn't a single function and the indentation is completely off. How can you even make sense of that thing?

What part of the code is working? What part is not? If you break the code into manageable chunks, you will be able to test them one at a time.

Please find the two different codes below: I'm new to coding and trying learn things.

Piezoelectric pump (I2C protocol)

#include <Wire.h>

#define Addr (0xF6>>1)
#define TASTER          3
#define LED             13
#define I2C_DEVICEID	0x00
#define I2C_POWERMODE	0x01
#define I2C_FREQUENCY	0x02
#define I2C_SHAPE	0x03
#define I2C_BOOST	0x04
#define I2C_PVOLTAGE	0x06
#define I2C_P1VOLTAGE	0x06
#define I2C_P2VOLTAGE	0x07
#define I2C_P3VOLTAGE	0x08
#define I2C_P4VOLTAGE	0x09
#define I2C_UPDATEVOLTAGE	0x0A
#define I2C_AUDIO       0x05

int debounce = 0 ; 

int mode = 0;
#define MODE_OFF  0
#define MODE_50   1
#define MODE_100  2
#define MODE_200  3
#define MODE_400  4
#define MODE_800  5
#define MODE_MAX  6

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  Wire.begin();
  Serial.begin(9600);
  Serial.println();
  pinMode(LED, OUTPUT);
  pinMode (TASTER,INPUT_PULLUP);
  Wire.beginTransmission(Addr);
  Wire.write(I2C_POWERMODE);             			// write address = 0x01
  Wire.write(0x00); //Adress 0x01 = 0x01 (enable)
  Wire.write(0x40); // Adress 0x02 = 0x40 (100Hz)
  Wire.write(0x00); //Adress 0x03 = 0x00 (sine wave) 
  Wire.write(0x00); //Adress 0x04 = 0x00 (800KHz) 
  Wire.write(0x00); //Adress 0x05 = 0x00 (audio off) 
  Wire.write(0x1F);                       			// Adress 0x06 = 0x00 (EL1)
  Wire.write(0x1F);                       			// Adress 0x07 = 0x00 (EL2)
  Wire.write(0x1F);                       			// Adress 0x08 = 0x00 (EL3)
  Wire.write(0x1F);                       			// Adress 0x09 = 0x00 (EL4)
  Wire.write(0x01);                       			// Adress 0x0A = 0x00 (update)
  Wire.endTransmission();
}

// the loop routine runs over and over again forever:
void loop() {
  if (digitalRead(TASTER)==LOW) {
    if (!(debounce&0x01)) {
      mode=(mode+1)%MODE_MAX;
      switch (mode) {
   case MODE_OFF: 
Wire.beginTransmission(Addr); 
Wire.write(I2C_POWERMODE); // start adress 
Wire.write(0x00); // disable pumps 
Wire.endTransmission(); 
digitalWrite(LED,LOW); 
break; 
          case MODE_50: 
          Wire.beginTransmission(Addr); 
          Wire.write(I2C_POWERMODE);   // start adress 
          Wire.write(0x01);            // enable pumps 
          Wire.write(0x00);            // frequency 50 Hz 
          Wire.endTransmission(); 
          digitalWrite(LED,HIGH); 
          break;    
        case MODE_100:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_FREQUENCY);   // start adress
          Wire.write(0x40);            // 100 Hz
          Wire.endTransmission();
          break;
        case MODE_200:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_FREQUENCY);   // start adress
          Wire.write(0x80);            // 200 Hz
          Wire.endTransmission();
          break;
        case MODE_400:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_FREQUENCY);   // start adress
          Wire.write(0xC0);            // 400 Hz
          Wire.endTransmission();
          break;
        case MODE_800:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_FREQUENCY);   // start adress
          Wire.write(0xFF);            // 800 Hz
          Wire.endTransmission();
          break;
      }
      debounce|=0x01;
    }
  } else {
    if (debounce&0x01) {
      debounce&=~0x01;
    }
  }
  delay(100);
}

Pressure sensor code (SPI protocol):

//Pin 13 Clock
//Pin 12 MISO
//Pin 11 MOSI
//Pin 10 SS
//3V power supply to sensor


#include <SPI.h>
int SlaveSel = 10;    //Define slave select as 10

//Transfer Variables
float Output_max   = 15099494;//for psi___counts(90% of 2^24 counts or 0xE6666)
float Output_min   = 1677722;//for psicounts (10% of 224 counts or 0x19999A) 
int   P_max = 300;//for mmHg;
int   P_min = 0;
int   Byte1,Byte2,Byte3,Byte4;
float Pressure;
float Result;
unsigned long Output;
long x2;
long x1;
long x0;
void setup() {
       Serial.begin(9600);  //Launch serial monitor
       SPI.begin(); //Begin SPI protocol
       pinMode(SlaveSel,  OUTPUT);  //Define pin 10 as an output
       digitalWrite(SlaveSel, HIGH);  //Pull Slave select line high
}

void loop()
{
  SPI.beginTransaction(SPISettings(800000, MSBFIRST, SPI_MODE0)); //Begin comm with sensor while using the SPI settings: 800kHz, MSB first, SPI mode 0
 digitalWrite(SlaveSel, LOW); //Pull slave select pin low to start data transfer

Byte1 = SPI.transfer(0xAA);
delay(25);
Byte2 = SPI.transfer(0x1E);
delay(25);
Byte3 = SPI.transfer(0x1E);
delay(25);
Byte4 = SPI.transfer(0x1E);
delay(25);

       digitalWrite(SlaveSel, HIGH);  //Pull slave select pin high to stop data transfer
       SPI.endTransaction();  //End comms with sensor
x2 = (long)Byte2<<16;
          x1 = (long)Byte3<<8;
          x0 = (long)Byte4;
          Output = x2 | x1 | x0;
           // Transfer function
           
Result = Output*5.8/15099494;
           Serial.print("mmHg");
           Serial.println(Result);
           
 delay(1000);


 

}

Thanks in advance

I'm trying to combine both codes together and run at the same time as a single program in order to operate the piezoelectric pump and measure pressure.

Whatever it is that you are trying to do, these general recommendations always apply:

  1. Break your program into manageable functions that just do one thing. Test the functions individually. When they work, chain them together. Just don't throw everything into the loop() and hope for the best.
  2. Fix that indentation, add some blank lines to separate different sections, bring some order into the code. The compiler may not care, but you will, and people trying to help you will, too.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Thanks for the suggestion. Below is the sorted code:
When I add the bold part of the code to just the pressure sensor code it starts to show 0.

#include <SPI.h>
#include <Wire.h>

//pump
#define Addr (0xF6>>1)
#define TASTER          3
#define LED             13
#define I2C_DEVICEID  0x00
#define I2C_POWERMODE 0x01
#define I2C_FREQUENCY 0x02
#define I2C_SHAPE 0x03
#define I2C_BOOST 0x04
#define I2C_PVOLTAGE  0x06
#define I2C_P1VOLTAGE 0x06
#define I2C_P2VOLTAGE 0x07
#define I2C_P3VOLTAGE 0x08
#define I2C_P4VOLTAGE 0x09
#define I2C_UPDATEVOLTAGE 0x0A
#define I2C_AUDIO       0x05



//pressure sensor
int SlaveSel = 10;    //Define slave select as 10
float Output_max   = 15099494;//for psi___counts(90% of 2^24 counts or 0xE6666)
float Output_min   = 1677722;//for psicounts (10% of 224 counts or 0x19999A) 
int   P_max = 300;//for mmHg;
int   P_min = 0;
int   Byte1,Byte2,Byte3,Byte4;
float Pressure;
float Result;
unsigned long Output;
long x2;
long x1;
long x0;



//pump
int debounce = 0 ; 

int mode = 0;
#define MODE_OFF  0
#define MODE_50   1
#define MODE_100  2
#define MODE_200  3
#define MODE_400  4
#define MODE_800  5
#define MODE_MAX  6



void setup() {

  
  //pressure sensor     
       SPI.begin(); //Begin SPI protocol
       Serial.begin(9600);  //Launch serial monitor
       pinMode(SlaveSel,  OUTPUT);  //Define pin 10 as an output
       digitalWrite(SlaveSel, HIGH);  //Pull Slave select line high
      

   **// pump**
**  // initialize the digital pin as an output.**
**  Wire.begin();**
**  Serial.begin(9600);**
**  Serial.println();**
**  pinMode(LED, OUTPUT);**
**  pinMode (TASTER,INPUT_PULLUP);**
**  Wire.beginTransmission(Addr);**
**  Wire.write(I2C_POWERMODE);                   // write address = 0x01**
**  Wire.write(0x00); //Adress 0x01 = 0x01 (enable)**
**  Wire.write(0x40); // Adress 0x02 = 0x40 (100Hz)**
**  Wire.write(0x00); //Adress 0x03 = 0x00 (sine wave) **
**  Wire.write(0x00); //Adress 0x04 = 0x00 (800KHz) **
**  Wire.write(0x00); //Adress 0x05 = 0x00 (audio off) **
**  Wire.write(0x1F);                             // Adress 0x06 = 0x00 (EL1)**
**  Wire.write(0x1F);                             // Adress 0x07 = 0x00 (EL2)**
**  Wire.write(0x1F);                             // Adress 0x08 = 0x00 (EL3)**
**  Wire.write(0x1F);                             // Adress 0x09 = 0x00 (EL4)**
**  Wire.write(0x01);                             // Adress 0x0A = 0x00 (update)**
**  Wire.endTransmission();**
}


void loop()


{
  // pressure sensor
  SPI.beginTransaction(SPISettings(800000, MSBFIRST, SPI_MODE0)); //Begin comm with sensor while using the SPI settings: 800kHz, MSB first, SPI mode 0
 digitalWrite(SlaveSel, LOW); //Pull slave select pin low to start data transfer

Byte1 = SPI.transfer(0xAA);
delay(25);
Byte2 = SPI.transfer(0x1E);
delay(25);
Byte3 = SPI.transfer(0x1E);
delay(25);
Byte4 = SPI.transfer(0x1E);
delay(25);

       digitalWrite(SlaveSel, HIGH);  //Pull slave select pin high to stop data transfer
       SPI.endTransaction();  //End comms with sensor
x2 = (long)Byte2<<16;
          x1 = (long)Byte3<<8;
          x0 = (long)Byte4;
          Output = x2 | x1 | x0;
           // Transfer function
           
Result = Output*5.8/15099494;
           Serial.print("mmHg");
           Serial.println(Result);


           

// pump

           if (digitalRead(TASTER)==LOW) {
    if (!(debounce&0x01)) {
      mode=(mode+1)%MODE_MAX;
      switch (mode) {
   case MODE_OFF: 
Wire.beginTransmission(Addr); 
Wire.write(I2C_POWERMODE); // start adress 
Wire.write(0x00); // disable pumps 
Wire.endTransmission(); 
digitalWrite(LED,LOW); 
break; 
          case MODE_50: 
          Wire.beginTransmission(Addr); 
          Wire.write(I2C_POWERMODE);   // start adress 
          Wire.write(0x01);            // enable pumps 
          Wire.write(0x00);            // frequency 50 Hz 
          Wire.endTransmission(); 
          digitalWrite(LED,HIGH); 
          break;    
        case MODE_100:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_FREQUENCY);   // start adress
          Wire.write(0x40);            // 100 Hz
          Wire.endTransmission();
          break;
        case MODE_200:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_FREQUENCY);   // start adress
          Wire.write(0x80);            // 200 Hz
          Wire.endTransmission();
          break;
        case MODE_400:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_FREQUENCY);   // start adress
          Wire.write(0xC0);            // 400 Hz
          Wire.endTransmission();
          break;
        case MODE_800:
          Wire.beginTransmission(Addr);
          Wire.write(I2C_FREQUENCY);   // start adress
          Wire.write(0xFF);            // 800 Hz
          Wire.endTransmission();
          break;
      }         
debounce|=0x01;
    }
  } else {
    if (debounce&0x01) {
      debounce&=~0x01;
    }
  }
  delay(100);
}

yes this is hard izlefy