remote Control Bed

Hello, I've just started with arduino a couple of weeks ago and got stuck trying to write the code for a automatic bed.

The reason we want it on a remote is cause the person using the bed has a bad back and the cored controller falls on the flour constantly, this making his problem even worse trying to pick it up >:(

we want replicate the 6 functions that or on the currant controller, this being;

head up/head down
feet up /feet down
both up /both down

the way the electronics work is with 4 relays and 2 motors.
each motor has a up and a down relay.

The part i'm stuck at in particular is that i can't get the motor to run as long as im holding down the button on the remote and stopping the moment i release the button.

Here is where im at in the sketch, this is going from a pushbutton to remote control (but got stuck on the first on)(did not put both up/both down in the sketch yet)

#include <IRremote.h>
int RECV_PIN = 3;//The definition of the infrared receiver pin 5
IRrecv irrecv(RECV_PIN);
decode_results results;

define HEADUPIOPIN 13

define FEETUPIOPIN 12

define HEADDOWNIOPIN 11

define FEETDOWNIOPIN 10

int headUp = HEADUPIOPIN;
int feetUp = FEETUPIOPIN;
int headDown = HEADDOWNIOPIN;
int feetDown = FEETDOWNIOPIN;

const int headUpBtn = 4;
const int feetUpBtn = 5;
const int headDownBtn = 6;
const int feetDownBtn = 7;

int buttonState = 0;
int buttonState2 = 0;

void setup() {

pinMode(headUp, OUTPUT);
pinMode(feetUp, OUTPUT);
pinMode(headDown, OUTPUT);
pinMode(feetDown, OUTPUT);

pinMode(headUpBtn, INPUT);
pinMode(feetUpBtn, INPUT);
pinMode(headDownBtn, INPUT);
pinMode(feetDownBtn, INPUT);

irrecv.enableIRIn();
}

void loop() {

if (irrecv.decode(&results)) {
if (results.value == 0xC101E57B) //0
{
digitalWrite(headUp, LOW); //drive the relay is closed off
}
if (results.value == 0x9716BE3F) //1
{
digitalWrite(headUp, HIGH); //drive relay closure conduction
}
irrecv.resume(); // Receiving the next value
}

buttonState = digitalRead(feetUpBtn);

if (buttonState == HIGH)
{
digitalWrite(feetUp, LOW);
}
else
{
digitalWrite(feetUp, HIGH);
}

buttonState = digitalRead(headDownBtn);

if (buttonState == HIGH)
{
digitalWrite(headDown, LOW);
}
else
{
digitalWrite(headDown, HIGH);
}
buttonState = digitalRead(feetDownBtn);

if (buttonState == HIGH)
{
digitalWrite(feetDown, LOW);
}
else
{
digitalWrite(feetDown, HIGH);
}

}

Welcome to the forum.
It would help us to help you if you read this: How to use this forum - please read.
especially #7 about using code tags.

Made a few changes and explained it in the comments.

Good Luck and if you need more help just ask :slight_smile:

// # define HEADUPIOPIN 13     <<< This group is doing the same thing as the next group
// # define FEETUPIOPIN 12     <<< so lets get rid of these.
// # define HEADDOWNIOPIN 11   <<<
// # define FEETDOWNIOPIN 10   <<<

const byte headUp = 13;     // This group will never change 
const byte feetUp = 12;     // and never be larger than 255
const byte headDown = 11;   
const byte feetDown = 10;

const byte headUpBtn = 4;    // Close but agian they will never be
const byte feetUpBtn = 5;    // more than 255 so why waste memory
const byte headDownBtn = 6;
const byte feetDownBtn = 7;

byte headUpButtonState = 0;    
byte feetUpButtonState = 0;   // You need a separate variable
byte headDownButtonState = 0; // to track each button state
byte feetDownButtonState = 0;

Thanks for the reply and my apologies for moving to fast and reading the rules first.
Been working on this a lot and whilst learning code and english at the same time i let this one slip.

Ive made the adjustments but im still stuck as to how to get the relay activated only when the button on remote is held down.(and deactivated as soon as the button is released)

hnsr:
Thanks for the reply and my apologies for moving to fast and reading the rules first.
Been working on this a lot and whilst learning code and english at the same time i let this one slip.

Ive made the adjustments but im still stuck as to how to get the relay activated only when the button on remote is held down.(and deactivated as soon as the button is released)

If you check with the "IRrecvDemo" program, printing the received codes to the serial monitor, you'll possibly find that when you hold the button down, first you get the actual code, then you get 0xFFFFFFFF for each repeat of that code. (I found that some codes do that while others don't.)
You could handle that by storing a code when it's received, then each time you get 0xFFFFFFFF you carry out the same action as when the initial code was received.

How are you driving the motors in the first place? Maybe you should look at the stepper library.

cheesestring:
How are you driving the motors in the first place? Maybe you should look at the stepper library.

Or maybe not.

OldSteve:
Or maybe not.

Definitely not.

aarg:
Definitely not.

Yeah, I sort of suspected that a stepper library might not be the answer to driving those (presumably DC) motors via relay. :slight_smile:

They or DC motors with switch buttons in the hand controller, so the easiest way to change the buttons for relays.

Still can't get my head around how to program the arduino so it keeps looking for the incoming code from the remote and stops when i let go of the button.

Ive found out that my remote does not change the codes and keeps sending the same one thanks for the suggestion do.

hnsr:
They or DC motors with switch buttons in the hand controller, so the easiest way to change the buttons for relays.

We figured that was the case. You can ignore the suggestion to use a stepper library. :slight_smile:

Still can't get my head around how to program the arduino so it keeps looking for the incoming code from the remote and stops when i let go of the button.

Could you please post the latest version of your code? Between code tags, of course. And it's a good idea to format it in the IDE beforehand, using >Tools >Auto Format. That makes it easier to read. :slight_smile:

Ive found out that my remote does not change the codes and keeps sending the same one thanks for the suggestion do.

Good, that makes coding easier than having to handle 0xFFFFFFFF repeat codes. I just thought I'd better ask, in case that was what was causing your problem.

Hi, for the moment I've programmed it so it works like a start stop function.
it works but is not ideal for small adjustments :frowning:

#include <IRremote.h>
int RECV_PIN = 3;//The definition of the infrared receiver pin 5
IRrecv irrecv(RECV_PIN);
decode_results results;

int headUp = 12;
int feetUp = 11;
int headDown = 10;
int feetDown = 9;

void setup() {

  pinMode(headUp, OUTPUT);
  pinMode(feetUp, OUTPUT);
  pinMode(headDown, OUTPUT);
  pinMode(feetDown, OUTPUT);

  digitalWrite(headDown, HIGH);
  digitalWrite(feetDown, HIGH);
  digitalWrite(headUp, HIGH);
  digitalWrite(feetUp, HIGH);


  Serial.begin(9600);
  irrecv.enableIRIn();
}

void loop() {


  if (irrecv.decode(&results)) {
    if (results.value == 0xC26BF044) //head and feet up at the same time
    {
      digitalWrite(headUp, LOW);
      digitalWrite(feetUp, LOW);
      digitalWrite(headDown, HIGH);
      digitalWrite(feetDown, HIGH);
    }
    if (results.value == 0xC4FFB646) //head and feet down at the same time
    {
      digitalWrite(headDown, LOW);
      digitalWrite(feetDown, LOW);
      digitalWrite(headUp, HIGH);
      digitalWrite(feetUp, HIGH);
    }
    if (results.value == 0x758C9D82) //head and feet up at the same time
    {
      digitalWrite(feetUp, LOW);
      digitalWrite(headDown, HIGH);
      digitalWrite(feetDown, HIGH);
    }
    if (results.value == 0x53801EE8) //head and feet down at the same time
    {
      digitalWrite(headUp, LOW);
      digitalWrite(headDown, HIGH);
      digitalWrite(feetDown, HIGH);
    }
    if (results.value == 0x8AF13528) //head and feet down at the same time
    {
      digitalWrite(headDown, HIGH);
      digitalWrite(feetDown, HIGH);
      digitalWrite(headUp, HIGH);
      digitalWrite(feetUp, HIGH);
    }
    irrecv.resume(); // Receiving the next value

  }

}

What you could do is when a code is received, start a very short timed period and turn the motors on. Then, when the period elapses, turn the motors off. You'd only need a very short timed period. You'd need to determine a period that works well.
If you use millis()-based timing for the timed period, not 'delay()', you can still watch for and respond to further IR commands, so if the same code arrives again, you re-start the timed period.