Pages: [1]   Go Down
Author Topic: Ping/RFID Reader not working.  (Read 414 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 43
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote some code for the TouchShield and Arduino to make a basic GUI that I'm calling pArduOS. For some reason, the RFID reader (Parallax) and the Ping)) won't report their data to the computer.

Code follows:

TouchShield Code
Code:
void setup() {
  Serial.begin(9600);
  background(0,0,0);
  stroke(255);
  text("pArduOS, Version .1",205,2);
  text("Written by Adam Aitoumeziane",150,10);
  text("Includes code samples from arduino.cc",95,18);
  fill(0,0,255);
  rect(0,0,50,50);
  text("Ping",12,22);
  fill(0,255,0);
  rect(0,75,50,50);
  text("RFID",12,97);

}

void loop() {
  gettouch();

  if(mouseX > 0 & mouseX < 50 & mouseY > 0 & mouseY < 50) {
    Serial.print('A');
  }

  if(mouseX > 0 & mouseX < 50 & mouseY > 75 & mouseY < 125) {
    Serial.print('B');
  }
}

Arduino Code
Code:
#include <AFSoftSerial.h>
#define RXPIN 3
#define TXPIN 2


int pingPin = 7;
char myChar;
int  val = 0;
char code[10];
int bytesread = 0;

AFSoftSerial tss = AFSoftSerial(RXPIN,TXPIN);


void setup() {
  tss.begin(9600);
  Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
  pinMode(4,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
  digitalWrite(4, LOW);                  // Activate the RFID reader
}

void loop() {
  if(Serial.available() > 0) {
    myChar = tss.read();
  }


  if(myChar == 'A') {
    pingProg();
  }

  if(myChar == 'B') {
    rfidProg();
  }
}

int pingProg() {
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  

  delay(100);
}

int rfidProg() {
  if(Serial.available() > 0) {          // if data available from reader
    if((val = Serial.read()) == 10) {   // check for header
      bytesread = 0;
      while(bytesread<10) {              // read 10 digit code
        if( Serial.available() > 0) {
          val = Serial.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) {              // if 10 digit read is complete
        Serial.print("TAG code is: ");   // possibly a good TAG
        Serial.println(code);            // print the TAG code
      }
      bytesread = 0;
      delay(500);                       // wait for a second
    }
  }
}

  long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 43
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nevermind!

I fixed the error.

This is what the problem was:
Code:
 if(Serial.available() > 0) {
    myChar = tss.read();
  }

It should say:

Code:
 if(tss.available() > 0) {
    myChar = tss.read();
  }
Logged

Pages: [1]   Go Up
Jump to: