Servo rotating infinitely when I want it to do so only once - arduino

I am very new to Arduino. I want to make a simple setup, in which, pressing a button on a remote will make the servo rotate 90 degree and come back to 0.
Here is my code:
#include <Servo.h>
#include <IRremote.h>
int receiver = 13;
IRrecv irrecv(receiver);
decode_results results;
Servo myServo;
int pos = 0;
void setup() {
// put your setup code here, to run once:
myServo.attach(9);
Serial.begin(9600);
irrecv.enableIRIn();
myServo.write(0);
delay(200);
}
void loop() {
// put your main code here, to run repeatedly:
if (irrecv.decode(&results)){
if (results.value== 0xC0000C){
for (pos = 0; pos <= 90; pos += 1) {
// in steps of 1 degree
myServo.write(pos);
delay(15);
}
for (pos = 90; pos >= 0; pos -= 1) {
myServo.write(pos);
delay(15);
}
}
}
delay(100);
}
However, when I press the button assigned, the servo keeps swinging from 0 to 90 degree and back infinitely, but I only want it to do so once, each time the button is pressed. How do I do that?

You need to add the line
irrecv.resume();
to the end if loop just before that final delay in order to clear out the results and start looking for a new signal.
void loop() {
// put your main code here, to run repeatedly:
if (irrecv.decode(&results)){
if (results.value== 0xC0000C){
for (pos = 0; pos <= 90; pos += 1) {
// in steps of 1 degree
myServo.write(pos);
delay(15);
}
for (pos = 90; pos >= 0; pos -= 1) {
myServo.write(pos);
delay(15);
}
}
}
irrecv.resume();
delay(100);
}

Related

issue with Arduino using FastLed with IRremote change mode

Hi I am trying to build a arduino project to control led strip and change the light pattern when pressed different button
the code i am using is
#include <IRremote.h> //include the library
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 70
#define DATA_PIN 3
#define CLOCK_PIN 13
#define Button_0 0xFF9867
#define Button_1 0xFFA25D
#define Button_2 0xFF629D
#define Button_3 0xFFE21D
#define Button_4 0xFF22DD
#define Button_5 0xFF02FD
#define Button_6 0xFFC23D
#define Button_7 0xFFE01F
#define Button_8 0xFFA857
#define Button_9 0xFF906F
int receiver = 7; //initialize pin 13 as recevier pin.
IRrecv irrecv(receiver); //create a new instance of receiver
decode_results results;
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(9600);
LEDS.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
LEDS.setBrightness(84);
irrecv.enableIRIn(); //start the receiver
}
void loop() {
if (irrecv.decode(&results)) { //if we have received an IR signal
Serial.println (results.value, HEX); //display HEX results
irrecv.resume(); //next value
if (results.value == Button_0) {
RunningLightSlow();
}
else if(results.value == Button_1) {
RainbowLights();
}
// switch (results.value) {
//
// case Button_0: RunningLightSlow(); break;
// case Button_1: RainbowLights(); break;
// }
}
}
// LIGHTSHOW 0 starts --------------------------------------------------------------------------------------------------
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }
void RunningLightSlow() {
while(1) {
static uint8_t hue = 0;
Serial.print("x");
// First slide the led in one direction
for(int i = 0; i < NUM_LEDS; i++) {
// Set the i'th led to red
leds[i] = CHSV(hue++, 255, 255);
// Show the leds
FastLED.show();
// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
fadeall();
// Wait a little bit before we loop around and do it again
delay(10);
}
Serial.print("x");
// Now go in the other direction.
for(int i = (NUM_LEDS)-1; i >= 0; i--) {
// Set the i'th led to red
leds[i] = CHSV(hue++, 255, 255);
// Show the leds
FastLED.show();
// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
fadeall();
// Wait a little bit before we loop around and do it again
delay(10);
}
}
}
// LIGHTSHOW 0 ends --------------------------------------------------------------------------------------------------------
// LIGHTSHOW 1 STARTS ------------------------------------------------------------------------------------------------------
void RainbowLights() {
randomSeed(millis());
int wait=random(10,30);
int dim=random(4,6);
int max_cycles=8;
int cycles=random(1,max_cycles+1);
while(1) {
rainbowCycle(wait, cycles, dim);
}
}
void rainbowCycle(int wait, int cycles, int dim)
{
Serial.println("Let's make a rainbow.");
//loop several times with same configurations and same delay
for(int cycle=0; cycle < cycles; cycle++)
{
byte dir=random(0,2);
int k=255;
//loop through all colors in the wheel
for (int j=0; j < 256; j++,k--)
{
if(k<0)
{
k=255;
}
//Set RGB color of each LED
for(int i=0; i<NUM_LEDS; i++)
{
CRGB ledColor = wheel(((i * 256 / NUM_LEDS) + (dir==0?j:k)) % 256,dim);
leds[i]=ledColor;
}
FastLED.show();
FastLED.delay(wait);
}
}
}
CRGB wheel(int WheelPos, int dim)
{
CRGB color;
if (85 > WheelPos)
{
color.r=0;
color.g=WheelPos * 3/dim;
color.b=(255 - WheelPos * 3)/dim;;
}
else if (170 > WheelPos)
{
color.r=WheelPos * 3/dim;
color.g=(255 - WheelPos * 3)/dim;
color.b=0;
}
else
{
color.r=(255 - WheelPos * 3)/dim;
color.g=0;
color.b=WheelPos * 3/dim;
}
return color;
}
Now the issue i am facing is when i press a button (for eg 0) it runs the function RunningLightSlow(); i have written the infinite loop code because i want infinite light show and when i press button 1 it does not change the light pattern ,
Root cause would be once the function goes into infinite loop it then never receives the ir signal and hence never change the light pattern
Remove all infinite loops from your program and create a simple structure in loop() where you check for IR signal and update a "state" variable with the last button pressed.
void loop() {
// create a function or add here whatever is needed to read the IR code
irCode = readIrCode();
// check if the new code is identical to a previous code
if (lastIrCode != irCode) {
// memorize the current code
lastIrCode = irCode;
/ act according to user selection
switch (irCode) {
case Button_0:
doThis(); // this could be your RunningLightSlow()
break;
case Button_1:
doThat(); // maybe RainbowLights()
break;
default:
// for cases you have not handled above
doWhatever();
}
}
}
Do not forget to remove the infinite loops from the functions that run the lights and obviously you would need to declare the variables and set the correct calls.

