I'm trying to keep this as simple as possible but if there is no way it's fine. I'm currently working on a rfid unlocked door. So basically I have the rfid-RC522 scanner and a stepper motor. I'm trying to get it programmed right so that when I scan my Rfid card with the UID of F1 94 8D 3D I want it to tell the motor to move but if the uid is wrong then I don't want it to do anything. I have already got the motor to work by its self I have it setup so when you enter 1 in the Serial Monitor it moves the motor 512 steps waits 10000 milliseconds then moves -512 steps. What i'm asking is if you can help me get it so that it all works together, right now the rfid scanner scans the card and gives me the uid in the serial monitor but then it keeps looping and I've tried to use a while loop then break it but unless I did it wrong it didn't work. It gives me F1 09 49 48 D94 while the real uid is F1 94 8D 3D. I have no errors but need a lot of help.
To simplify all that I need help with is combining my code for the rfid scanner and my motor, and the code for my rfid scanner to not spam the Monitor with the Uid. This code will not be ran with a computer/laptop present once the programming is done. I'm sorry if i'm asking to much.
Here is the Stepper Motor code
#include <Stepper.h>
#define STEPS 64
Stepper stepper(STEPS, 8, 10, 9, 11);
int val = 0;
void setup() {
Serial.begin(9600);
stepper.setSpeed(200);
}
void loop() {
if (Serial.available() == 1)
{
stepper.step(512);
delay(10000);
stepper.step(-512);
void flipflop();
{
Serial.print("Stop");//Here so the flipflop runs
}
}
}
Here is the Rfid scanner code
#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>
#include <SPI.h>
#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
unsigned long UID[3];
unsigned long UID1;
void setup() {
Serial.begin(9600);
//while(!Serial);
SPI.begin();
}
void loop() {
if (!mfrc522.PICC_IsNewCardPresent())
{
return;
}
if (!mfrc522.PICC_ReadCardSerial())
{
return ;
}
for (int i = 0; i < 3; i++) {
UID[i] = mfrc522.uid.uidByte[i];
Serial.print(UID[i], HEX);
Serial.print(UID[1], HEX);
}
}
#define SS_PIN 53 // Configurable, see typical pin layout above
Are you using an Arduino MEGA?
You say you expect "F1 94 8D 3D" and you say you get "F1 09 49 48 D94" but your sketch displays six 1-digit or 2-digit hex numbers with no spaces. You set UID[0] and then print UID[0] and UID[1]. Then you set UID[1] and print that twice. Then you set UID[2] and print that and UID[1]. The first time I would expect the output to be "F10998D9" (representing "F1 0 9 9 D8 9") and in subsequent reads, "F19998D9" (representing "F1 9 9 9 D8 9").
If you want to see the actual value:
byte UID[4]; // Was 'unsigned long UID[3];' for some reason
for (int i = 0; i < 4; i++) {
UID[i] = mfrc522.uid.uidByte[i];
if (UID[i] < 0x10)
Serial.print('0'); // Add a leading 0 for single-digit HEX numbers
Serial.print(UID[i], HEX);
Serial.print(' ');
}
Serial.println();
you asking for help which is totally OK. As long as you demonstrate some own effort of walking up the learning-curve about programming - you can ask as much as you like. What is very good is that you have given a verbal description of what should happen. Please post information about your knowledge-level in programmingf and post a first own attempt how you think the two sketches can be combined. This demonstrates your knowledge-level which is very important to adapt the answers to your knowledge-level or giving recommendations how you can improve your knowledge-level.
From what you have posted so far some users could misundersand you are a Copy&Paster-Guy who now wants others to finish his project. So please write another post with content as described above.
void flipflop();
{
Serial.print("Stop");//Here so the flipflop runs
}
Sorry, what?
Looks like you were trying to define a function in the middle of another function (which is not legal)?
But then added the ; at the end (presumably because you got an error) and just left the { } code block behind.
you asking for help which is totally OK. As long as you demonstrate some own effort of walking up the learning-curve about programming - you can ask as much as you like. What is very good is that you have given a verbal description of what should happen. Please post information about your knowledge-level in programming and post a first own attempt how you think the two sketches can be combined. This demonstrates your knowledge-level which is very important to adapt the answers to your knowledge-level or giving recommendations how you can improve your knowledge-level.
From what you have posted so far some users could misundersand you are a Copy&Paster-Guy who now wants others to finish his project. So please write another post with content as described above.
best regards Stefan
I am quite experienced in coding I have about 3 years in a variety of different languages but i dont quite understand this because its similar to c++ and i'm quite new to c++ I have very little experience in it. I just had a few issues I dont need anyone to finish my project because this is not all im going to do with it and I didn't copy and paste anything its an original code this is why im having issues with it. I have tried to combine the codes with a void flip flop on the void loop but I had more errors with it.
{
Serial.print("Stop");//Here so the flipflop runs
}
Sorry, what?
Looks like you were trying to define a function in the middle of another function (which is not legal)?
But then added the ; at the end (presumably because you got an error) and just left the { } code block behind.
I did this so that when the motor runs clockwise 90 degrees then waits 10 seconds and turns back it will stop the loop because this is the only way I could figure out how to stop the loop without a void break but it has to have a function so I made it print stop because it wont mess with anything else.
So If your asking why i didn't do something like this its because then it will just run that flip flop over and over so the monitor will be spammed with stopstopstop....etc
#include <Stepper.h>
#define STEPS 64
Stepper stepper(STEPS, 8, 10, 9, 11);
int val = 0;
void setup() {
Serial.begin(9600);
stepper.setSpeed(200);
}
void loop() {
if (Serial.available() == 1)
{
stepper.step(512);
delay(10000);
stepper.step(-512);
[b]
}
void flipflop();
{
Serial.print("Stop");//Here so the flipflop runs
}
}
[/b]
{
Serial.print("Stop");//Here so the flipflop runs
}
Sorry, what?
Looks like you were trying to define a function in the middle of another function (which is not legal)?
But then added the ; at the end (presumably because you got an error) and just left the { } code block behind.
I did this so that when the motor runs clockwise 90 degrees then waits 10 seconds and turns back it will stop the loop because this is the only way I could figure out how to stop the loop without a void break but it has to have a function so I made it print stop because it wont mess with anything else.
So If your asking why i didn't do something like this its because then it will just run that flip flop over and over so the monitor will be spammed with stopstopstop....etc
}
void flipflop();
{
Serial.print("Stop");If i have it outside of the other loop function it will not work the way I want it to
}
}
elf100:
From what I understand it's just used to break a loop in certain scenarios but in mine I could not find a way to use it without getting an error.
loop() is a function, not a "void". In several years of Arduino programming, I have not had to use 'break' even once, except in switch statements.
This is the updated code I have so far
Ok so I have hopefully this last error to debug and I need y'all help so I need to take part of my code out of my main because you can not have a function-definition inside of the main i think i said that right but I dont know how to do that and leave it in the else statement I think its the void loop i need to move im not %100 sure.
here is the code
#include <deprecated.h>
#include <MFRC522.h>
#include <Stepper.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>
#include <SPI.h>
#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above
#define STEPS 64
Stepper stepper(STEPS, 8, 10, 9, 11);
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
unsigned long UID[3];
unsigned long UID1;
int main()
{
int val = 0;
if (Serial.available() == 1)
{
stepper.step(512);
delay(10000);
stepper.step(-512);
delay(20000);
}
else;
{
void setup() {
Serial.begin(9600);
//while(!Serial);
SPI.begin();
Serial.begin(9600);
stepper.setSpeed(200);
}
void loop() {
if (!mfrc522.PICC_IsNewCardPresent())
{
return;
}
if (!mfrc522.PICC_ReadCardSerial())
{
return ;
}
{
byte UID[4];
for (int i = 0; i < 4; i++) {
UID[i] = mfrc522.uid.uidByte[i];
if (UID[i] < 0x10)
Serial.print('0'); // Add a leading 0 for single-digit HEX numbers
Serial.print(UID[i], HEX);
Serial.print(' ');
}
Serial.println();
}
if (Serial.available() == 19, 97, 95, 99)
{
Serial.print("1");
}
}
}
}
Here is your sketch with some changes to allow it to compile without errors and warnings:
// #include <deprecated.h> // Not used
#include <MFRC522.h>
#include <Stepper.h>
// #include <MFRC522Extended.h> // Not used
// #include <require_cpp11.h> // Not used
// #include <SPI.h> // Not used
#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above
#define STEPS 64
Stepper stepper(STEPS, 8, 10, 9, 11);
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
byte UID[4]; // Was: unsigned long UID[3]; ?!?!?
unsigned long UID1;
// WARNING: Defining your own 'main()' will keep the built-in
// 'main()' from being used so setup() and loop() will not be called.
// DO NOT create a function called 'main()' if you want to
// do 'Arduino' programming.
int main()
{
// int val = 0; //Not used
// WARNING: There is no call to init() initVariant() and no
// call to Serial.begin() so don't expect Serial to work.
// If the number of received characters is exactly 1
if (Serial.available() == 1)
{
stepper.step(512);
delay(10000);
stepper.step(-512);
delay(20000);
}
}
void setup()
{
Serial.begin(9600);
//while(!Serial);
SPI.begin();
Serial.begin(9600);
stepper.setSpeed(200);
}
void loop()
{
if (!mfrc522.PICC_IsNewCardPresent())
{
return;
}
if (!mfrc522.PICC_ReadCardSerial())
{
return;
}
byte UID[4];
for (int i = 0; i < 4; i++)
{
UID[i] = mfrc522.uid.uidByte[i];
if (UID[i] < 0x10)
Serial.print('0'); // Add a leading 0 for single-digit HEX numbers
Serial.print(UID[i], HEX);
Serial.print(' ');
}
Serial.println();
if (Serial.available() == 19 || Serial.available() == 97 || Serial.available() == 95 || Serial.available() == 99)
{
Serial.print("1");
}
}
to be outside of the main() {
}
braces - so the functions are not defined with a function.
I know That Ihad some stuff that i need to fix I am still new to c++ but i need the void loop to be ran under the else in my if else statemant so im lost on how to take my loop and loop setup outside of my main and it still be ran under else.
Everything can be coded in mutliple ways. And there is really no need to call loop() under an if-condition.
If you prefer to code with the very classical NON-Arduino-Toolchain you can do so. But this means a lot of additional things to learn. I guess this is something you just won't do.
By your comment to insisting on using loop() under an if-condition I can see that you are a real newbee who has some fundamental misunderstandings about how coding with the Arduino-IDE works.
Now there are two ways to finish your project:
way 1: not learning the fundamental basics of programming coming back to the forum to ask for the next detail or the next changes inside your code. Over time this will lead into unwillingness to answer your questions and more and more users will refuse to answer. Answering here is the totally free choice of every user.
way2: start learning the fundamentals about programming and asking concrete questions that show that you put some own effort in improving your programming-skills. This will rise the willingness to answer your questions.
My recommendation to start is this tutorial:
Arduino Programming Course
Arduino Programming Course
It is easy to understand and a good mixture between writing about important concepts
and get you going.
StefanL38:
programming with the Arduino-IDE needs no main()
loop() IS the new main()
Everything can be coded in mutliple ways. And there is really no need to call loop() under an if-condition.
If you prefer to code with the very classical NON-Arduino-Toolchain you can do so. But this means a lot of additional things to learn. I guess this is something you just won't do.
By your comment to insisting on using loop() under an if-condition I can see that you are a real newbee who has some fundamental misunderstandings about how coding with the Arduino-IDE works.
Now there are two ways to finish your project:
way 1: not learning the fundamental basics of programming coming back to the forum to ask for the next detail or the next changes inside your code. Over time this will lead into unwillingness to answer your questions and more and more users will refuse to answer. Answering here is the totally free choice of every user.
way2: start learning the fundamentals about programming and asking concrete questions that show that you put some own effort in improving your programming-skills. This will rise the willingness to answer your questions.
Arduino Programming Course
It is easy to understand and a good mixture between writing about important concepts
and get you going.
Best regards Stefan
I am trying my best to learn as much as I can in my free time but i don't have much time to learn this language that's why i'm asking so many questions and I know that they may be dumb but I don't know much about it.
I will watch that tutorial thank you for that but i'm putting forth a lot of effort to make time to even work on this project I just need a lot of help even with the simple stuff because i don't have much time to do tutorials and stuff like them. I will try to work on this without the loop under the if statement and i have been trying to get this to work for a while now but because i don't quite understand this programming language its hard. What i'm trying to say is I would love to have the time to sit here all day and learn more about Arduino-IDE programming but I just don't have much time to do that. If you have any suggestions on how to improve my code i'm open to try anything.
you still seem not to understand what the point is:
If you stay on that low level of understanding. You will have to beg for every single line of code to be pre-written for you so you can copy and paste it.
If this is your very first project it is three levels too difficult.
Imho there is no short-path to a solution. Except you find a code that does fit 101% to your needs.
The best way would be to work through a tutorial with basic examples.
fifth best way to understand what the code does is adding serial debug-output to a code to see what the code is doing.
This will give you hints for asking concrete questions.
So here is the RFID-demo-code with added debug-output.
I have changed baudrate to 115200 to make the serial output faster.
You have to change the baudrate in the serial monitor to this value.
Load this code into your board open the serial monitor and watch what is running through the serial monitor.
I insist on that you have to ask a questions about everything that is unclear to you.
Because this means I would have work for weeks. Whatever questions come up ask them.
You still seem to hope that someone will write down all you need to get it working.
If this happends big congratulations.
With the attitude you have shown so far I refuse to give an all inclusive tutorial about everything. And I will not develop the code for you.
So as soon as you start asking concrete questions about whatever I will answer them. That's my way of giving support in this forum.
here is the code with the serial output
#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>
#include <SPI.h>
#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
unsigned long UID[3];
unsigned long UID1;
void setup() {
Serial.begin(115200);
SPI.begin();
}
void loop() {
if (!mfrc522.PICC_IsNewCardPresent()) {
Serial.println("if (!mfrc522.PICC_IsNewCardPresent()) return will be executed");
return;
}
if (!mfrc522.PICC_ReadCardSerial()) {
Serial.println("if (!mfrc522.PICC_ReadCardSerial()) return will be executed");
return ;
}
Serial.println("right before for (int i = 0; i < 3; i++) {");
for (int i = 0; i < 3; i++) {
UID[i] = mfrc522.uid.uidByte[i];
Serial.print(UID[i], HEX);
Serial.print(UID[1], HEX);
}
Serial.println("right behind for (int i = 0; i < 3; i++) {");
}