ID Locking System Not functioning/Char Array Converting
hey everyone, unable convert receivedchars output (which id scanned off card) inputid further use. have tried every single thing under sun, copying arrays, strings, separate arrays, @ point give up. can please me inputid data format storing same characters receivedchars? have tested , else works except 1 little thing. thank in advance.
code: [select]
#include <servo.h>
servo servo1;
servo servo2;
const byte numchars = 32;
char receivedchars[numchars];
boolean newdata = false;
boolean slot1taken = false;
boolean slot2taken = false;
boolean slot3taken = false;
boolean unlocked = false;
char inputid = '0';
char slot1id = '0';
char slot2id = '0';
char slot3id = '0';
//open
int o = 90;
//closed
int c = 0;
void setup() {
serial.begin(115200);
inputid = '0';
servo1.attach(2);
servo1.write(90);
servo2.attach(3);
servo2.write(90);
serial.println("the device ready use.");
}
void loop() {
recievewithstartendmarkers();
shownewdata();
if (inputid != '0'){
unlock();
if (unlocked == false){
lock();
}
unlocked = false;
debug();
}
}
void recievewithstartendmarkers() {
static boolean recieveinprogress = false;
static byte ndx = 0;
char startmarker = '%';
char endmarker = '?';
char received;
if (serial.available() > 0) {
received = serial.read();
if (recieveinprogress == true) {
if (received != endmarker) {
receivedchars[ndx] = received;
ndx++;
if (ndx >= numchars) {
ndx = numchars - 1;
}
}
else {
receivedchars[ndx] = '\0'; // terminate string
recieveinprogress = false;
ndx = 0;
newdata = true;
}
}
else if (received == startmarker) {
recieveinprogress = true;
}
}
}
void shownewdata() {
if (newdata == true) {
serial.print("input student id: ");
serial.println(receivedchars);
inputid = receivedchars;
serial.println(inputid);
newdata = false;
}
}
void unlock () {
if(slot1taken == true && inputid == slot1id && inputid != '0')
{
//unlock slot1
servo1.write(o);
slot1taken = false;
slot1id = '0';
unlocked = true;
inputid = '0';
}
if(slot2taken == true && inputid == slot2id && inputid != '0')
{
//unlock slot2
servo2.write(o);
slot2taken = false;
slot2id = '0';
unlocked = true;
inputid = '0';
}
if(slot3taken == true && inputid == slot3id)
{
//unlock slot3
slot3taken = false;
slot3id = '0';
unlocked = true;
inputid = '0';
}
}
void lock(){
if(slot1taken == false && inputid != '0')
{
//lock slot1
servo1.write(c);
slot1taken = true;
slot1id = inputid;
inputid = '0';
}
else if(slot2taken == false && inputid != '0')
{
//lock slot2
servo2.write(c);
slot2taken = true;
slot2id = inputid;
inputid = '0';
}
else if(slot3taken == false)
{
//lock slot3
slot3taken = true;
slot3id = inputid;
inputid = '0';
}
}
void debug(){
if(inputid == "1234"){
serial.print("slot 1 id:");
serial.println(slot1id);
serial.print("slot 2 id:");
serial.println(slot2id);
serial.print("slot 3 id:");
serial.println(slot3id);
inputid = '0';
}
}
Arduino Forum > Using Arduino > Programming Questions > ID Locking System Not functioning/Char Array Converting
arduino
Comments
Post a Comment