Servo doesn't respond as expected

I am using an Arduino for the first time to control the level crossing gates on my model railway using an LDR and two servos. I want to have a train run over a photo resistor (LDR). On this event, the gates should close. When the train has passed over, there should be a delay and then the gates should open. The code presented here is simplified to just open the gates and close them without the required delays. As the LDR sends constant readings, the servos should only respond on the initial change as I don't want to manage servos pulling or pushing on gates once they have moved.
I have tried using a global to hold the gate state. I have now changed the code to pass a variable around.
#include <Servo.h>
Servo myservo;
int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
int pos = 1;
int barState;
void setup() {
Serial.begin(9600); //sets serial port for communication
myservo.attach(9);
barState = 0;
}
int upMove(int bs)
bs = 0;
for (pos = 0; pos <= 60; pos += 1) { // goes from 0 degrees to 60 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 30ms for the servo to reach the position
}
return bs;
}
int downMove(int bs)
{
bs = 1;
for (pos = 60; pos >= 0; pos -= 1) { // goes from 60 degrees to 0 degrees
// in steps of 1 degree
{ myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 30ms for the servo to reach the position
}
return bs;
}
int up(int bs)
{
if ( bs = 1) {
bs = upMove(bs);
}
return bs;
}
int down(int bs)
{
bs = downMove(bs);
}
return bs;
}
void loop() {
{ sensorValue = analogRead(sensorPin); //define photocellReading as pin 0 input from LDR
sensorValue = map(sensorValue, 0, 1023, 0, 179); //map the LDR input to a value between 1-180 so the servo can understand it
if (sensorValue > 90) //if the LDR is showing less than half light intensity
{
up(barState);
}
else if (sensorValue <= 90) //if the LDR is showing more than half light intensity
{
down(barState); //then tell the servo to rotate forwards at a steady rate
}
}
}
The code is working for Up but not for Down. Up works once but Down continually resets and operates the servo.
I have honed the code to make it a bit simpler. It now operates as expected except that it continues to operate the servo instead of just once for each change of the LDR
#include <Servo.h>
Servo myservo;
int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
int pos = 1;
int barState;
void setup() {
Serial.begin(9600); //sets serial port for communication
myservo.attach(9);
barState = 0;
}
void upMove()
{
Serial.print( " In upMove ");Serial.println(barState);
for (pos = 0; pos <= 60; pos += 1) { // goes from 0 degrees to 60 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 30ms for the servo to reach the position
barState = 0;
}
}
void downMove()
{
Serial.print( " In downMove ");Serial.println(barState);
for (pos = 60; pos >= 0; pos -= 1) { // goes from 0 degrees to 60 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 30ms for the servo to reach the position
barState = 0;
}
}
void up()
{
Serial.print( " In UP 1 ");Serial.println(barState);
{
if (barState = 1) upMove();
}
Serial.print( " In UP 2 ");Serial.println(barState);
}
void down()
{
Serial.print( " In DOWN 1 ");Serial.println(barState);
{
if (barState = 1) downMove();
}
Serial.print( " In DOWN 2 ");Serial.println(barState);
}
void loop() {
{ sensorValue = analogRead(sensorPin); //define photocellReading as pin 0 input from LDR
sensorValue = map(sensorValue, 0, 1023, 0, 179); //map the LDR input to a value between 1-180 so the servo can understand it
if (sensorValue > 90) //if the LDR is showing less than half light intensity
{
up();
}
else if (sensorValue <= 90) //if the LDR is showing more than half light intensity
{
down(); //then tell the servo to rotate forwards at a steady rate
}
}
}

