Pezio's mistake is not being able to run

I made a piezo on Arduino 1, on ESP32 why doesn't the piezo action work?
deserialzeJson() failed IncompleteInput

Arduino

#include <ArduinoJson.h>

int knockSensor0 = 2;  
int knockSensor1 = 3;
int knockSensor2 = 4;
int knockSensor3 = 5;
int knockSensor4 = 6;

int val0, val1, val2, val3, val4;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  val0 = analogRead(knockSensor0);    
  val1 = analogRead(knockSensor1); 
  val2 = analogRead(knockSensor2);    
  val3 = analogRead(knockSensor3);
  val4 = analogRead(knockSensor4);

  // Create the JSON document
  const size_t capacity = JSON_OBJECT_SIZE(128);
  DynamicJsonDocument doc(capacity);

  doc["val00"] = val0; // 100
  doc["val11"] = val1; // 150
  doc["val22"] = val2; // 150
  doc["val33"] = val3; // 150
  doc["val44"] = val4; // 100


  //Send data to NodeMCU
  serializeJson(doc, Serial);
}

ESP32

// Include required libraries
#include "Arduino.h"
#include "Audio.h"
#include "SPI.h"
#include "SD.h"
#include "FS.h"
#include <ArduinoJson.h>
 
// microSD Card Reader connections
#define SD_CS          5
#define SPI_MOSI      23 
#define SPI_MISO      19
#define SPI_SCK       18
 
// I2S Connections
#define I2S_DOUT      22
#define I2S_BCLK      26
#define I2S_LRC       25

Audio audio;

int val0, val1, val2, val3, val4;

void setup() {
    Serial.begin(9600);
    while (!Serial) continue;
    
    // Set microSD Card CS as OUTPUT and set HIGH
    pinMode(SD_CS, OUTPUT);      
    digitalWrite(SD_CS, HIGH); 
    
    // Initialize SPI bus for microSD Card
    SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
    
    // Start Serial Port
    Serial.begin(115200);
    
    // Start microSD Card
    if(!SD.begin(SD_CS))
    {
      Serial.println("Error accessing microSD card!");
      while(true); 
    }
    
    // Setup I2S 
    audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
    
    // Set Volume
    audio.setVolume(5);
}

void loop(){
  const size_t capacity = JSON_OBJECT_SIZE(128);
  DynamicJsonDocument doc(capacity);
  
  DeserializationError error = deserializeJson(doc, Serial);
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }

  val0 = doc["val00"]; // 100
  val1 = doc["val11"]; // 150
  val2 = doc["val22"]; // 150
  val3 = doc["val33"]; // 150
  val4 = doc["val44"]; // 100

  if (val0 >= 100) {playsound();}
  if (val1 >= 150) {playsound1();}
  if (val2 >= 150) {playsound2();}
  if (val3 >= 150) {playsound3();}
  if (val4 >= 100) {playsound4();}
}


void playsound()
{
  audio.connecttoFS(SD, "0.wav");
  audio.loop();
}
void playsound1()
{
  audio.connecttoFS(SD, "1.wav");
  audio.loop();
}
void playsound2()
{
  audio.connecttoFS(SD, "2.wav");
  audio.loop();
}
void playsound3()
{
  audio.connecttoFS(SD, "3.wav");
  audio.loop();
}
void playsound4()
{
  audio.connecttoFS(SD, "4.wav");
  audio.loop();
}


Just looking at your code... seriously set it aside and learn to use arrays. Your fingers and keyboard should thank you, they won't but they should!

What should I change, I want when the button is pressed the ESP32 can sound?

It seems to be a robot generated code.

Project fixation will steal your time, maybe you figure out how and gain from that.. I did almost 40 years ago.

I'm not going to wade through all of that looking for a bug. I have some kind of life and it would suck that.

A quick learn that should turn you on greatly;
Arduino Reference Page on arrays.
The information should be all the explanation needed!

I have been using the json code above for 6 months but not for buttons but for the LCD screen, so from there I want to understand if I add a button what I have to do so that it works

JSON huh? Please explain the code above!

const byte numberOfPiezos = 5; // const variables never change
byte knockSensorPin[ numberOfPiezos ]  =  { 2, 3, 4, 5, 6 };  
int piezoSense[ numberOfPiezos ]; // I have NO IDEA if you use analog or digital to sense and right now don't care.

To add buttons, wire them up, change numberOfPiezos and add pin numbers. With code that uses the array well, that is ALL you need.
When every pin variable has its own name, every pin also has to have its own sections (yes, plural!) of code and that is a path to huge spaghetti code. I give you SOLID advice here that will save you loads of time even in the short term.

