Can't figure out why whis isnt working?

I’m making a project with a key on my door and some servos and stuff. The thing is that I can’t figure out the code…

I want the code to do this:
When I have the Potentiometer to the right(manual_key > 250) the door should just open
When it is to the left(opposite of right) it should wait until the door is closed (message = 0) and lock the door

Then you should just turn the pot right and left and that is what it keeps doing the unlock/lock sequence.

But it doesn’t unlock it when it is to the right if it isn’t at the beginning of the program.
But the left does work???

And the serial print CAN see it should be unlocked… it just isn’t.

The serial print says this: Message received: 0, and the POT is (numbers that declare its to the right), door state is then Unlocked

But that is where it isn’t unlocked :frowning:

I hope it is here to post… the first time. Can anyone help?

Btw version 1.8.12

//using servo timer 2 cause RH_ASK(Radio module library) is using timer 1 as the Normal servo

#include <ServoTimer2.h>
#include<RH_ASK.h>
#include<SPI.h>


ServoTimer2 key; // setting servo to be key
RH_ASK rf_driver;

String message; //Message from the open(1)/closed(0) door 
String door_state = "Unlocked";
int manual_key_pin = A0; //Potentiometer Pin
int manual_key; // potentiometer

//double knock stuff ignore havent configured it yet)
int Sensor = A1;
int clap = 0;
long detection_range_start = 0;
long detection_range = 0;
boolean status_lights = false;

void setup() 
{
  rf_driver.init();
  Serial.begin(9600);
  pinMode(2, OUTPUT); // setting up a led (does nothing at this point)
  pinMode(Sensor, INPUT); // double knock again
  pinMode(manual_key_pin, INPUT); // setting POT up
  key.attach(9); //Attaching servo
}

void loop() 
{
  //setting up message
  uint8_t buf[1];
  uint8_t buflen = sizeof(buf);

  manual_key = analogRead(manual_key_pin); //reading POT
  int status_sensor = digitalRead(Sensor);

    if (rf_driver.recv(buf, &buflen)) //When message received...
    {
      message = String((char*)buf);
/* ignore this
if (status_sensor == 0)
{
if (clap == 0)
{
detection_range_start = detection_range = millis();
clap++;
}
else if (clap > 0 && millis()-detection_range >= 50)
{
detection_range = millis();
clap++;
}
}
if (millis()-detection_range_start >= 400)
{
if (clap == 2)
{
if (!status_lights)
{
status_lights = true;
digitalWrite(13, HIGH);
}
else if (status_lights)
{
status_lights = false;
digitalWrite(13, LOW);
}
}
clap = 0;
}*/
      
      if (manual_key < "250 " && door_state == "Unlocked") //when the door is unlocked and the POT is to the left
      {
        if (message == "0") //When the door is closed (Receving from the radio module
        {
          delay(1000); // wait to be sure the door is closed (tried a for lopp didnt work)
          
          key.write(-90); //Locking the door (Turning the Servo)
          door_state = "Locked"; // Setting the door state to locked
        }
      }
      if (manual_key > "250" && door_state == "Locked") // if the pot is to the right then just open
      {
        door_state = "Unlocked"; // setting door state to unlocked
        key.write(90); // unlocking the door
      }
       
   }
   // printing some info
   Serial.print("Message Received: ");
   Serial.print(message);
   Serial.print(", and the POT is on: ");
   Serial.print(manual_key);
   Serial.print(", door_state is: ");
   Serial.println(door_state);
}
int manual_key; // potentiometer

manual_key is declared as an int, ie a number

    if (manual_key < "250 " && door_state == "Unlocked")

Here you are comparing it to a string

Does that seem right to you ?

Compare? The && isn't it just two parameters (and)? It is supposed to mean:

When the POT is to the right and when the door is unlocked

Rumrobot:
Compare? The && isn’t it just two parameters (and)? It is supposed to mean:

When the POT is to the right and when the door is unlocked

Yes, two parameters. But both the left and right parameters are comparisons.

The left one…

manual_key < "250 "

…is comparing an integer variable (manual_key) with a string constant (“250 “).

Try 250 without the quotes for an integer constant.

ok thx. i will try that, and come back later :wink:

Nope... nothing changed :frowning:

