How to make 2 touch sensors move one servo? - arduino

I want to make 2 touch sensors do the same thing which is to move the servo.
If either touch sensor is touched the motor moves up or down depending on it's previous position.
The second touch sensor should be named TOUCH_SENSOR_PIN2
here is the code:
#include <Servo.h>
const int TOUCH_SENSOR_PIN = 6; // Arduino pin connected to touch sensor's pin
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin
Servo servo; // create servo object to control a servo
// variables will change:
int angle = 141; // the current angle of servo motor
int lastTouchState; // the previous state of touch sensor
int currentTouchState; // the current state of touch sensor
void setup() {
Serial.begin(9600); // initialize serial
pinMode(TOUCH_SENSOR_PIN, INPUT);
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(angle);
currentTouchState = digitalRead(TOUCH_SENSOR_PIN);
}
void loop() {
lastTouchState = currentTouchState; // save the last state
currentTouchState = digitalRead(TOUCH_SENSOR_PIN); //read new state
if(lastTouchState == LOW && currentTouchState == HIGH) {
// change angle of servo motor
if(angle == 141)
angle = 156;
else
if(angle == 156)
angle = 141;
// control servo motor arccoding to the angle
servo.write(angle);
}
}

I haven't tried it, but this should work. Please replace <pin number> with the number of the pin to which your second touch sensor is connected.
#include <Servo.h>
const int TOUCH_SENSOR_PIN = 6; // Arduino pin connected to touch sensor's pin
const int TOUCH_SENSOR_PIN2 = <pin number>;
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin
Servo servo; // create servo object to control a servo
// variables will change:
int angle = 141; // the current angle of servo motor
int lastTouchState; // the previous state of touch sensor
int currentTouchState; // the current state of touch sensor
int lastTouchState2; // the previous state of touch sensor 2
int currentTouchState2; // the current state of touch sensor 2
void setup() {
Serial.begin(9600); // initialize serial
pinMode(TOUCH_SENSOR_PIN, INPUT);
pinMode(TOUCH_SENSOR_PIN2, INPUT);
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(angle);
currentTouchState = digitalRead(TOUCH_SENSOR_PIN);
currentTouchState2 = digitalRead(TOUCH_SENSOR_PIN2);
}
void loop() {
lastTouchState = currentTouchState; // save the last state
currentTouchState = digitalRead(TOUCH_SENSOR_PIN); //read new state
lastTouchState2 = currentTouchState2; // save the last state
currentTouchState2 = digitalRead(TOUCH_SENSOR_PIN2); //read new state
if((lastTouchState == LOW && currentTouchState == HIGH) || (lastTouchState2 == LOW && currentTouchState2 == HIGH)) {
// change angle of servo motor
if(angle == 141)
angle = 156;
else
if(angle == 156)
angle = 141;
// control servo motor arccoding to the angle
servo.write(angle);
}
}

Related

Servo Speed Control

How can i make my servo do this but slower?
if(angle == 140)
angle = 159;
else
if(angle == 159)
angle = 140;
This is the whole code:
#include <Servo.h>
const int TOUCH_SENSOR_PIN = 6; // Arduino pin connected to touch sensor's pin
const int TOUCH_SENSOR_PIN2 = 7 ;
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin
Servo servo; // create servo object to control a servo
// variables will change:
int angle = 140; // the current angle of servo motor
int lastTouchState; // the previous state of touch sensor
int currentTouchState; // the current state of touch sensor
int lastTouchState2; // the previous state of touch sensor 2
int currentTouchState2; // the current state of touch sensor 2
void setup() {
Serial.begin(9600); // initialize serial
pinMode(TOUCH_SENSOR_PIN, INPUT);
pinMode(TOUCH_SENSOR_PIN2, INPUT);
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(angle);
currentTouchState = digitalRead(TOUCH_SENSOR_PIN);
currentTouchState2 = digitalRead(TOUCH_SENSOR_PIN2);
}
void loop() {
lastTouchState = currentTouchState; // save the last state
currentTouchState = digitalRead(TOUCH_SENSOR_PIN); //read new state
lastTouchState2 = currentTouchState2; // save the last state
currentTouchState2 = digitalRead(TOUCH_SENSOR_PIN2); //read new state
if((lastTouchState == LOW && currentTouchState == HIGH) || (lastTouchState2 == LOW && currentTouchState2 == HIGH)) {
if(angle == 140)
angle = 159;
else
if(angle == 159)
angle = 140;
// control servo motor arccoding to the angle
servo.write(angle);
} }
you can control the speed of the servo by making it rotating several little steps instead of giving it the final demand angle, something like this.
if(angle <= 140){
for (; angle<= 150; angle += 1) {
myservo.write(angle);
delay(15);
}
}
else if(angle >= 150){
for (; angle >= 140; angle -= 1) {
myservo.write(angle);
delay(15);
}
}
you can adjust the speed by changing the delay value in both for-loops.
A full working example can be found in the examples provided by the Arduino IDE under Servo -> Sweep I guess.

