ok so i have various states in order to accomplish my goal.
the code spends most of its time scanning for a rfid signal, when its detected it twitches the servos,
in a random period of time it will randomly select one of two other states, each of these have a secondry state that follows. these pairs operate the same servos as the rfid.
each "pair" of these states once complete return back to scanning for rfid.
reason for this is because i dont want the servos being activated from the rfid in the middle of the other states as it will cause damage to what the servos are being installed in.
anyway, im having difficulty making it choose one of the two "master" states randomly. (i.e choosing which one randomly)
heres the code i have done so far
#include <Wire.h>
#include <SPI.h>
#include <Servo.h>
#include <MFRC522.h>
MFRC522 mfrc522(10, 9); // MFRC522 mfrc522(SS_PIN, RST_PIN)
Servo fingers;
Servo thumb;
Servo wrist;
int angle=0;
String tagUID = "F5 7E C9 EF"; // String to store UID of tag. Change it with your tag's UID
typedef enum
{
initialState,
wantCheckrfid,
want1, //Fistclench,
wantFistrelax,
want2, //Thumbsupclench,
wantThumbsuprelax,
} states;
// state machine variables
states state = initialState;
unsigned long lastStateChange = 0;
unsigned long timeInThisState = 1000;
void setup() {
fingers.attach(6);
thumb.attach(7);
wrist.attach(8);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
}
void doStateChange ()
{
lastStateChange = millis ();
timeInThisState = 1000;
switch (state)
{
case initialState:
state = wantCheckrfid;
break;
case wantCheckrfid:
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
//Reading from the card
String tag = "";
for (byte i = 0; i < mfrc522.uid.size; i++)
{
tag.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
tag.concat(String(mfrc522.uid.uidByte[i], HEX));
}
tag.toUpperCase();
//Checking the card
if (tag.substring(1) == tagUID) //change here the UID of the card/cards that you want to give access
{
// If UID of tag is matched.
fingers.write(20);
thumb.write(30);
delay (120);
fingers.write(0);
thumb.write(0);
}
state = random(want1)+want2);
timeInThisState = random 10000(50000);
break;
case want1:
fingers.write(120);
thumb.write(80);
state = wantFistrelax;
timeInThisState = 20000;
break;
case wantFistrelax:
fingers.write(0);
thumb.write(0);
state = wantCheckrfid;
timeInThisState = 3000;
break;
case want2:
fingers.write(120);
wrist.write(90);
state = wantThumbsuprelax;
timeInThisState = 20000;
break;
case wantThumbsuprelax:
fingers.write(0);
wrist.write(0);
state = wantCheckrfid;
timeInThisState = 3000;
break;
}
}
void loop() {
if (millis () - lastStateChange >= timeInThisState)
doStateChange ();
}
any help would be appreciated.
i know "random" only returns numbers, so i tried renaming the states to 1 and 2 without "want" but then it complained about needing an identifier.
on a side note, yes i know theres a cheeky "delay" in the rfid code, it shouldnt pose an issue as i couldnt work out how to add the delay using millis lol