Thank you for all the input everyone. @anon73444976 's suggestion to use references worked out for me. To be honest, this sketch was for me to test this method to see if I could use it as a button, and I chose to do it with an LED. I have another sketch where I am using this bit of code. This other sketch is used to smooth out the motion of a servo motor in a simple way and it uses a button to do so. However, moving forward into my project, my button is replaced by a virtual one, which I have set up to send "1" when pushed and "0" when not continuously via the serial monitor. This button is on NodeRed, a
web browser-based flow editor, which can be used to create JavaScript functions.
The reason I wanted to use multiple arguments was that at some point I am going to be using 6 servos, which basically means I would need 6 switches. Now, this sketch worked out great using the references, for the LEDs. However, when I use it to run a single servo, my servo is basically going haywire. It goes back and forth at a single spot as if the button were being pushed and released very fast, i.e., 1010101010. And I'm not sure why this happening.
Here's the code for smoothing out my servo using a physical button:
#include<Servo.h>
int switch1;
float switch1Smoothed;
float switch1Prev;
Servo myservo1;
float a=1200.0;
void setup() {
Serial.begin(9600);
pinMode(A1,INPUT_PULLUP);
myservo1.attach(9);
myservo1.writeMicroseconds(a);
switch1Prev=a;
}
void loop() {
//Motor 1
switch1 = digitalRead(A1);
Serial.println(switch1);// read switch
switch1 = switch1 * 2400; // multiply by 10
// *** smoothing ***
switch1Smoothed = (switch1 * 0.05) + (switch1Prev * 0.95);
if(switch1Smoothed>=a){
switch1Prev = switch1Smoothed;
}
else{
switch1Prev = a;
}
// *** end of smoothing ***
// Serial.print(switch1); // print to serial terminal/plotter
// Serial.print(" , ");
// Serial.println(switch1Smoothed);
myservo1.writeMicroseconds(switch1Smoothed);
delay(20);
}
I've tested this code with a physical button and it works fine.
And here's the code after I merge it with the one I started this post with:
#include <Servo.h>
int switch1;
float switch1Smoothed;
float switch1Prev;
Servo myservo1;
float a=1200.0;
String readString, mot1, mot2;
void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
myservo1.attach(9);
myservo1.writeMicroseconds(a);
switch1Prev=a;
}
void captureChar(int& n1){
while (Serial.available()) {
delay(3); //delay to allow buffer to fill
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
mot1 = readString.substring(0,1);
mot2 = readString.substring(1,2);
n1 = mot1.toInt();
//n2 = mot2.toInt();
readString="";
}
}
void loop(){
captureChar(switch1);
switch1 = switch1 * 2400; // multiply by 2400
// *** smoothing ***
switch1Smoothed = (switch1 * 0.05) + (switch1Prev * 0.95);
if(switch1Smoothed>=a){
switch1Prev = switch1Smoothed;
}
else{
switch1Prev = a;
}
// *** end of smoothing ***
// Serial.print(switch1); // print to serial terminal/plotter
// Serial.print(" , ");
// Serial.println(switch1Smoothed);
myservo1.writeMicroseconds(switch1Smoothed);
delay(15);
}
Please let me know if you think I should make a separate post about this since I do realize that my question has now kind of changed.
Thank you.