Controlling Servos with Arduino

I am trying to control a small servo motor with an Arduino and I am running into a problem where the Servo continuously runs even when I want it to stop. I have pasted a picture of my code below. The for loops should make the Servo stop when it rotates 180 degrees, but it just keeps spinning.
I am trying to control the direction of the servos using the Serial monitor as input.
#include <Servo.h>
Servo myservo;
int i = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myservo.attach(9);
}
void loop() {
// put your main code here, to run repeatedly:
keyboardControl();
}
void keyboardControl() {
if (Serial.available() > 0) {
int data = Serial.read();
switch (data) {
case '1' :
for (i = 0; i <= 180; i += 1) {
myservo.write(i);
delay(15);
if (i = 180) {
break;
}
case '2':
for (i = 180; i >= 0; i -= 1) {
myservo.write(i);
delay(15);
break;
}
}
}
}
}
I would give this code a try:
void stop()
{
while(1 = 180);
}

Arduino servo and IR remote

I am trying to get a continuous (180 to 0 and back 0 to 180) movement from a servo when I press a button in remote and ONLY stop when I press the other button. So far, I have gotten it to move continuously, but then it doesn't stop when I press the 'stop' button. I know it is because of the while loop. However, I have tried switch-case, if statement, nothing has worked so far.
Please help, any advice for it make it work is appreciated.
#include <Servo.h>
#define code1 2534850111 //decimal value of button 1
#define code3 16724175 //decimal value of button 1
#define code 4294967295 //random value
#define code2 16738455 //decimal value of button 0
#define code4 3238126971 //decimal value of button 0
Servo myservo; // servo object
int RECV_PIN = 11; //receiveing pin IR remote
int pos = 0;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); //start the receiver
myservo.attach(9); //servo connect to pin 9
pinMode(2, OUTPUT); //LED connect to pin 2
}
void loop() {
if(irrecv.decode(&results)){
// if(results.value == code1 || results.value == code3){
while(results.value == code1 || results.value == code3){
digitalWrite(2,HIGH); //turn the led on
for(pos = 0; pos <= 180; pos += 1){ //servo goes form 0 to 180 degrees in steps of 1 degree
myservo.write(pos);
delay(7);
}
for(pos = 180; pos >= 0; pos -= 1){ //servo goes back from 180 to 0 degrees with 1 degree step
myservo.write(pos);
delay(7);
}
}
while(results.value == code2 || results.value == code4){
digitalWrite(2, LOW); // turn the led off
myservo.write(pos);
delay(15);
break;
}
Serial.println(results.value, DEC); //show the decimal value of the pressed button
irrecv.resume(); //receive the next value
}
}
One way to solve your problem would be to check for the presence of a "button push" deeper inside loop(). Put your checks for the button presses inside your movement for loop to catch the changes right away. Looks like you might have two startcodes (?) so you might have to alter the if statements below, but hopefully I demonstrate how to check the condition to "keep going" in the code sample below.
void loop()
{
if(irrecv.decode(&results))
{
// turn one way
for(pos = 0; pos <= 180; pos += 1)
{
// only continue if the start code(s) still active
if(results.value == STARTCODE || results.value == OTHERSTARTCODE)
{
myservo.write(pos);
delay(7);
irrecv.resume(); //receive the next value
}
}
// turn the other way
for(pos = 180; pos >= 0; pos -= 1)
{
// only continue if the start code(s) still active
if(results.value == STARTCODE || results.value == OTHERSTARTCODE)
{
myservo.write(pos);
delay(7);
irrecv.resume(); //receive the next value
}
}
}
}

arduino interrupts with servo motor

