I'm doing a project and I'm trying to make a servo turn using two button, an LRD, and a sound sensor. I have three different codes for each componant but I am having a lot of trouble putting them all in one main code. I put all the three codes below. Thank you!
Buttons:
#include <Servo.h>
Servo myservo; // name servo
int angle =90; // initial angle for servo
int angleStep =5; //speed
int redLED=4;
int greenLED=5;
#define greenButton 2 // pin 2 is connected to green button
#define redButton 3 // pin 3 is connected to red button
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(greenButton,INPUT_PULLUP); // assign pin 12 as input for Left button.
pinMode(redButton,INPUT_PULLUP);// assing pin 2 as input for right button.
pinMode(greenLED,OUTPUT);
pinMode(redLED, OUTPUT);
myservo.write(angle);// send servo to the middle at 90 degrees (initial angle)
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
}
void loop() {
while(digitalRead(redButton) == LOW){
if (angle > 20 && angle <= 100) {
angle = angle - angleStep;
if(angle < 20){
angle = 20;
}else{
myservo.write(angle); // move the servo to desired angle
}
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
}
delay(5); // waits for the servo to get there
}// while
while(digitalRead(greenButton) == LOW){
if (angle >= 20 && angle <= 100) {
angle = angle + angleStep;
if(angle >100){
angle =100;
}else{
myservo.write(angle); // move the servo to desired angle
}
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
}
delay(5); // waits for the servo to get there
}//
}
LRD:
#include <Servo.h> // include Servo library
Servo myservo; // horizontal servo
int servoh= 90; // stand horizontal servo
int ldrlt = 0; //LDR top left
int ldrrt = 1; //LDR top rigt
int ldrld = 2; //LDR down left
int ldrrd = 3; //ldr down rigt
void setup()
{
Serial.begin(9600);
// servo connections
// name.attacht(pin);
myservo.attach(9);
}
void loop()
{
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
int dtime = analogRead(4)/20; // read potentiometers
int tol = analogRead(5)/4;
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dhoriz = avl - avr;// check the diffirence og left and rigt
if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
{
if (avl > avr)
{
servoh = --servoh;
if (servoh < 0)
{
servoh = 0;
}
}
else if (avl < avr)
{
servoh = ++servoh;
if (servoh > 180)
{
servoh = 180;
}
}
else if (avl == avr)
{
// nothing
}
myservo.write(servoh);
}
delay(dtime);
}
Sound sensor:
#include <Servo.h>
Servo servo;
/*****************************************
* global variables *
*****************************************/
const byte analog_pin = A2; //connected to envelope pin of
//sparkfun sound detector
const byte threshold = 70; //value determined through //experimentation
int servo_pin = 9;
int lamp_state = 0; //stores current lamp_state
int greenLED = 5;// 0 for OFF, 1 for ON
int redLED = 4;
/****************************************
* setup *
****************************************/
void setup(){
pinMode(analog_pin, INPUT);
servo.attach(servo_pin);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
}
/****************************************
* main loop *
****************************************/
void loop(){
int analog_value = analogRead(analog_pin); //read and store envelope //value from sf sound detector
//if analog value is greater than threshold
if(analog_value > threshold){
lamp_state = !lamp_state; //toggle value of lamp state variable
//if lamp_state was 0, flip it to 1
// if lamp_state was 1, flip it to 0
if(!servo.attached()){ //if servo not attached, attach it
servo.attach(9);
}
if(lamp_state == 1){ //if lamp_state is 1,
//switch ON
delay(500); //helps reduce false triggering
//servo.write values determined through experiment
//specific linkage design, length, etc.,
//will dictate the ON/OFF write values needed
//1)mount servo
//2) disconnect linkage from switch
//3) snap fingers
//4) note distance and direction servo moves linkage when
//e.g., 45, 90, etc., are written to servo
//increase/decrease write values until
//the correct "throw" is achieved
servo.write(100); //lamp ON
delay(1000); //helps to prevent false triggering
servo.detach();
digitalWrite(greenLED,LOW);
digitalWrite(redLED,HIGH);
}
//
else{
servo.write(20); //lamp OFF
delay(1000);
servo.detach();
digitalWrite(greenLED,HIGH);
digitalWrite(redLED,LOW);
}
}
} // <-------- end of main loop
Welcome to the forum. Please read the instructions at the top of the forum about how to properly post your code using code tags. It will help others help you.
All I see are three different sketches. Please post your attempt at putting them together, along with any errors you are getting as we can help you.
Sorry for the incovenience, here is our attempt. When we put them together, our LED lights are on at the same time, the buttons work but the sound sensor and the LRDs don't work.
#include <Servo.h>
Servo myservo; // name servo
int angle =90; // initial angle for servo
int angleStep =5; //speed
int redLED=4;
int greenLED=5;
int servoh= 90;
int ldrlt = 0; //LDR top left
int ldrrt = 1; //LDR top rigt
int ldrld = 2; //LDR down left
int ldrrd = 3; //ldr down rigt
const byte analog_pin = A2;
const byte threshold = 200;
int servo_pin = 9;
int lamp_state = 0;
#define greenButton 2 // pin 2 is connected to green button
#define redButton 3 // pin 3 is connected to red button
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(greenButton,INPUT_PULLUP); // assign pin 12 as input for Left button.
pinMode(redButton,INPUT_PULLUP);// assing pin 2 as input for right button.
pinMode(greenLED,OUTPUT);
pinMode(redLED, OUTPUT);
myservo.write(angle);// send servo to the middle at 90 degrees (initial angle)
pinMode(analog_pin, INPUT);
myservo.attach(servo_pin);
}
void loop() {
int analog_value = analogRead(analog_pin);
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
int dtime = analogRead(4)/20; // read potentiometers
int tol = analogRead(5)/4;
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dhoriz = avl - avr;// check the diffirence og left and rigt
if(digitalRead(redButton) == LOW){
if (angle > 20 && angle <= 100) {
angle = angle - angleStep;
if(angle < 20){
angle = 20;
}else{
myservo.write(angle); // move the servo to desired angle
}
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
} delay(5); // waits for the servo to get there
}
else if (-1*tol > dhoriz || dhoriz > tol) {// check if the diffirence is in the tolerance else change horizontal angle
if (avl > avr){
servoh = --servoh;
if (servoh < 0){
servoh = 0;
}
}
else if (avl < avr){
servoh = ++servoh;
if (servoh > 180){
servoh = 180;
}
}
myservo.write(servoh);
}
delay(dtime);
if(digitalRead(greenButton) == LOW){
if (angle >= 20 && angle <= 100) {
angle = angle + angleStep;
if(angle >100){
angle =100;
}else{
myservo.write(angle); // move the servo to desired angle
}
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
}
delay(5);
}
else if(analog_value > threshold){
lamp_state = !lamp_state;
if(!myservo.attached()){
myservo.attach(9);
if(lamp_state == 1){
delay(500);
myservo.write(100); //lamp ON
delay(250);
myservo.detach();
digitalWrite(greenLED,LOW);
digitalWrite(redLED,HIGH);
}
else{
myservo.write(20); //lamp OFF
delay(250);
myservo.detach();
digitalWrite(greenLED,HIGH);
digitalWrite(redLED,LOW);
}
}
}
}
Your all 3 separate sketches are combined and compiled without error. Check that A2-pin of Sound Control sketch is now changed to A6-pin A4-pin. Please, check the functionality of the program.
#include <Servo.h>
Servo myservo; // name servo
int angle = 90; // initial angle for servo
int angleStep = 5; //speed
int redLED = 4;
int greenLED = 5;
int servoh = 90; // stand horizontal servo
int ldrlt = 0; //LDR top left
int ldrrt = 1; //LDR top rigt
int ldrld = 2; //LDR down left
int ldrrd = 3; //ldr down rigt
const byte analog_pin = A6;//A2; //connected to envelope pin of
const byte threshold = 70; //value determined through
int lamp_state = 0; //stores current lamp_state
#define greenButton 2 // pin 2 is connected to green button
#define redButton 3 // pin 3 is connected to red button
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(greenButton, INPUT_PULLUP); // assign pin 12 as input for Left button.
pinMode(redButton, INPUT_PULLUP); // assing pin 2 as input for right button.
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
myservo.write(angle);// send servo to the middle at 90 degrees (initial angle)
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
}
void loop()
{
buttonControl();
ldrControl();
soundControl();
}
void buttonControl()
{
while (digitalRead(redButton) == LOW)
{
if (angle > 20 && angle <= 100)
{
angle = angle - angleStep;
if (angle < 20)
{
angle = 20;
}
else
{
myservo.write(angle); // move the servo to desired angle
}
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
}
delay(5); // waits for the servo to get there
}// while
while (digitalRead(greenButton) == LOW)
{
if (angle >= 20 && angle <= 100)
{
angle = angle + angleStep;
if (angle > 100)
{
angle = 100;
} else
{
myservo.write(angle); // move the servo to desired angle
}
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
}
delay(5); // waits for the servo to get there
}//
}
//--------------------------------------
void ldrControl()
{
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
int dtime = analogRead(4) / 20; // read potentiometers
int tol = analogRead(5) / 4;
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dhoriz = avl - avr;// check the diffirence og left and rigt
if (-1 * tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
{
if (avl > avr)
{
servoh = --servoh;
if (servoh < 0)
{
servoh = 0;
}
}
else if (avl < avr)
{
servoh = ++servoh;
if (servoh > 180)
{
servoh = 180;
}
}
else if (avl == avr)
{
// nothing
}
myservo.write(servoh);
}
delay(dtime);
}
//-------------------------------------------
void soundControl()
{
int analog_value = analogRead(analog_pin); //read and store envelope //value from sf sound detector
//if analog value is greater than threshold
if (analog_value > threshold)
{
lamp_state = !lamp_state; //toggle value of lamp state variable
//if lamp_state was 0, flip it to 1
// if lamp_state was 1, flip it to 0
if (!myservo.attached())
{ //if servo not attached, attach it
myservo.attach(9);
}
if (lamp_state == 1)
{ //if lamp_state is 1,
//switch ON
delay(500); //helps reduce false triggering
//servo.write values determined through experiment
//specific linkage design, length, etc.,
//will dictate the ON/OFF write values needed
//1)mount servo
//2) disconnect linkage from switch
//3) snap fingers
//4) note distance and direction servo moves linkage when
//e.g., 45, 90, etc., are written to servo
//increase/decrease write values until
//the correct "throw" is achieved
myservo.write(100); //lamp ON
delay(1000); //helps to prevent false triggering
myservo.detach();
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
}
//
else
{
myservo.write(20); //lamp OFF
delay(1000);
myservo.detach();
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, LOW);
}
}
} // <-------- end of main loop
I am trying to make a servo motor turn in two different directions using two buttons, LRD and sound sensor. I have three seperate codes for each componant but I am having a lot of trouble putting them all in one main code. When I tried only the buttons worked and both the LED lights are on at the same time. Here is the code that i tried with all three codes. Thank you!
#include <Servo.h>
Servo myservo; // name servo
int angle =90; // initial angle for servo
int angleStep =5; //speed
int redLED=4;
int greenLED=5;
int servoh= 90;
int ldrlt = 0; //LDR top left
int ldrrt = 1; //LDR top rigt
int ldrld = 2; //LDR down left
int ldrrd = 3; //ldr down rigt
const byte analog_pin = A2;
const byte threshold = 200;
int servo_pin = 9;
int lamp_state = 0;
#define greenButton 2 // pin 2 is connected to green button
#define redButton 3 // pin 3 is connected to red button
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(greenButton,INPUT_PULLUP); // assign pin 12 as input for Left button.
pinMode(redButton,INPUT_PULLUP);// assing pin 2 as input for right button.
pinMode(greenLED,OUTPUT);
pinMode(redLED, OUTPUT);
myservo.write(angle);// send servo to the middle at 90 degrees (initial angle)
pinMode(analog_pin, INPUT);
myservo.attach(servo_pin);
}
void loop() {
int analog_value = analogRead(analog_pin);
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
int dtime = analogRead(4)/20; // read potentiometers
int tol = analogRead(5)/4;
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dhoriz = avl - avr;// check the diffirence og left and rigt
if(digitalRead(redButton) == LOW){
if (angle > 20 && angle <= 100) {
angle = angle - angleStep;
if(angle < 20){
angle = 20;
}else{
myservo.write(angle); // move the servo to desired angle
}
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
} delay(5); // waits for the servo to get there
}
else if (-1*tol > dhoriz || dhoriz > tol) {// check if the diffirence is in the tolerance else change horizontal angle
if (avl > avr){
servoh = --servoh;
if (servoh < 0){
servoh = 0;
}
}
else if (avl < avr){
servoh = ++servoh;
if (servoh > 180){
servoh = 180;
}
}
myservo.write(servoh);
}
delay(dtime);
if(digitalRead(greenButton) == LOW){
if (angle >= 20 && angle <= 100) {
angle = angle + angleStep;
if(angle >100){
angle =100;
}else{
myservo.write(angle); // move the servo to desired angle
}
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
}
delay(5);
}
else if(analog_value > threshold){
lamp_state = !lamp_state;
if(!myservo.attached()){
myservo.attach(9);
if(lamp_state == 1){
delay(500);
myservo.write(100); //lamp ON
delay(250);
myservo.detach();
digitalWrite(greenLED,LOW);
digitalWrite(redLED,HIGH);
}
else{
myservo.write(20); //lamp OFF
delay(250);
myservo.detach();
digitalWrite(greenLED,HIGH);
digitalWrite(redLED,LOW);
}
}
}
}
Thank you so much for your time and help. I didn' know you could add void loops, but there's a small problem that I can't seem to understand: the buttons and the LED lights are working perfectly but the sound sensor and the LDR aren't. I verified all the wiring and it seems to be ok, do you know any solutions?
Before trying to merge the programs go through each of them carefully to make sure they are not trying to use the same resource - for example an I/O pin. If you find conflicts change something in one of the separate programs and make sure it still works with the change.
I am new to Arduio and I've been trying to merge three sketches in one main code. I've been working on it for hours and I still can't get it to work. The first code consists of two buttons that control the servo motor and LEDs light up when the servo reaches the correct position, the second is the same but instead of buttons I used two light sensors, and the third is the same but with a sound sensor(I clap). Here is an attempt.
#include <Servo.h>
Servo myservo; // name servo
int angle = 90; // initial angle for servo
int angleStep = 5; //speed
int redLED = 4;
int greenLED = 5;
int servoh = 90; // stand horizontal servo
int ldrlt = 0; //LDR top left
int ldrrt = 1; //LDR top rigt
int ldrld = 2; //LDR down left
int ldrrd = 3; //ldr down rigt
const byte analog_pin = A4;//A2; //connected to envelope pin of
const byte threshold = 70; //value determined through
int lamp_state = 0; //stores current lamp_state
#define greenButton 2 // pin 2 is connected to green button
#define redButton 3 // pin 3 is connected to red button
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(greenButton, INPUT_PULLUP); // assign pin 12 as input for Left button.
pinMode(redButton, INPUT_PULLUP); // assing pin 2 as input for right button.
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
myservo.write(angle);// send servo to the middle at 90 degrees (initial angle)
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
}
void loop()
{
buttonControl();
ldrControl();
soundControl();
}
void buttonControl()
{
while (digitalRead(redButton) == LOW)
{
if (angle > 20 && angle <= 100)
{
angle = angle - angleStep;
if (angle < 20)
{
angle = 20;
}
else
{
myservo.write(angle); // move the servo to desired angle
}
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
}
delay(5); // waits for the servo to get there
}// while
while (digitalRead(greenButton) == LOW)
{
if (angle >= 20 && angle <= 100)
{
angle = angle + angleStep;
if (angle > 100)
{
angle = 100;
} else
{
myservo.write(angle); // move the servo to desired angle
}
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
}
delay(5); // waits for the servo to get there
}//
}
//--------------------------------------
void ldrControl()
{
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
int dtime = analogRead(4) / 20; // read potentiometers
int tol = analogRead(5) / 4;
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dhoriz = avl - avr;// check the diffirence og left and rigt
if (-1 * tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
{
if (avl > avr)
{
servoh = --servoh;
if (servoh < 0)
{
servoh = 0;
}
}
else if (avl < avr)
{
servoh = ++servoh;
if (servoh > 180)
{
servoh = 180;
}
}
else if (avl == avr)
{
// nothing
}
myservo.write(servoh);
}
delay(dtime);
}
//-------------------------------------------
void soundControl()
{
int analog_value = analogRead(analog_pin); //read and store envelope //value from sf sound detector
//if analog value is greater than threshold
if (analog_value > threshold)
{
lamp_state = !lamp_state; //toggle value of lamp state variable
//if lamp_state was 0, flip it to 1
// if lamp_state was 1, flip it to 0
if (!myservo.attached())
{ //if servo not attached, attach it
myservo.attach(9);
}
if (lamp_state == 1)
{ //if lamp_state is 1,
//switch ON
delay(500); //helps reduce false triggering
//servo.write values determined through experiment
//specific linkage design, length, etc.,
//will dictate the ON/OFF write values needed
//1)mount servo
//2) disconnect linkage from switch
//3) snap fingers
//4) note distance and direction servo moves linkage when
//e.g., 45, 90, etc., are written to servo
//increase/decrease write values until
//the correct "throw" is achieved
myservo.write(100); //lamp ON
delay(1000); //helps to prevent false triggering
myservo.detach();
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
}
//
else
{
myservo.write(20); //lamp OFF
delay(1000);
myservo.detach();
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, LOW);
}
}
} // <-------- end of main loop