Rumrobot:
Nope... nothing changed :frowning:

Not even your code?

Post your code as it is now

//using servo timer 2 cause RH_ASK(Radio module library) is using timer 1 as the Normal servo

#include <ServoTimer2.h>
#include<RH_ASK.h>
#include<SPI.h>


ServoTimer2 key; // setting servo to be key
RH_ASK rf_driver;

String message; //Message from the open(1)/closed(0) door 
String door_state = "Unlocked";
int manual_key_pin = A0; //Potentiometer Pin
int manual_key; // potentiometer

//double knock stuff ignore havent configured it yet)
int Sensor = A1;
int clap = 0;
long detection_range_start = 0;
long detection_range = 0;
boolean status_lights = false;

void setup() 
{
  rf_driver.init();
  Serial.begin(9600);
  pinMode(2, OUTPUT); // setting up a led (does nothing at this point)
  pinMode(Sensor, INPUT); // double knock again
  pinMode(manual_key_pin, INPUT); // setting POT up
  key.attach(9); //Attaching servo
}

void loop() 
{
  //setting up message
  uint8_t buf[1];
  uint8_t buflen = sizeof(buf);

  manual_key = analogRead(manual_key_pin); //reading POT
  int status_sensor = digitalRead(Sensor);

  if (rf_driver.recv(buf, &buflen)) //When message received...
  {
    message = String((char*)buf);
    
/* ignore this

if (status_sensor == 0)
{
if (clap == 0)
{
detection_range_start = detection_range = millis();
clap++;
}
else if (clap > 0 && millis()-detection_range >= 50)
{
detection_range = millis();
clap++;
}
}
if (millis()-detection_range_start >= 400)
{
if (clap == 2)
{
if (!status_lights)
{
status_lights = true;
digitalWrite(13, HIGH);
}
else if (status_lights)
{
status_lights = false;
digitalWrite(13, LOW);
}
}
clap = 0;
}*/
     
    if (manual_key < 550 && door_state == "Unlocked") //when the door is unlocked and the POT is to the left
    {
      if (message == "0") //When the door is closed (Receving from the radio module
      {
        for (int i=0;i==1500;i++)
        {
          if (message != 0)
          {
            i = 1500;
          } else
          {
            delay(1); //wait 1,5 seconds if the door shouldnt close properly 
          }
        }
        key.write(-90); //Locking the door (Turning the Servo)
        door_state = "Locked"; // Setting the door state to locked
      }
    }
    if (manual_key > 550 && door_state == "Locked") // if the pot is to the right then just open
    {
      door_state = "Unlocked"; // setting door state to unlocked
      key.write(90); // unlocking the door
    }  
  }
  
  // printing some info
  Serial.print("Message Received: ");
  Serial.print(message);
  Serial.print(", and the POT is on: ");
  Serial.print(manual_key);
  Serial.print(", door_state is: ");
  Serial.println(door_state);
}

