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
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);
}
//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)))
//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”.
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.
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...