Ping and iButton together

Since I had both the Ping and iButton at hand I decided to test them together. My intention is to make it so that the PING only works when the iButton receives the right key/button. If the wrong key is put the led will bling three times. The course

 

 

 

 

 

Lauri and Suvi’s modified code:

[edit]

Code unusable since wordpress decides to change it. Example ” is "

[edit2]

Should be fixed

#include <OneWire.h>

//iButton
OneWire ds(7); //onewire 7th slot.
byte addr[8]; //iButton's fingerprint.
int but[6] = {195,133,181,20,0,0};
String keyStatus="";//why does wordpress change my code?!

//PING
const int pingPin = 8;//Ping sensor on 8th slot
long int duration, distanceCm;
int limitCm = 20;//distance

void setup(void)
{
 Serial.begin(115200);
 pinMode(13, OUTPUT);
}

void loop(void)
{
  getKeyCode(); //check if there is a code and if it is the ds1990a-buttons code.

  Serial.println();
  if(keyStatus=="ok")
  {
    //continue here if getKeyCode has read the right kind of button.
    byte i;
    for( i = 5; i >0; i--)
    {
      Serial.print(" : ");
      Serial.print(addr[i], DEC);
    }
    if(addr[1] == but[0] && addr[2] == but[1] && addr[3] == but[2] && addr[4] == but[3])
    {
      digitalWrite(13, HIGH);
      delay(1);
      //start the PING
      pinMode(pingPin, OUTPUT);

      digitalWrite(pingPin, LOW);

      delayMicroseconds(2);

      digitalWrite(pingPin, HIGH);

      delayMicroseconds(5);

      digitalWrite(pingPin, LOW);

      pinMode(pingPin, INPUT);

      duration = pulseIn(pingPin, HIGH);

      distanceCm = microsecondsToCentimeters(duration);

      Serial.println (distanceCm);

      delay(100);
    }
    else
    {
      //Wrong key/button. Blink led 3 times
      digitalWrite(13, LOW);
      delay(100);
      digitalWrite(13, HIGH);
      delay(250);
      digitalWrite(13, LOW);
      delay(100);
      digitalWrite(13, HIGH);
      delay(250);
      digitalWrite(13, LOW);
      delay(100);
      digitalWrite(13, HIGH);
      delay(250);
      digitalWrite(13, LOW);
      delay(100);
    }
  }
    else if (keyStatus!="")
    {
      Serial.print(keyStatus);
    }
    delay(1000);
  }

void getKeyCode()
{
 keyStatus="";

 //return to beginning if reader doensn't get any contact from button.
 if ( !ds.search(addr))
{
 //keyStatus="testi";
//tämän kirjoitin testiksi, ja totesinkin lukijan olevan väärässä pinnissä
 //(ohjeen mukainen 2, ei koodin mukainen 12),
 //kun ohjelma tulosti koko ajan pelkkää "testiä".
 ds.reset_search();
 return;
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
keyStatus="CRC invalid";
return;
}

//Check if the button is ds1990a-model.
if ( addr[0] != 0x01) {
keyStatus="not DS1990A";
return;
}

//if the code reached here without any errors the keystatus is ok.
keyStatus="ok";
ds.reset();
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

Everything didn’t go as planned. The output was supposed to show the distance the PING sensor calculated

EDIT

Got the “copy paste error” fixed. just deleted the lines 30-34.

The modified and cleaned code:


#include <OneWire.h>

//iButton
OneWire ds(7); //onewire 7th slot.
byte addr[8]; //iButton's fingerprint.
int but[6] = {195,133,181,20,0,0};
String keyStatus="";

//PING
const int pingPin = 8;//Ping sensor on 8th slot
long int duration, distanceCm;

void setup(void)
{
 Serial.begin(115200);
 pinMode(13, OUTPUT);
}

void loop(void)
{
  getKeyCode(); //check if there is a code and if it is the ds1990a-buttons code.

  //Serial.println();TEST
  if(keyStatus=="ok")
  {
    //continue here if getKeyCode has read the right kind of button.
    byte i;

    if(addr[1] == but[0] && addr[2] == but[1] && addr[3] == but[2] && addr[4] == but[3])
    {
      keyOk();
    }
    else
    {
      keyNotOk();
    }
  }
    else if (keyStatus!="")
    {
      Serial.print(keyStatus);
    }
    delay(1000);
  }

void getKeyCode()
{
 keyStatus="";

 //return to beginning if reader doensn't get any contact from button.
 if ( !ds.search(addr))
{
 //keyStatus="testi";
 //tämän kirjoitin testiksi, ja totesinkin lukijan olevan väärässä pinnissä
 //(ohjeen mukainen 2, ei koodin mukainen 12),
 //kun ohjelma tulosti koko ajan pelkkää "testiä".
 ds.reset_search();
 return;
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
keyStatus="CRC invalid";
return;
}

//Check if the button is ds1990a-model.
if ( addr[0] != 0x01) {
keyStatus="not DS1990A";
return;
}

//if the code reached here without any errors the keystatus is ok.
keyStatus="ok";
ds.reset();
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

void keyOk()
{
      digitalWrite(13, HIGH);
      delay(1);
      //start the PING
      pinMode(pingPin, OUTPUT);

      digitalWrite(pingPin, LOW);

      delayMicroseconds(2);

      digitalWrite(pingPin, HIGH);

      delayMicroseconds(5);

      digitalWrite(pingPin, LOW);

      pinMode(pingPin, INPUT);

      duration = pulseIn(pingPin, HIGH);

      distanceCm = microsecondsToCentimeters(duration);

      Serial.print(distanceCm);
      Serial.println("cm");

      delay(100);
}

void keyNotOk()
{
      //Wrong key/button. Blink led 3 times
      digitalWrite(13, LOW);
      delay(100);
      digitalWrite(13, HIGH);
      delay(250);
      digitalWrite(13, LOW);
      delay(100);
      digitalWrite(13, HIGH);
      delay(250);
      digitalWrite(13, LOW);
      delay(100);
      digitalWrite(13, HIGH);
      delay(250);
      digitalWrite(13, LOW);
      delay(100);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s