2 piezo switches with diverent instructions to the same pin.

Hi

I am working on a e-surfboard prototype to help me get into waves. I have a shoulder injury that is not going to get any better and I need some paddle assistants.

I have 2 thruster motors that are controlled by 2 electronic speed controllers that get a signal from my Arduino. At the moment I have 1 Piezo switch that gives the instruction to the Arduino to send the program to the esc’s. I would like to add another piezo switch to be able to send a second program to the esc’s. Something like 2 minutes at 50 % thrust. I hope someone on this forum can write the code for me. Because I really don’t understand it. The code I have now is written for me as well. I can make small changes in thrust and time but that is about it. I try to understand it and did some internet searches and read a book about it but it looks like acradabra (Chinese, French or any other languages that is not dutch) to me.

Please help me out I want to be able to go surfing again.

this is the code I have now feel free to alter it and reply.

//By Adam Šimko for Bart de Rooij
//Blue Robotics T200 Surfboard Paddle Assist
//Jan 03 2017

#include <Servo.h> //include servo control library to operate ESC

//start critial values to edit as desired
//---------------------------------------
#define BUTTON 10 //momentary button switch on digital pin 10

#define THRUSTERS 5 //thruster ESCs on digital pin 5

#define THRUST1 0.1 //initial thrust level for both thrusters after button press (decimal percent, i.e. 10% = 0.1)

#define TIME1 5 //thrust time at thrust level 1 (seconds)

#define THRUST2 0.20 //thrust level for both thrusters after end thrust1 (decimal percent, i.e. 20% = 0.2)

#define TIME2 5 //thrust time at thrust level 1 (seconds)

#define THRUST3 0.3 //initial thrust level for both thrusters after button press (decimal percent, i.e. 30% = 0.3)

#define TIME3 5 //thrust time at thrust level 1 (seconds)

#define THRUST4 0.15 //thrust level for both thrusters after end thrust1 (decimal percent, i.e. 15% = 0.15)

#define TIME4 5 //thrust time at thrust level 1 (seconds)

int throttleChangeDelay = 1; //delay in milliseconds per microsecond of PWM value change (i.e. deault of one means 400ms time from 1500us to 1900us, or stopped to full throttle)

//end critial values to edit as desired
//-------------------------------------

int currentThrottle = 1500; //initial throttle value
int changeThrottle; //initial empty throttle value to change to

int pushButton = BUTTON; //momentary push button attached to digital pin 10 and GND
bool buttonPushed; //pushbutton has only two states

Servo esc; //Servo function named esc

void setup() {

pinMode(pushButton, INPUT); //set pushButton as an input

pinMode(BUTTON, INPUT); //pin 10 is input to push button is connected
digitalWrite(BUTTON, HIGH); //makes pin 10 high (5v)

esc.attach(THRUSTERS); //attaches ESCs to THRUSTERS pin

esc.writeMicroseconds(1500); // send “stop” signal to ESC.
delay(1000); // delay to allow the ESC to recognize the stopped signal
}

void loop() {

int buttonState = digitalRead(pushButton); //reads the state of push button
if ( buttonState == 0 ) { //if button is making contact with ground
changeThrottle = (1500 + (THRUST1 * 400)); //set thrusters to THRUST1…
smooth(); //uses smooth function to get to throttle value
delay(TIME1 * 1000); //…for TIME1
changeThrottle = (1500 + (THRUST2 * 400)); //set thrusters to THRUST2…
smooth(); //uses smooth function to get to throttle value
delay(TIME2 * 1000); //…for TIME2
changeThrottle = (1500 + (THRUST3 * 400)); //set thrusters to THRUST3…
smooth(); //uses smooth function to get to throttle value
delay(TIME3 * 1000); //…for TIME1
changeThrottle = (1500 + (THRUST4 * 400)); //set thrusters to THRUST4…
smooth(); //uses smooth function to get to throttle value
delay(TIME4 * 1000); //…for TIME2
changeThrottle = 1500; //stops thrusters
smooth(); //uses smooth function to get to throttle value
}
if ( buttonState == 1 ) { //if button is not making contact with ground
esc.writeMicroseconds(1500); //keep thrusters stopped
}
}

void smooth() { //smooth function to transition smoothly between throttle values

// Are we going up or down?
int step = 1; //steps up by 1 per cycle
if ( changeThrottle < currentThrottle ) //if decreasing throttle
step = -1; //or down by 1 per cycle

// Slowly move to the new throttle value
while ( currentThrottle != changeThrottle ) { //while current throttle different from desired throttle
esc.write(currentThrottle + step); //stup us throttle by 1us per cycle
currentThrottle = currentThrottle + step; //changes current throttle to new current throttle
delay(throttleChangeDelay); //delay cyacle
}

}

At the moment I have 1 Piezo switch that gives the instruction to the Arduino to send the program to the esc’s.

No, it does not. You can send pulses to the ESCs, NOT programs.

That code reads a digital pin. Piezos are typically connected to analog pins.

Hi Paul thanks for the correction.

Is there any change that you will creat the additional code so i have two options. the one in my first post and another one with 2 minutes 50%. I guess you know how sadly I don't.

I would be really thankfull if you would.

Kind regards,

Bart