Pages: [1]   Go Down
Author Topic: software serial loop  (Read 570 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

managed to get an rfid reader and servo working but want to incorporate a flashing led that would blink softly when the object is activated and off when deactivated, problem is I'm new to software serials and thats what the rest of my code is in any help would be great.
even if any one knows how to put a loop into a software serial setup. heres the code if you can help please do.

thanks in advance.
Code:
//BEGIN CODE
#include <SoftwareSerial.h>
#define servoPin 4 // control pin for servo motor (White or yellow wire of servo)
#define minPulse 500 // minimum servo position (Open position)
#define maxPulse 2200 // maximum servo position (Closed position)
#define rxPin 8 // SOUT pin of RFID module
#define txPin 9
#define enable 2 // /ENABLE pin of RFID module
#define LED1 13 // LED output pin
#define LED2 12 // other LED output pin for two-way LED (yellow)
#define LED3 10
#define switchPin 7

boolean open = true; // default start up is to assume the lock is open
int val = 0;
char code[10];
int bytesread = 0;
int pulse, switchVal;

void startblink(){
  digitalWrite(LED3,HIGH);
  delay(2000);
  digitalWrite(LED3,LOW);
  delay(2000);
}

char tag1[11] = "0800E15D4C"; // this is size 11 because it is a NULL terminating string
char tag2[11] = "0000000000"; // 25007E0E61 for small round
char tag3[11] = "0000000000"; //  2500603A78 for card tag


void LEDControl(int state){
switch (state){
case 1:
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
break;
case 2:
digitalWrite(LED2,HIGH);
digitalWrite(LED1,LOW);
break;
case 3:
for(int y=0;y<5;y++){
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
delay(250);
digitalWrite(LED2,HIGH);
digitalWrite(LED1,LOW);
delay(250);
}
}

}

boolean checkTag(char *tag){

for (int x=0;x<10;x++){
if( tag[x] != code[x]){
return false;
}
}
return true;
}

boolean findGoodTag(){
if (checkTag(tag1)){ return true;}
else if (checkTag(tag2)){ return true;}
else if (checkTag(tag3)){ return true;}

// Add more lines right here like the one above if you have more tags

else{
Serial.print("Bad tag: ");
Serial.println(code);
LEDControl(3);
return false;
}

}
void moveServo(){

if (open){
pulse = minPulse;
open = false;
LEDControl(1);
}
else if (!open){
pulse = maxPulse;
open = true;
LEDControl(2);
startblink();
}

for (int x =1;x<150;x++){
delay (10); // don't know why this works, but it does
digitalWrite(servoPin, HIGH); // start the pulse
delayMicroseconds(pulse); // pulse width
digitalWrite(servoPin, LOW); // stop the pulse
}

}



void setup() {

pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pinMode(LED1,OUTPUT); // Set LED pin as output
pinMode(LED2,OUTPUT); // Set LED pin as output
pinMode(LED3,OUTPUT);
Serial.begin(9600);
Serial.println("Begin");
pinMode(enable,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(enable, LOW); // Activate the RFID reader
pinMode(switchPin, INPUT);
}

void loop() {
SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);
RFID.begin(2400);

switchVal = digitalRead(switchPin);



if((val = RFID.read()) == 10)
{ // check for header
if(switchVal == HIGH){
Serial.println("Button");
moveServo();
}
bytesread = 0;
while(bytesread<10)
{ // read 10 digit code
val = RFID.read();
if((val == 10)||(val == 13))
{ // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}



if((bytesread == 10) && (findGoodTag()))
{ // if 10 digit read is complete
digitalWrite(enable, HIGH); // dectivate the RFID reader
moveServo();
startblink();
delay(500);
digitalWrite(enable, LOW); // Activate the RFID reader
}
}
}



Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Arduino has a code prettyfier button on the IDE's toolbar under "tools", called Auto Format (ctrl-T) - you might want to use it before posting.

The problem with SoftwareSerial is, it has to be looking at the serial line virtually all the time to receive correctly (unlike NewSoftwareSerial), so any "delay" function calls will kill it, or cause it to return incorrect results.

The answer (as nearly always) is to work through the "blink without delay" tutorial.
« Last Edit: May 13, 2010, 01:32:27 am by GrooveFlotilla » Logged

Per Arduino ad Astra

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I always work on the assumption that I've failed if I resort to delay....  

Its an immense waste of resources, having the whole processor dead for a hunk of time.
Logged


Pages: [1]   Go Up
Jump to: