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
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.
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.
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
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);
}
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.
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.
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.