Category Archives: Research

Sangyun Han: a partial diagram of my project.

I will use RFID technology for my project.
Here I found a base code of connecting Arduino and RFID reader.

P.S. Currently, the code generates an error yet.



#include
NewSoftSerial mySerial(8, 10);

int cardPresent = 9;
int led_pin = 13;

void setup()
{
Serial.begin(57600);
Serial.println("RFID Reader Active");
pinMode(cardPresent, INPUT);
pinMode(led_pin, OUTPUT);
mySerial.begin(2400);
}

void loop()
{

if (digitalRead(cardPresent)==HIGH) {
digitalWrite(led_pin, HIGH);
} else {
digitalWrite(led_pin, LOW);
}
if (mySerial.available()) {
Serial.print((char)mySerial.read(),HEX);
}
}

Sangyun Han: Pressure Sensor + Piezo spk while an LED is fading.

Here I documented the instruction for making sound by a piezo speaker while an LED is blinking.


/*
* LED attached from digital pin 9 to ground.
* spk attached from pin 13 to ground
* pressuresensor attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
*/
const int buttonPin = 2;      // the number of the pushbutton pin
const int spkPin =  10;       // the number of the blinking spk pin
const int ledPinf = 9;        // the number of the fading LED pin
int fadeValue = 0;            // variable for the brightness of the fading LED
boolean fadeUp = true;        // variable to keep track of if the LED is fading up or down
int buttonState = 0;          // variable for reading the pressuresensor status
int spkState = LOW;             // ledState used to set the blinking spk on or off
long previousMillis = 0;        // will store last time LED was updated
// the follow variable is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1700;           // interval at which to blink (milliseconds)
boolean blinkOn = false;      // variable to keep track of if blink spk should be blinking or not
int blink2 = 0;                // variable to keep track of how many times the spk blinks
void setup()  {
// initialize the blinking spk pin as an output:
// initialize the pressuresensor pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);         // you only need this to send data to the serial port
}
void loop()  {
// -------  LED fading  ---------------
if (fadeUp == true) {               // if we are making it brighter
fadeValue +=5;                    //increase the brightness by 5
if (fadeValue >= 255) {          // if the brightness gets too high
fadeUp=false;                    // we are not fading up
}
analogWrite(ledPinf, fadeValue);  // send the brightness to the LED
delay(15);
}
else {                              // if we are fading down
fadeValue -=5;
if (fadeValue <= 5) {
fadeUp=true;
}
analogWrite(ledPinf, fadeValue);
delay(15);
}
// --------- check the button ------------------
// read the state of the pressuresensor value:
buttonState = digitalRead(buttonPin);
// check if the pressuresensor is pressed.
// if it is, the buttonState is HIGH: we need to blink
if (buttonState == HIGH) {
blinkOn = true;
}
// ------------ blink the spk ------------------------
/*
This next statement asks if the current time minus the time of the last blink
is greater then the blink interval AND has blinkOn been set to true
*/
if (millis() - previousMillis > interval && blinkOn == true) {
// save the last time you blinked the spk
previousMillis = millis();
// if the spk is off turn it on and vice-versa:
analogWrite(spkPin, fadeValue);
blink2 += 1; // count the number of blinks
}
if (blink2 >= 3) { // after we do three blinks stop blinking
blink2 = 0;
blinkOn = false;
analogWrite(spkPin, 0);
}
// you can uncomment these to watch the values change in the monitor
//Serial.print(millis());
//Serial.print("  blinkOn = ");
//Serial.print(blinkOn,BIN);
//Serial.print("  blink2 = ");
//Serial.print(blink2);
//Serial.print("  spkState = ");
//Serial.print(spkState);
//Serial.print("  FadeUp = ");
//Serial.print(fadeUp,BIN);
//Serial.print("  FadeValue = ");
//Serial.print(fadeValue);
//Serial.println("");
}