currently am working on project to open a door with access code using arduino UNO and a servo motor. Normal operation requires entering access code using keypad which is working fine. Another option requires pressing a button that causes an interrupt to rotate the servo motor. My problem is my interrupt only works once and never works again. Plus how do i put the for-loop to rotate the servo motor inside the interrupt function with a delay. I know that is not possible but am calling another function that has the delayMicroseconds but all this is not working. Below is my implementation please help
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Servo.h>
Servo servo;
const int openButtonPin = 2;
void setup() {
// put your setup code here, to run once:
servo.attach(5);
pinMode(openButtonPin, INPUT); //Pin 2 is input
attachInterrupt(0, enforceOpenAccess, HIGH); // PIN 2
}
void(* resetFunc)(void) = 0;
void loop()
{
//My other keypad implementations go here
}
void myDelay(int x) // function to cause delay in the interrupt
{
for(int i = 0; i<x; i++)
{
delayMicroseconds(1000);
}
}
void enforceOpenAccess() // ISR
{
for(int k =0; k<=180; k+=2)
{
servo.write(k); //rotate the servo
myDelay(30); //delay the rotation of the servo
}
}
The code above is run on arduino UNO being simulated in proteus and the interrupt button is a push button. Please if there is other ways of implementing that but with the same behaviour as I have described above help out. Thanks a lot
There are a couple of problems in the slice of code you posted. Just for completeness, you should post the loop function, since we can't guess what you wrote inside.
Just one comment: did you put a pullup? Otherwise use INPUT_PULLUP instead of INPUT for the button pinmode.
The main one is that you attached the interrupt for the HIGH mode, which will trigger the interrupt any time the pin is up, not on the rising edge. And please use the macro digitalPinToInterrupt to map to the correct pin:
attachInterrupt(digitalPinToInterrupt(openButtonPin), enforceOpenAccess, RISING);
Then.. Let's improve the code. You really should use the interrupts only when strictly necessary when you have to respond IMMEDIATELY (= less than a couple of milliseconds) to an input. Here you don't have to, so it's MUCH better to check for the button in the loop (more on turning the motor following)
uint8_t lastState;
void setup()
{
...
lastState = LOW;
}
void loop()
{
uint8_t currentState = digitalRead(openButtonPin);
if ((currentState != lastState) && (currentState == HIGH))
{
// Start turning the motor
}
lastState = currentState;
...
}
This will enable you to properly debounce the button too:
#include <Bounce2.h>
Bounce debouncer = Bounce();
void setup()
{
...
pinMode(openButtonPin, INPUT); //Pin 2 is input
debouncer.attach(openButtonPin);
debouncer.interval(5); // interval in ms
}
void loop()
{
debouncer.update();
if (debouncer.rose())
{
// Start turning the motor
}
...
}
If, on the other way, you REALLY want to use the interrupts (because waiting for a couple of milliseconds is too much for you), you should do something like this:
#include <Bounce2.h>
Bounce debouncer = Bounce();
void setup()
{
...
pinMode(openButtonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(openButtonPin), enforceOpenAccess, RISING);
}
void loop()
{
...
}
void enforceOpenAccess() // ISR
{
// Start turning the motor
}
It looks like your code? No, because now we'll speak about turning the motor
You should NOT use delays to make steps, because otherwise you will wait for 30ms * 180 steps = 5.4s before being able to do anything else.
You can, however, make a sort of reduced state machine. You want your servo to move from 0 to 180 in steps of 1. So let's code the "don't move" state with any value greater than 180, and consequently we can do something like this in the loop:
unsigned long lastServoTime;
uint8_t servoPosition = 255;
const int timeBetweenSteps_in_ms = 30;
void loop()
{
...
if (servoPosition <= 180)
{ // servo should move
if ((millis() - lastServoTime) >= timeBetweenSteps_in_ms)
{
lastServoTime += timeBetweenSteps_in_ms;
servoPosition++;
if (servoPosition <= 180)
servo.write(servoPosition);
}
}
}
Then, using any of the previous examples, instead of // Start turning the motor write
lastServoTime = millis();
servoPosition = 0;
servo.write(servoPosition);
This way you won't block the main loop even when the button is pressed
This is what is in my loop()
char key = keypad.getKey();
if(key)
{
if(j < 10)
{
studentNumber[j] = key;
//holdMaskedNumber[j] = '*';
lcd.setCursor(0,2);
lcd.print(String(studentNumber));
if(j == 9)
{
studentNumber[9] = '\0';
//holdMaskedNumber[9] = 0;
lcd.clear();
//String number = String(studentNumber);
//lcd.print(number);
//delay(1000);
//lcd.clear();
lcd.print("Access Code");
}
j++;
}
else
{
if(i < 5)
{
accessCode[i] = key;
holdMaskedCode[i] = '*';
lcd.setCursor(1,2);
lcd.print(String(holdMaskedCode));
if(i == 4)
{
holdMaskedCode[5] = '\0';
accessCode[5] = '\0';
//lcd.clear();
//lcd.setCursor(0,0);
//accessCodeString = String(accessCode);
//lcd.print(accessCodeString);
//delay(1000);
lcd.clear();
for(int i =0; i<6; i++)
{
lcd.print("Please wait.");
delay(500);
lcd.clear();
lcd.print("Please wait..");
delay(500);
lcd.clear();
lcd.print("Please wait...");
delay(500);
lcd.clear();
}
digitalWrite(4, HIGH);
lcd.print("Access Granted");
for(int k =0; k<=180; k+=2)
{
servo.write(k);
delay(30);
}
resetFunc();
}
i++;
}
}
}

Resources