/*NOTE:
it kinda works logically with serial-USB connection and with mannually-typed-in commands. but had some troubles with BT communication- virtual serial port error – and Signal Strength detection – timing, swtiching between dataMode and commandMode with \r, uncertainty with Strength indicator value and all.. - */
/* pedometer counter with BT communication
jane oh
11.18.06
**********reminder**************
miniarduino: atmega168
serial baudrate: 9600
********************************
*/
#define ledPin 13 // choose the pin for the LED
#define statPin 12
#define inPin 9 // choose the input pin (for a pushbutton)
#define break 500 // variable for the break after serial send
int val = 0; // variable for reading the pin status
int stat = 0; // variable for BT status
int counter = 0;
int currentState = 0;
int preState = 0;
int inByte = -1;
boolean far = false;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(statPin, OUTPUT);
pinMode(inPin, INPUT); // declare pushbutton as input
Serial.begin(9600);
blink(3); //blinkie
digitalWrite(inPin, LOW);
}
void loop(){
inByte = Serial.read();
if (inByte == ‘T’){ //need to type ‘T’ not T. weird..
stat = 1;
}
if (far){
stat = 0;
}
//when connected, do BT communication
if (stat == 1){
digitalWrite(statPin, HIGH);
BTtalk();
}
//when disconnected, send signal and start counting
if (stat == 0) {
digitalWrite(statPin, LOW);
//Serial.print(‘P’);
//delay(break);
stepCount();
}
}
void stepCount(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
currentState = 1;
}
else {
digitalWrite(ledPin, LOW); // turn LED off
currentState = 0;
}
if ((currentState != preState) && (stat == 0)){
if (currentState == 1){
counter = counter + 1;
//Serial.print(counter, DEC);
Serial.print(‘P’);
delay(400); //500 little lagging
}
}
preState = currentState;
}
void BTtalk(){
//when connected to TV, send counter value
if (inByte == ‘C’){
Serial.print(counter, DEC);
delay(break);
//blink(2);
Serial.print(‘S’);
}
//when sent counter value, set the value to 0
if (inByte == ‘G’){
counter = 0;
//blink(2);
}
//detect distance
checkSSI();
}
//get Signal Strength info and detect distance
void checkSSI(){
Serial.print(“+++”); //go into commandMode
// wait for the radio to respond with “OK\r”
char thisByte = 0;
while (thisByte != ‘\r’) {
if (Serial.available() > 0) {
thisByte = Serial.read();
}
}
Serial.print(“ATRSSI\r”); //get the signal strength indicator
if (inByte <= ‘-20′){
far = true;
}
else {
far = false;
}
Serial.print(“ATMD\r”); //go back to datamode
}
// Blink an LED:
void blink(int howManyTimes) {
for (int i=0; i< howManyTimes; i++) {
digitalWrite(statPin, HIGH);
delay(200);
digitalWrite(statPin, LOW);
delay(200);
}
}