int needToPrint = 0;
int count;
int in = 2;
int lastState = LOW;
int trueState = LOW;
long lastStateChangeTime = 0;
int cleared = 0;
// constants
int dialHasFinishedRotatingAfterMs = 100;
int debounceDelay = 10;
void setup()
{
Serial.begin(9600);
pinMode(in, INPUT);
}
void loop()
{
int reading = digitalRead(in);
if ((millis() - lastStateChangeTime) > dialHasFinishedRotatingAfterMs) {
// the dial isn't being dialed, or has just finished being dialed.
if (needToPrint) {
// if it's only just finished being dialed, we need to send the number down the serial
// line and reset the count. We mod the count by 10 because '0' will send 10 pulses.
Serial.print(count % 10, DEC);
needToPrint = 0;
count = 0;
cleared = 0;
}
}
if (reading != lastState) {
lastStateChangeTime = millis();
}
if ((millis() - lastStateChangeTime) > debounceDelay) {
// debounce - this happens once it's stablized
if (reading != trueState) {
// this means that the switch has either just gone from closed->open or vice versa.
trueState = reading;
if (trueState == HIGH) {
// increment the count of pulses if it's gone high.
count++;
needToPrint = 1; // we'll need to print this number (once the dial has finished rotating)
}
}
}
lastState = reading;
}
How to store data from software serial? I need 9 didigts of data to compare it with specific digits for example 132812832 and if false clear them
Jabbadabbadahaht:
How to store data from software serial? I need 9 didigts of data to compare it with specific digits for example 132812832 and if false clear them
int needToPrint = 0;
int count;
int in = 2;
int led1 = 3;
int lastState = LOW;
int trueState = LOW;
long lastStateChangeTime = 0;
int cleared = 0;
/////////////////
int password; //
int currentLength = 0; //defines which number we are currently writing
int i = 0;
char entered[4];
int tpw = 1234;
////////////////
// constants
int dialHasFinishedRotatingAfterMs = 100;
int debounceDelay = 20;
void setup()
{
Serial.begin(9600);
pinMode(in, INPUT);
pinMode(led1, OUTPUT);
}
void loop()
{
int reading = digitalRead(in);
if ((millis() - lastStateChangeTime) > dialHasFinishedRotatingAfterMs) {
// the dial isn't being dialed, or has just finished being dialed.
if (needToPrint) {
// if it's only just finished being dialed, we need to send the number down the serial
// line and reset the count. We mod the count by 10 because '0' will send 10 pulses.
Serial.print(count % 10, DEC);
//////////////////
count = Serial.read();
count == password;
//password = needToPrint;
password++;
//////////////////
needToPrint = 0;
count = 0;
cleared = 0;
}
}
if (reading != lastState) {
lastStateChangeTime = millis();
}
if ((millis() - lastStateChangeTime) > debounceDelay) {
// debounce - this happens once it's stablized
if (reading != trueState) {
// this means that the switch has either just gone from closed->open or vice versa.
trueState = reading;
if (trueState == HIGH) {
// increment the count of pulses if it's gone high.
count++;
needToPrint = 1; // we'll need to print this number (once the dial has finished rotating)
}
}
}
lastState = reading;
//if (currentLength == 4) {
if (1111 == password){
digitalWrite(3, HIGH);
//}
// else{
// password == "";
// }
}
}
i think i tried and failed.
EDIT:
Old rotary board and this piece of code reads pulses. Sorry i'm babystepping in arduino and my english isn't great.
About software serial: i misunderstood concept of softwareserial and thought it works like one. I used this instrucrable: Link and
I want to make it work like codelock with servo. You just dial specific nuber and it allow servo to move else it clears int with saved code
Jabbadabbadahaht:
I want to make it work like codelock with servo. You just dial specific nuber and it allow servo to move else it clears int with saved code
I would like to try to help you. But first you need to respond to my request for info about the rotary board. At the moment you have all the info and I have none.
The topic said that because i thought my rotary dialer comunicate with arduino by software serial cose of "Serial.print(count % 10, DEC);" but now i know that command communicates with 'pc' to show output.
The photos:
I attached scheme and photo of rotary dialer.
How it work? It just count ticks that rotary dialer makes (eg choosing 9 gives us 9 impulses that arduino reads as 9 and show it by software serial). How i want it to work? I want to dial number and store it somehow, then compare it with hardcoded code eg 153321226 if statment is true then it returns with something ill code (propably led + servo), else it wipes data and start over.
I don't know how to describe it better - ask if you need/.
The code in Reply #3 (assuming that is what you are using) does not use SoftwareSerial at all. Why do you keep referring to it as in "show it by software serial)"
Are you able to get the Arduino to count the pulses correctly and display the value on the Serial Monitor?
If so, post the program that does that.
int needToPrint = 0;
int count;
int in = 2;
int lastState = LOW;
int trueState = LOW;
long lastStateChangeTime = 0;
int cleared = 0;
// constants
int dialHasFinishedRotatingAfterMs = 100;
int debounceDelay = 10;
void setup()
{
Serial.begin(9600);
pinMode(in, INPUT);
}
void loop()
{
int reading = digitalRead(in);
if ((millis() - lastStateChangeTime) > dialHasFinishedRotatingAfterMs) {
// the dial isn't being dialed, or has just finished being dialed.
if (needToPrint) {
// if it's only just finished being dialed, we need to send the number down the serial
// line and reset the count. We mod the count by 10 because '0' will send 10 pulses.
Serial.print(count % 10, DEC);
needToPrint = 0;
count = 0;
cleared = 0;
}
}
if (reading != lastState) {
lastStateChangeTime = millis();
}
if ((millis() - lastStateChangeTime) > debounceDelay) {
// debounce - this happens once it's stablized
if (reading != trueState) {
// this means that the switch has either just gone from closed->open or vice versa.
trueState = reading;
if (trueState == HIGH) {
// increment the count of pulses if it's gone high.
count++;
needToPrint = 1; // we'll need to print this number (once the dial has finished rotating)
}
}
}
lastState = reading;
}
int 'count' stores numbr of ticks and send it by software serial
I think that you may be better of with building your password from the individual counts and keeping it as a number.
password = password*10 + count % 10;
There is an issue of start and stop, but you can handle it with a password of known length like the 4 digits you were using in the code you posted.
For clarity, change "needToPrint" flag to "addDigit".
This is untested, but see if it gives you ideas
//int needToPrint = 0;
byte addDigit = 0;
int count;
int in = 2;
int led1 = 3;
int lastState = LOW;
int trueState = LOW;
long lastStateChangeTime = 0;
int cleared = 0;
/////////////////
int password; //
int currentLength = 0; //defines which number we are currently writing
int i = 0;
//char entered[4]; leave password as number
int tpw = 1234;
////////////////
// constants
int dialHasFinishedRotatingAfterMs = 100;
int debounceDelay = 20;
void setup()
{
Serial.begin(9600);
pinMode(in, INPUT);
pinMode(led1, OUTPUT);
}
void loop()
{
int reading = digitalRead(in);
if ((millis() - lastStateChangeTime) > dialHasFinishedRotatingAfterMs) {
// the dial isn't being dialed, or has just finished being dialed.
if (addDigit) {
// if it's only just finished being dialed, we need to send the number down the serial
// line and reset the count. We mod the count by 10 because '0' will send 10 pulses.
//Serial.print(count % 10, DEC);
password = password * 10 + count % 10;
//////////////////
//count = Serial.read();
//count == password;
//password = needToPrint;
//password++;
currentLength++ ;
//////////////////
//needToPrint = 0;
addDigit = 0;
count = 0;
//cleared = 0;
}
}
if (reading != lastState) {
lastStateChangeTime = millis();
}
if ((millis() - lastStateChangeTime) > debounceDelay) {
// debounce - this happens once it's stablized
if (reading != trueState) {
// this means that the switch has either just gone from closed->open or vice versa.
trueState = reading;
if (trueState == HIGH) {
// increment the count of pulses if it's gone high.
count++;
addDigit = 1; // we'll need to print this number (once the dial has finished rotating)
}
}
}
lastState = reading;
if (currentLength == 4) {
if (1111 == password) {
digitalWrite(3, HIGH);
}
else { //reset on Failure
password = 0;
currentLength = 0;
//Blink led or serial print to indicate failure
}
}
}
//int needToPrint = 0;
byte addDigit = 0;
int count;
int in = 2;
int led1 = 3;
int lastState = LOW;
int trueState = LOW;
long lastStateChangeTime = 0;
int cleared = 0;
/////////////////
int password; //
int currentLength = 0; //defines which number we are currently writing
int i = 0;
//char entered[4]; leave password as number
int tpw = 1234;
////////////////
// constants
int dialHasFinishedRotatingAfterMs = 100;
int debounceDelay = 20;
void setup()
{
Serial.begin(9600);
pinMode(in, INPUT);
pinMode(led1, OUTPUT);
}
void loop()
{
int reading = digitalRead(in);
if ((millis() - lastStateChangeTime) > dialHasFinishedRotatingAfterMs) {
// the dial isn't being dialed, or has just finished being dialed.
if (addDigit) {
// if it's only just finished being dialed, we need to send the number down the serial
// line and reset the count. We mod the count by 10 because '0' will send 10 pulses.
Serial.print(count % 10, DEC);
password = password * 10 + count % 10;
//////////////////
//count = Serial.read();
//count == password;
//password = needToPrint;
//password++;
currentLength++ ;
//////////////////
//needToPrint = 0;
addDigit = 0;
count = 0;
//cleared = 0;
}
}
if (reading != lastState) {
lastStateChangeTime = millis();
}
if ((millis() - lastStateChangeTime) > debounceDelay) {
// debounce - this happens once it's stablized
if (reading != trueState) {
// this means that the switch has either just gone from closed->open or vice versa.
trueState = reading;
if (trueState == HIGH) {
// increment the count of pulses if it's gone high.
count++;
addDigit = 1; // we'll need to print this number (once the dial has finished rotating)
}
}
}
lastState = reading;
if (currentLength == 9) {
if (513636814 == password) {
digitalWrite(3, HIGH);
}
else { //reset on Failure
password = 0;
currentLength = 0;
digitalWrite(3, HIGH);
delay(200);
digitalWrite(3, LOW);
//Blink led or serial print to indicate failure
}
}
}
it seems to work when i type 1111 password but when i changed it to 9 digit code "519636814" (no zeros) and im dialing wrong code first then good one it doesnt work and sends me blinking led singnal (for wrong), for good pw i have static led light.
Jabbadabbadahaht:
and im dialing wrong code first then good one it doesnt work
How do you define where a password sequence starts. Perhaps it is confusing part of the wrong password and part of the new one.
Maybe you could be required to dial 2 zeroes to start the sequence but zeroes would never be part of a password - which would avoid the problems others have mentioned.
Jabbadabbadahaht:
it seems to work when i type 1111 password but when i changed it to 9 digit code "519636814" (no zeros) and im dialing wrong code first then good one it doesnt work and sends me blinking led singnal (for wrong), for good pw i have static led light.
Because, 519636814 won't fit in the int data type. You need to make "password" a long, and compare with 519636814L.
The largest password you can currently use is 32767.