Are you ready to learn arrays in pursuit of that?

When I connected piezos to Arduino I made a circuit to protect the board from >5V then used what I got out to tell how hard the piezo was hit.. those were my piezo button/force sensors... and I didn't use slowwww analog reads.

You are not using Analog, means you are using digital pins?

My only problem is that in esp32 it reads as an array in the serial monitor which appears and then I want to take it so I can enter the if

I am using arduinojson assistant

I passed the piezo through a diode to gate a BJT for the time of the diode-flattened pulse. That let 5V flow through a 2K resistor to fill a wire connected to a digital pin.
A digital read "eats" about 1 micro-Amp of current (Atmel datasheet info) from that charged wire. A fast loop to count how many reads to get the pin LOW (20 to 50 micros for a soft to regular tap, up to 2000-3000 reads when I smacked the disk with a screwdriver handle hard enough to make the table bounce.. and the disk took it but not the cheap disks I got later of eBay). Any EE could give you a better circuit using a cheap opto-isolator.
One big lesson from that is that diodes can tame piezo spikes!

The full circuit used 4 diodes (2 per lead) to read not only press on 1 pin but release on another to get changes in force. And again, any EE can do it better.

ok, what about the code in esp32 is there something wrong?

See post #7.

I am NOT slogging through the mess that those variables guarantee.
I am NOT spending hours checking every line of bloat for mistypes.

If YOU want to then have at it! Make dozens of sketches the same way even, why waste hours when you can waste months?

OTOH, you may be trolling.

Comment-out all audio.xyz() lines and replace them with Serial.print("something"); lines to see if the buzzer has any effect. As @paulpaulson noted, your robot code (very obvious) puts the burden on everyone but you. Write your sketch from your knowledge and abilities. You will find the result better than asking a robot.

I tried with the button and this is what happened. I haven't been pressured to go my own way
Screenshot pada 2023-11-03 19-40-50

Arduino

#include <ArduinoJson.h>

int knockSensor0 = 7;  

int val0 = 0;

void setup() {
  pinMode(knockSensor0, INPUT);
  // put your setup code here, to run once:
  
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  val0 = digitalRead(knockSensor0);    

  // Create the JSON document
  const size_t capacity = JSON_OBJECT_SIZE(48);
  DynamicJsonDocument doc(capacity);

  doc["val0"] = val0; // 100

  //Send data to NodeMCU
  serializeJson(doc, Serial);
}

ESP32

#include <ArduinoJson.h>

int val0 = 0;

void setup() {
    Serial.begin(9600);
    while (!Serial) continue;
}

void loop(){
  const size_t capacity = JSON_OBJECT_SIZE(48);
  DynamicJsonDocument doc(capacity);
  
  DeserializationError error = deserializeJson(doc, Serial);
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }

  val0 = doc["val0"]; // 100

  if (val0 == HIGH){
    playsound1();
  } else {
    playsound();
  }
}


void playsound()
{
  Serial.println("Berhasil");
}
void playsound1()
{
  Serial.println("Tidak ada aksi");
}

At least either define capacity as static const size_t capacity or make capacity a global variable.

Begin Serial at 115200 rather than 9600 to transmit print characters out of the 64 char print buffer faster. If you try to print more text thanthe buffer has room then regular code execution chokes until what's in the buffer transmits out enough for the print line to finish -- which happens faster at high baud rate.

Learn how to use arrays to manage your sensors and leave the JSON part out to reduce complications and unknown factors --- and add that on top of stable sensing.

When all piezo pins are in an array named like piezoPin[ ]. Then one code can address the pin # of any as piezoPin[ whichPiezo ].
The same with time values, should you sense knock vibrations over time, make an array for each value and sensor.
Without arrays you need a separate section of code for each different piezo's sense0, sense1, sense2.... etc, variables. Then every change has to me made to alllll of the sections or there's a bug in a heap of spaghetti code.

I offer for you to reduce code complexity to make bug spotting easier so resist if you want, just make no effort.

Ok, that works fine. It's just the sound library that won't make a sound when the pizeo is tapped like it's hit by a slow sound, is there a solution?

Are the piezos supposed to make the sound or trigger a sound maker?

Just to be sure, are the piezos for buttons (inputs) or speakers (outputs)?

I want to make when the piezo is hit it produces the sound that is on the SD card into the speaker but I am confused about which library is suitable and can be used on the ESP8266 and ESP32 to produce sound from the SD card to the speaker

Hi,
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

I used a DFPlayer Mini to play MP3 files. Finding a library for sound on an ESP chip is outside of my experience but my guess is that you can play wav files through bit bangs.