I am completely new to arduino and coding. I have a robot that looks like a tripod (each leg is made of scissor-shaped joints) and is attached to a servo motor so when the motor moves up and down with a gear, the legs expand and retract making it seem like the robot is standing tall and then the opposite. I've already figured out a sketch to make that happen at regular intervals.
But now I want to install Big Sound Sensor KY-037 to animate the robot when I clap for example.
I want to either achieve:
Clap - robot goes up -- then -- clap - robot goes down
OR
Clap - robot goes up and down (or down and up) -- repeat when I clap again
Here is the sketch I currently have:
#include <VarSpeedServo.h> //Library for to run a servo with different speeds
VarSpeedServo ahmadservo; // create servo object to control a servo
int BigSound = 2;
void setup() {
ahmadservo.attach(10); // attaches the servo on pin 10 to the servo object
ahmadservo.write(180, 50, true); //set servo to 180 degrees at the speed of 50
pinMode (BigSound, INPUT);
}
void loop() {
int SensorData=digitalRead(BigSound);
if (SensorData==1)
{
ahmadservo.write(180, 50, true); //turn servo to 180 degrees and the speed of 50
delay(15); //wait 0.015 seconds
}
else
{
ahmadservo.write(0, 50, true); //turn servo to 10 degrees and the speed of 50
delay(15); //wait 0.015 seconds
}
}
I am completely new to this and I feel I am really far off from what I need to be doing, I am really confused lol. In my head this should work but I am sure there's a lot more that I need to do. So any help will be greatly appreciated. Any direction at all.
Thank you! You are correct. I looked up this tutorial to help me do that.
My updated sketch is:
#include <VarSpeedServo.h> //Library for to run a servo with different speeds
VarSpeedServo ahmadservo; // create servo object to control a servo
int BigSound = 2;
boolean trig = true;
boolean state = true;
void setup() {
ahmadservo.attach(10); // attaches the servo on pin 10 to the servo object
ahmadservo.write(180, 50, true); //set servo to 180 degrees at the speed of 50
pinMode (BigSound, INPUT);
}
void loop() {
trig = digitalRead(BigSound);
if (trig == false && state == false)
{
ahmadservo.write(180, 50, true); //turn servo to 180 degrees and the speed of 50
state = true;
delay(15); //wait 0.015 seconds
}
else if (trig == false && state == true)
{
ahmadservo.write(0, 50, true); //turn servo to 0 degrees and the speed of 50
state = false;
delay(15); //wait 0.015 seconds
}
}
This makes it go up once I clap and back down when I clap again. However, I have no idea what any of this means. Boolean state...? Boolean trig...? what is the state? I don't get what is happening with the "if" statements. Can you please explain?
The normal way to do it is to read the state of an input at the start of loop() and compare it with the last time that you read it. If they are the same then nothing has happened so go round loop() again until it does
Once a change is detected, test whether the input is now active. Could be HIGH or LOW depending on your circuit. If it is now active set a boolean variable to true, otherwise set it to false. If you give the variable a meaningful name such as active then its meaning will be clearer later. Save the current pin state as the previous pin state ready to test for a subsequent change next time round loop()
OK, that is the change detection over and done with and you have a variable named active with a value of true or false. Test the value of active and if it is true take the required action such as moving the servo to 180 else move the servo to 0