Control 2 servo motors with 2 buttons Arduino

I'm working on another school project where I'm trying to make an obstacle course (model size). For this project I'm using 2 servo motors, which I want to control with 2 different buttons. So 1 button is connected to 1 servo motor and the other one is connected to the other servo. I'm actually struggling to get both buttons to work with the servo motors.
When I attach 1 button and 1 servo motor everything works exactly the way I want. I press the button, the servo motor moves 90 degrees and after 5 seconds it moves back.
The code:
#include <Servo.h>
Servo myservo;
const int servoPin = D8; // Servo pin
const int buttonPin = D7; // Pushbutton pin
void setup() {
myservo.attach(servoPin);
pinMode(buttonPin, INPUT);
}//setup
void loop() {
if (digitalRead(buttonPin) == HIGH) {
myservo.write(180);
delay(50); // waits 50ms to reach the position
delay(15000);//15 seconden wachten
myservo.write(0);
delay(50); // waits 50ms to reach the position
}
}//loop
However I read on a forum that when you want to use more then one servo motor, you have to write the code differently. You have to include servo motors like this:
#include <Servo.h>
Servo myservoa, myservob;
When I changed the code everything stopped working and I don't really understand what I'm doing wrong here. I want the servo motors to work AT THE SAME TIME, with 2 different buttons.
The new code:
#include <Servo.h>
Servo myservoa, myservob;
const int servoPin1 = D8; // Servo pin
const int servoPin2 = D6; // Servo pin
const int buttonPin1 = D7; // Pushbutton pin
const int buttonPin2 = D5; // Pushbutton pin
void setup() {
myservoa.attach(servoPin1);
myservob.attach(servoPin2);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}//setup
void loop() {
if (digitalRead(buttonPin1) == HIGH) {
myservoa.write(90);
delay(50); // waits 50ms to reach the position
delay(5000);// 5 seconden wachten
myservoa.write(0);
delay(50); // waits 50ms to reach the position
}
if (digitalRead(buttonPin2) == HIGH) {
myservob.write(90);
delay(50); // waits 50ms to reach the position
delay(5000);// 5 seconden wachten
myservob.write(0);
delay(50); // waits 50ms to reach the position
}
}//loop
I hope somebody can help me out!
EDIT:
So i found out that 2 servo motors actually was to much for my NodeMCU. The code in the comments worked fine tho! Now I'm trying to combine the servo motor with a small vibration motor. The 2 sensors work well together but I can't get the vibration motor to work properly.
I want the vibration motor to vibrate for 5 seconds after I pressed the button. After 5 seconds it has to stop automatically. With the code the vibration motor only vibrates when I press the button. When the button isn't pressed, the vibration motor stops directly.
Code:
#include <Servo.h>
Servo myservo;
const int servoPin = D8; // Servo pin
const int vibratiePin = D3; // Servo pin
const int buttonPin1 = D6; // Pushbutton pin
const int buttonPin2 = D5; // Pushbutton Pin
unsigned long stopA = 0;
unsigned long stopB = 0;
bool controlA = false;
bool controlB = false;
void setup() {
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
myservo.attach(servoPin);
pinMode(vibratiePin, OUTPUT);
}//setup
void loop() {
unsigned long now = millis();
if(controlA && stopA < now) {
myservo.write(0);
controlA = false;
} else if (!controlA && digitalRead(buttonPin1) == HIGH) {
controlA = true;
myservo.write(90);
stopA = millis() + 5000;
}
if(controlB && stopB < now) {
digitalWrite(vibratiePin, LOW);
controlB = false;
stopB = millis() + 5000;
} else if (!controlB && digitalRead(buttonPin2) == HIGH) {
controlB = true;
digitalWrite(vibratiePin, HIGH);
}
stopB = now;
}
I hope somebody can see the problem here because I don't understand what I'm doing wrong.
Your problem is that you are sleeping the code at each on if statement:
if (digitalRead(buttonPin1) == HIGH) {
...
delay(5000);// the code is blocked for 5 seconds here
...
}
So if the buttonPin1 is HIGH the code after that block will run just after 5100 milliseconds after (at least).
Indeed you shouldn't use longs delays like that inside the loop. The reason is what you see: while the code is stopped the motors are still running and it is a real dangerous scenario for real applications.
The solution is: don't sleep! There are several ways to do it. One of them is use a program variable to control the state of each of motors and another one to set the time limit to run the motor:
unsigned long stopA = 0;
unsigned long stopB = 0;
bool controlA = false;
bool controlB = false;
void loop() {
unsigned long now = mills();
if(controlA && stopA < now) {
myservoa.write(0);
controlA = false;
} else if (!controlA && digitalRead(buttonPin1) == HIGH) {
controlA = true;
myservoa.write(90);
stopA = mills() + 5050;
}
//similar to motor b
//...
}
This way the code never be blocked into a sleeping command and loop can perform other actions while each motor is running.

