This code isn't working. The remote works but the button doesn't. I am trying to make a remote light that can also be controlled by a button. There are two servos, one button, and one IR receiver. any help would be appreciated. Thank you!
#include <Servo.h>
#include <ezButton.h>
#include <IRremote.h>
int IRpin = 11;
const int buttonpin = 7;
IRrecv irrecv(IRpin);
decode_results results;
Servo myservo;
Servo myservo1;
ezButton button(buttonpin);
int servoangle = 0;
int servoangle1 = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // initialize serial
button.setDebounceTime(50);
irrecv.enableIRIn(); // Start the receiver
myservo.attach(9);
myservo1.attach(8);
myservo.write(servoangle);
myservo1.write(servoangle1);
}
void loop() {
button.loop(); // MUST call the loop() function first
if(button.isPressed()) {
if(servoangle == 0)
servoangle = 90;
else
if(servoangle == 90)
servoangle = 0;
if(servoangle1 == 90)
servoangle1 = 0;
else
if(servoangle1 == 0)
servoangle1 = 90;
}
if (irrecv.decode(&results))
{
irrecv.resume(); // Receive the next value
}
if (results.value == 2704) // change according to your IR remote button number
{
myservo.write(0);
delay(15);
}
if (results.value == 16) // change according to your IR remote button number
{
myservo.write(180);
delay(15);
}
if (results.value == 16) // change according to your IR remote button number
{
myservo1.write(180);
delay(15);
}
if (results.value == 2704) // change according to your IR remote button number
{
myservo1.write(0);
delay(15);
}
}
Likely because the button is not wired correctly. Schematic please.
the button is on a breadboard with two wires I can confirm are connected to it going to pin 7 and to ground.
I based it on these two codes:
#include <Servo.h>
#include <ezButton.h>
// constants won't change
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
ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7;
Servo servo; // create servo object to control a servo
// variables will change:
int angle = 0; // the current angle of servo motor
void setup() {
Serial.begin(9600); // initialize serial
button.setDebounceTime(50); // set debounce time to 50 milliseconds
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(angle);
}
void loop() {
button.loop(); // MUST call the loop() function first
if(button.isPressed()) {
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);
}
}
and this one:
#include <IRremote.h>
#include <Servo.h>
int IRpin = 11; // pin for the IR sensor
IRrecv irrecv(IRpin);
decode_results results;
Servo myservo;
Servo myservo1;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo1.attach(8);
}
void loop()
{
if (irrecv.decode(&results))
{
irrecv.resume(); // Receive the next value
}
if (results.value == 0xFFA25D) // change according to your IR remote button number
{
myservo.write(0);
delay(15);
}
if (results.value == 0xFFFFFFFF ) // change according to your IR remote button number
{
myservo.write(180);
delay(15);
}
if (results.value == 0xFFFFFFFF ) // change according to your IR remote button number
{
myservo1.write(180);
delay(15);
}
if (results.value == 0xFFA25D) // change according to your IR remote button number
{
myservo1.write(0);
delay(15);
}
}
Nevermind, I fixed it, and on the slight chance someone will want this code, here it is:
#include <Servo.h>
#include <ezButton.h>
#include <IRremote.h>
int IRpin = 11;
const int buttonpin = 13;
const int servo_pin = 9;
const int servo_pin1 = 8;
IRrecv irrecv(IRpin);
decode_results results;
Servo servo;
Servo servo1;
ezButton button(buttonpin);
int servoangle = 0;
int servoangle1 = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // initialize serial
button.setDebounceTime(50);
irrecv.enableIRIn(); // Start the receiver
servo.attach(servo_pin);
servo1.attach(servo_pin1);
servo.write(servoangle);
servo1.write(servoangle1);
}
void loop() {
button.loop(); // MUST call the loop() function first
if(button.isPressed()) {
Serial.println("The button is pressed");
if(servoangle == 0)
servoangle = 180;
else
if(servoangle == 180)
servoangle = 0;
servo.write(servoangle);
}
if(button.isPressed()) {
if(servoangle1 == 180)
servoangle1 = 0;
else
if(servoangle1 == 0)
servoangle1 = 180;
servo1.write(servoangle1);
}
if (irrecv.decode(&results))
{
irrecv.resume(); // Receive the next value
}
if (results.value == 2704) // change according to your IR remote button number
{
servo.write(0);
delay(15);
}
if (results.value == 16) // change according to your IR remote button number
{
servo.write(180);
delay(15);
}
if (results.value == 16) // change according to your IR remote button number
{
servo1.write(180);
delay(15);
}
if (results.value == 2704) // change according to your IR remote button number
{
servo1.write(0);
delay(15);
}
}