Servos moving when not supposed to

Hey everyone, I have been working on a project that is supposed to move the servo motor back and forth one time per sensor detection to open then close a door. They sensor and motor are communicating through a NRF24L01 transceiver. The issue is that if the sensor detects something, it executes the code moving the servo multiple times at what seems like a random amount.

Transmitter code (has sensor):

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10); // CE, CSN

const byte address[6] = "GDoor"; //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.
int inPin = 8;
boolean inPin_state = 0;

void setup() {
Serial.begin(9600);
pinMode(inPin, INPUT);
radio.begin(); //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.stopListening(); //This sets the module as transmitter
}

void loop() {
if (digitalRead(inPin) == HIGH) {
inPin_state = HIGH;
}
else {
inPin_state = LOW;
}
radio.write(&inPin_state, sizeof(inPin_state)); //Sending the message to receiver
}

Receiver code (has servo):

#include <Servo.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

Servo myservo;
RF24 radio(9, 10); // CE, CSN

const byte address[6] = "GDoor";
boolean inPin_state = 0;

int pos = 0;
int outPin = 7;

void setup() {
pinMode(outPin, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
radio.startListening(); //This sets the module as receiver

myservo.attach(8);
}

void loop() {

if (radio.available()) { //Looking for the data
radio.read(&inPin_state, sizeof(inPin_state)); //Reading the data
if (inPin_state == HIGH) {
digitalWrite(outPin, HIGH);
for (pos = 50; pos <= 120; pos += 1) {
myservo.write(pos);
delay(10);
}
delay(5000);
for (pos = 120; pos >= 50; pos -= 1) {
myservo.write(pos);
delay(10);
}
}
else {
digitalWrite(outPin, LOW);
}
}
}

Please edit your post to add code tags, as described in the "How to get the best out of the forum" post, at the head of every topic.

Use Serial.print to verify that the data received is what you expect, and avoid using long delay() statements. The Arduino can't do anything else while executing them.

Do edit your post to place the two sketches into code tags. TIA.

Your sender sends the message over and over when the digitalRead() condition is met. As long as it is met.

So this is a classic case for sending the message only once, when the sensor value changes.

The way it is expressed here frequently is

when the button *becomes* pressed

not

while it *is* pressed

I found code on these fora here


and modified it a bit
int led = 5;
int button = 7;

int val = 0;
int old_val = 0;
int state = 0;

void setup ()
{
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop() { 
  val = digitalRead(button);

  if ( (val == HIGH) && (old_val == LOW)) {
    state = 1 - state;
  }

  old_val = val;

  if (state == 1) {
    digitalWrite(led, HIGH);
  }
  else {
    digitalWrite (led, LOW);
  }
}

and threw it into the wokwi:

Read it carefully to see how it works and try to backfit the logic into your sender sketch.

It's not tricky code at all, but it might help to play really dumb and put your finger on the code and trace through it to see how it works. Or woks in the simulation, haha.

NOTE: I used a bounceless switch, becuase I could and I don't think you'll need to officially debounce the sensor signal. And the delay() in your code will cover any glitches.

But we will see your next attempt and fix it so.

a7

Thank you so much. I've been troubleshooting this for 3 days. You're a legend

HTH.

So… if you can, post, in a reply on this thread, your sender and receiver sketches so the technique can be seen in action, so to speak.

Use the </> button in the button bar in the window where you compose the reply for each part, just hit the button and paste where it says.

Or use the IDE “copy for forum” on your sketches, I think it’s on the edit menu, and just paste here.

Also, in the IDE is a tool “autoformat”, that will put the code into a common “approved” format as far as indentation and stuff like that goes. Do that before you copy it, whichever way you do, from the IDE.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.