"Too many arguments" error

I am building a laser that is to be controlled by a joystick. The joystick uses 2 servo motors to move the laser in the direction I want. I am using an arduino board that uses C++ coding.
I get a "too many arguments" error on my outputs.
#include <Servo.h>
const int servo1 = 11; // first servo
const int servo2 = 10; // second servo
const int joy1 = 5; // switch 1
const int joy2 = 4; // switch 2
const int joy3 = 3; // switch 3
const int joy4 = 2; // switch 4
int servoVal; // variable to read the value from the digital pin
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
// Display Joystick values using the serial monitor
outputJoystick();
// Read the horizontal joystick value (value between 0 and 180)
servoVal = digitalRead(joy1, joy2, joy3, joy4);
servoVal = map(servoVal, 0, 45, 135, 180); // scale it to use it with the servo (result between 0 and 180)
myservo2.write(servoVal); // sets the servo position according to the scaled value
// Read the horizontal joystick value (value between 0 and 180)
servoVal = digitalRead(joy1, joy2, joy3, joy4);
servoVal = map(servoVal, 0, 45, 135, 180); // scale it to use it with the servo (result between 0 and 180)
myservo1.write(servoVal); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
/**
* Display joystick values
*/
void outputJoystick(){
Serial.print(digitalRead(joy1, joy2, joy3, joy4));
Serial.print ("---");
Serial.print(digitalRead(joy1, joy2, joy3, joy4));
Serial.println ("----------------");
}
Would the joystick actually give a digital value or should have been an analogRead? Also I'm pretty sure you can only sample one item at a time with the digitalRead.

continuous rotation servo (arduino) responding to button press

