hi all
im trying really hard but this is driving me crazy. Im a design tech teacher who is trying to learn arduino so i can teach my students. i have a girl who is making an opening garage door model.
RFID starts Geared motor, opens door, trips Limit switch, stops motor; RFID strats motor in reverse, door closes, trips second limit switch, motor stops; whole thing loops.
I think my issue is tied up in a loop somewhere but i cannot for the life of me get the circuit to stop the motor when the opening switch is sensed. Once i sort this i can work on the closing side of things. I post the code underneath (most of it is from a really great project book i bought).
i have made as many notes on the code as i can (mostly to remind me what is going on.
i am using a nano and the two switch pins are 18 & 19. a lot of stuff is commented out as i have been trying diff things.
pin 17 supplies 5v to one side of both switches. the other side of each switch is connected to ground via 1.2k resistor and also connected to pins 18 & 19 (two limit switches.) i dont quite get the whole pulldown/up thing. at the start of the void loop code i turn pin 17 HIGH to supply switches with 5v.
Thanks heaps people!
#include <SPI.h>
#include <RFID.h>
//#include <Servo.h>
#include "pitches.h"
#include <Wire.h>
// wired connections
#define IN1 5 // D5 --> Motor B Input A --> MOTOR B +
#define IN2 6 // D6 --> Motor B Input B --> MOTOR B -
// functional connections
#define MOTOR_B_PWM IN1 // Motor B PWM Speed
#define MOTOR_B_DIR IN2 // Motor B Direction
// the actual values for "fast" and "slow" depend on the motor
#define PWM_SLOW 125 // arbitrary slow speed PWM duty cycle
#define PWM_MEDIUM 190 // arbitrary fast speed PWM duty cycle
#define PWM_FAST 250 // arbitrary fast speed PWM duty cycle
#define DIR_DELAY 1000 // brief delay for abrupt motor changes
RFID rfid(10, 5); // Define the RFID
// Replace this with the code from your card in hex form
byte card[5] = {0xE1,0xB3,0xE0,0xD5,0x67}; //E1, B3, E0, D5, 67
// List any other codes for cards with access here
byte serNum[5];
byte data[5];
// Define the melodies for successful access and denied access
int access_melody[] = {NOTE_G4, 0, NOTE_AS4, 0, NOTE_C4, 0, NOTE_G4,
0, NOTE_AS4, 0, NOTE_C4, 0};
const int limitUp = 18;
const int limitDown = 19;
const int switchVolt = 17;
int access_noteDurations[] = {8, 8, 8, 8, 8, 4, 8, 8, 8, 8, 8, 4};
int fail_melody[] = {NOTE_G2, 0, NOTE_F2, 0, NOTE_D2, 0};
int fail_noteDurations[] = {8, 8, 8, 8, 8, 4};
int LED_access = 2; // Pin connected to green LED
int LED_intruder = 3; // Pin connected to red LED
int speaker_pin = 8; // Pin connected to piezo buzzer
int buttonState = 2; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600); // Start serial communication
SPI.begin(); // Start serial communication between the RFID and PC
rfid.init(); // Initialize the RFID
Serial.println("Arduino card reader");
Serial.println("System Ready");
delay(1000);
pinMode(LED_access, OUTPUT);
pinMode(LED_intruder, OUTPUT);
pinMode(speaker_pin, OUTPUT);
pinMode(switchVolt, OUTPUT); //5V for Switches
pinMode(limitUp, INPUT); //limit switch door up
pinMode(limitDown, INPUT); //limit switch door down
pinMode(MOTOR_B_PWM, OUTPUT); // set all the motor control pins to outputs
pinMode(MOTOR_B_DIR, OUTPUT);
digitalWrite(MOTOR_B_PWM, LOW);
digitalWrite(MOTOR_B_DIR, LOW);
}
void loop()
{
digitalWrite(switchVolt, HIGH); //turn 5V supply for switches on
//analogWrite(MOTOR_B_PWM, 250-PWM_MEDIUM);
//(MOTOR_B_DIR, HIGH);
boolean card_card = true; // Define your card
if (rfid.isCard()) {
if (rfid.readCardSerial()) {
delay(1000);
data[0] = rfid.serNum[0];
data[1] = rfid.serNum[1];
data[2] = rfid.serNum[2];
data[3] = rfid.serNum[3];
data[4] = rfid.serNum[4];
}
Serial.print("Card found - code:");
for (int i = 0; i < 5; i++) { // If it is not your card, the card is considered false
if (data[i] != card[i]) card_card = false;
}
Serial.println();
if (card_card) { // A card with access permission is found
Serial.println("Hello!");
{ // Print to Serial Monitor
for (int i = 0; i < 12; i++) { // Play welcome music
int access_noteDuration = 1000 / access_noteDurations[i];
tone(speaker_pin, access_melody[i], access_noteDuration);
int access_pauseBetweenNotes = access_noteDuration * 1.30;
delay(access_pauseBetweenNotes);
noTone(speaker_pin);
}
//----------------------------------------
Serial.println("Access granted.......Welcome!");
digitalWrite(LED_access, HIGH); // Turn on green LED
digitalWrite(MOTOR_B_DIR, LOW); //forward
analogWrite(MOTOR_B_PWM, PWM_MEDIUM); //medium speed, PWM_MEDIUM
//if(digitalRead(limitUp) == HIGH) // read the state of the pushbutton value:
buttonState = digitalRead(limitUp);
if (buttonState == HIGH){ // check if the pushbutton is pressed.
stop();
//digitalWrite(MOTOR_B_DIR, LOW); //STOP
//digitalWrite(MOTOR_B_PWM, LOW);
//digitalWrite(LED_access, LOW);
Serial.println("FINISHED!");
}
}
}
else { // If the card is not recognized
Serial.println("Card not recognized! Contact administrator!"); // Print message to Serial Monitor
digitalWrite(LED_intruder, HIGH); // Turn on red LED
for (int i = 0; i < 6; i++) { // Play intruder melody
int fail_noteDuration = 1000 / fail_noteDurations[i];
tone(speaker_pin, fail_melody[i], fail_noteDuration);
int fail_pauseBetweenNotes = fail_noteDuration * 1.30;
delay(fail_pauseBetweenNotes);
noTone(speaker_pin);
}
delay(2000);
digitalWrite(LED_intruder, LOW);
}
}
}
void stop()
{
{
digitalWrite(MOTOR_B_DIR, LOW); //STOP
digitalWrite(MOTOR_B_PWM, LOW);
digitalWrite(LED_access, LOW);
Serial.println("FINISHED!");
}
}
