I wanted to build a product which is a Posture Corrector. It suppose to have push button connected to servo motors, that move when the push button is pressed. The buttons will be places where the back should not be in so the servo motor can be activated. (There is also one button that moves two servo motors.) So I also built a app on MIT app inventor to control the speed of the servo motor. How would I do that with this code? I also wanted to see how would I stop the servo motor after a bit because they were just being turned off and on with the push button. I put a delay for around a second and a half, then just but the servo write to 0.
Here The code:
#include <Servo.h>
// constants won't change
//First Circuit
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin
//Second Circuit
const int BUTTON1_PIN = 6; // Arduino pin connected to button's pin
const int SERVO1_PIN = 10; // Arduino pin connected to servo motor's pin
//Third Circuit
const int BUTTON2_PIN = 5; // Arduino pin connected to button's pin
const int SERVO2_PIN = 11; // Arduino pin connected to servo motor's pin
const int SERVO3_PIN = 12; // Arduino pin connected to servo motor's pin
Servo servo; // create servo object to control a servo
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
Servo servo3; // create servo object to control a servo
// variables will change:
int angle = 0; // the current angle of servo motor
//First Circuit
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
//Second Circuit
int lastButtonState1; // the previous state of button
int currentButtonState1; // the current state of button
//Thrid Circuit
int lastButtonState2; // the previous state of button
int currentButtonState2; // the current state of button
void setup() {
Serial.begin(9600); // initialize serial
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo1.attach(SERVO1_PIN);
servo2.attach(SERVO2_PIN);
servo3.attach(SERVO3_PIN);
servo.write(angle);
servo1.write(angle);
servo2.write(angle);
servo3.write(angle);
currentButtonState = digitalRead(BUTTON_PIN);
currentButtonState1 = digitalRead(BUTTON1_PIN);
currentButtonState2 = digitalRead(BUTTON1_PIN);
}
void loop() {
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");
// change angle of servo motor
if(angle == 0)
angle = 90;
else
if(angle == 90)
angle = 0;
// control servo motor arccoding to the angle
servo.write(angle);
delay(1500);
servo.write(0);
}
lastButtonState1 = currentButtonState1; // save the last state
currentButtonState1 = digitalRead(BUTTON1_PIN); // read new state
if(lastButtonState1 == HIGH && currentButtonState1 == LOW) {
Serial.println("The button is pressed");
// change angle of servo motor
if(angle == 0)
angle = 90;
else
if(angle == 90)
angle = 0;
// control servo motor arccoding to the angle
servo1.write(angle);
delay(1500);
servo1.write(0);
}
lastButtonState2 = currentButtonState2; // save the last state
currentButtonState2 = digitalRead(BUTTON2_PIN); // read new state
if(lastButtonState2 == HIGH && currentButtonState2 == LOW) {
Serial.println("The button is pressed");
// change angle of servo motor
if(angle == 0)
angle = 90;
else
if(angle == 90)
angle = 0;
// control servo motor arccoding to the angle
servo2.write(angle);
servo3.write(angle);
delay(1500);
servo2.write(0);
servo3.write(0);
}
}