I am trying to make a continous rotation servo move clockwise if button on pin2 is pressed, and counterclockwise if button on pin3 is pressed. I want the servo to keep moving in the direction set according to the button until the button is released. This is the code I have so far (I am new to arduino):
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// CONSTANTS
// PINS
const int crServo = 12; // sets pin 12 as servo
const int buttonPinCW = 2; // sets pin 2 as button; CW => clockwise => FOCUS FAR
const int buttonPinCC = 3; // sets pin 3 as button; CC => counterclockwise => FOCUS NEAR
const int ledPin = 10; // sets pin 10 as LED
// SERVO PROPERTIES
const int crSpeedDefault = 1500; // 1500 is the stay still position, motor should not turn
const int crSpeedCW = 1300; // 1300 turns the motor full speed clockwise
const int crSpeedCC = 1700; // 1700 turns the motor full speed counter-clockwise
const int crStepDefault = 2;
// SET BUTTON STATES
int buttonStateCW = 0; //sets button 1 as off
int buttonStateCC = 0; // sets button 2 as off
void setup()
{
myservo.attach(crServo); // attaches the servo on pin 12 to the servo object
pinMode (buttonPinCW, INPUT); // sets button as input
pinMode (buttonPinCC, INPUT); // sets button as input
pinMode (ledPin, OUTPUT); // sets led as output
myservo.write(crSpeedDefault); // default servo to crSpeedDefault
}
int slowFocusPull(int x){
int result;
result = abs(x - crSpeedDefault) / crStepDefault;
return result;
}
void loop()
{
buttonStateCW = digitalRead(buttonPinCW);
buttonStateCC = digitalRead(buttonPinCC);
// clockwise rotation
if (buttonStateCW == HIGH) {
digitalWrite(ledPin, HIGH);
myservo.write(slowFocusPull(crSpeedCW));
// counterclockwise rotation
} else if (buttonStateCC == HIGH) {
digitalWrite(ledPin, HIGH);
myservo.write(slowFocusPull(crSpeedCC));
} else {
digitalWrite(ledPin, LOW);
}
}
The issue lies in the function slowFocusPull. Basically I just want to be able to adjust the speed with just modifying the constant. Without this function everything works fine.
UPDATE: final loop for reference
void loop()
{
buttonStateCW = digitalRead(buttonPinCW);
buttonStateCC = digitalRead(buttonPinCC);
// clockwise rotation
if (buttonStateCW == HIGH) {
digitalWrite(ledPinR, HIGH);
float speed = crSpeedCW;
Serial.print("CLOCKWISE-ROTATION \n");
for (int i = 0; i < t * 5; i++) {
speed += ((float)crSpeedDefault - speed)/ 10;
Serial.print(speed);
Serial.print("\n");
myservo.write((int)speed);
delay(100);
}
myservo.write(crSpeedCW);
}
else if (buttonStateCC == HIGH) {
digitalWrite(ledPinG, HIGH);
float speed = crSpeedCC;
Serial.print("COUNTER-CLOCKWISE-ROTATION \n");
for (int i = 0; i < t * 5; i++) {
speed += ((float)crSpeedDefault - speed) / 10;
Serial.print(speed);
Serial.print("\n");
myservo.write((int)speed);
delay(100);
}
myservo.write(crSpeedCC);
}
else {
myservo.write(crSpeedDefault);
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinG, LOW); // turn the LED off by making the voltage LOW
}
}
Looks like your project would benefit from using Hardware Interrupts, which asynchronously call functions when events (like button presses) occur (these are perfect for controllers, and remove the overhead of polling).
Try wiring up the two pins and wiring up the buttons to pins 2 and 3 as is shown in this diagram:
Hardware interrupts literally interrupt the code, the uno has two such pins: digital pin 2 and digital pin 3 (this is really useful for robotics, also the mega has 6 such pins!)
here's a skeleton for how your code might want to look
void setup() {
attachInterrupt(0, goClockwise, RISING); //the "0" places arduino uno's interrupt pin 2 (for uno r3)
attachInterrupt(1, goCounterClockwise, RISING); //the "1" places interrupt for arduino uno's pin 3
}
void loop() {
delay(1000); dummy delay, code is handled in interrupt functions
}
void goClockwise () {
//runs when pin 2's button is pressed
//code for making servo go clockwise
}
void goCounterClockwise () {
//code triggered when pin 3's button is pressed
//code for ccw goes here
}
If you have any questions on I'd be happy to work through them with you.
Here's a link to the Arduino ref page for hardware interrupts:
click here to learn more about arduino hardware interrupts

Arduino IR door

i want to make an IR sensor door with DFRobot IRsensor (switch) and Servo. the problem is in the if statement because the digitalread is always changing when there's nothing in front of the IR sensor which is the way it suppouse to be... so it rapidly closing and opening. but i want the door to be open when there's something in front of the sensor and then some delay, then its gonna close if there's something (new) in front of the door.
mycode:
#include <Servo.h>
Servo myservo;// create servo object to control a servo
// a maximum of eight servo objects can be created
int IRsensor =8;
int pos = 180; // variable to store the servo position
int d;
boolean kondisi;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode (IRsensor,INPUT);
Serial.begin (9600);
}
void loop()
{
d = digitalRead (IRsensor);
bukatutup (d);
delay (15);
}
void bukatutup(int IR)
{
Serial.println (IR);
if (IR == 0 and (kondisi == false))
{
pos = 0;
Serial.println ("terbuka");
myservo.write (pos);
kondisi == true;
delay(1000);
}
else if ( IR == 0 and (kondisi == true))
{
pos = 180;
Serial.println ("tertutup");
kondisi == false;
myservo.write (pos);
delay (1000);
}
}
Thx,

Resources