I changed the string thing, and i put the for loop back in instead of just a delay (doesnt work again (for loop(doesnt matter that much)))

  uint8_t buf[1];
  uint8_t buflen = sizeof(buf);

  if (rf_driver.recv(buf, &buflen)) //When message received...
  {
    message = String((char*)buf);

This is just a huge waste of resources. Just compare the character in buf[0] to ‘0’ or ‘1’.

What IS in buf[0]? Print it immediately after you read it, not later. Print the value of manual_key immediately after you read it, not later.

You make exactly the same mistake here:

if (message == "0") //When the door is closed (Receving from the radio module
      {
        for (int i=0;i==1500;i++)
        {
          if (message != 0)

First you compare message to a String, then you compare it to an integer. Is message a String or an integer? IT certainly can’t be both.

Regards,
Ray L.

The Buf[1] is for the radio receiver. It means that there is 1 character in the message. A part of RH_ASK.h

//using servo timer 2 cause RH_ASK(Radio module library) is using timer 1 as the Normal servo

#include <ServoTimer2.h>
#include<RH_ASK.h>
#include<SPI.h>


ServoTimer2 key; // setting servo to be key
RH_ASK rf_driver;

String message; //Message from the open(1)/closed(0) door 
String door_state = "Unlocked";
int manual_key_pin = A0; //Potentiometer Pin
int manual_key; // potentiometer

//double knock stuff ignore havent configured it yet)
int Sensor = A1;
int clap = 0;
long detection_range_start = 0;
long detection_range = 0;
boolean status_lights = false;

void setup() 
{
  rf_driver.init();
  Serial.begin(9600);
  pinMode(2, OUTPUT); // setting up a led (does nothing at this point)
  pinMode(Sensor, INPUT); // double knock again
  pinMode(manual_key_pin, INPUT); // setting POT up
  key.attach(9); //Attaching servo
}

void loop() 
{
  //setting up message
  uint8_t buf[1];
  uint8_t buflen = sizeof(buf);

  manual_key = analogRead(manual_key_pin); //reading POT
  int status_sensor = digitalRead(Sensor);

  if (rf_driver.recv(buf, &buflen)) //When message received...
  {
    message = String((char*)buf);
    
/* ignore this

if (status_sensor == 0)
{
if (clap == 0)
{
detection_range_start = detection_range = millis();
clap++;
}
else if (clap > 0 && millis()-detection_range >= 50)
{
detection_range = millis();
clap++;
}
}
if (millis()-detection_range_start >= 400)
{
if (clap == 2)
{
if (!status_lights)
{
status_lights = true;
digitalWrite(13, HIGH);
}
else if (status_lights)
{
status_lights = false;
digitalWrite(13, LOW);
}
}
clap = 0;
}*/
     
    if (manual_key < 550 && door_state == "Unlocked") //when the door is unlocked and the POT is to the left
    {
      if (message == 0) //When the door is closed (Receving from the radio module
      {
        for (int i=0;i==1500;i++)
        {
          if (message != 0)
          {
            i = 1500;
          } else
          {
            delay(1); //wait 1,5 seconds if the door shouldnt close properly 
          }
        }
        key.write(-90); //Locking the door (Turning the Servo)
        door_state = "Locked"; // Setting the door state to locked
      }
    }
    if (manual_key > 550 && door_state == "Locked") // if the pot is to the right then just open
    {
      door_state = "Unlocked"; // setting door state to unlocked
      key.write(90); // unlocking the door
    }  
  }
  
  // printing some info
  Serial.print("Message Received: ");
  Serial.print(message);
  Serial.print(", and the POT is on: ");
  Serial.print(manual_key);
  Serial.print(", door_state is: ");
  Serial.println(door_state);
}

It is a string, but I use it as an integer. It didnt work as a intenger but it did with the string.

Rumrobot:
It is a string, but I use it as an integer. It didnt work as a intenger but it did with the string.

You cannot use it as an integer if it is NOT an integer, and message is NOT an integer, it IS a String. Every variable always has a “type”, and you can only use it as that type unless you explicitly cast it, or convert it, to another type. You MUST be consistent, and use variable correctly, or the operations you perform on them will give absolutely results. Comparing a String to an integer is a nonsense comparison, and will give a nonsense result. The compiler will not magically fix your mistake for you, and give you the result you want. In this case, the comparison will ALWAYS return not-zero, regardless of what the String message actually contains. So, “if (message == 0)” will ALWAYS return false, and “if (message != 0)” will ALWAYS return true.

Programming is all about precision. You must define variables correctly, and you must use them correctly. It is neither horseshoes nor hand-grenades - “close” is exactly the same as “off by a mile”.

Regards,
Ray L.

But 1 and 0 is still a character, right?

Rumrobot:
But 1 and 0 is still a character, right?

No. 1 and 0 are ints. '1' and '0' are characters. That is a VERY important distinction.

oh yeah got that. Then my for loop should work then. thx 1/2 problems solved

The integers 0 and 1 have the numeric values 0 and 1. The characters ‘0’ and ‘1’ have the numeric values 48 and 49, since they are ASCII encoded. 0 is NEVER == ‘0’ and 1 is NEVER == ‘1’. These are truly apples and oranges comparisons.

Regards,
Ray L.

got something new in this. When I open the Serial port it unlocks when the pot is turned to the left, which it should do when that is true. The Serial is closed and then opens, door unlocks...

Hope this might find the problem :slight_smile:

Is the code in post #12 still what you are testing ?