Sensor de distância Ultrasonico com o Atmega328 – Arduino.
Autor: Victorio | Publicado: 2013-01-24T16:34:00-08:00






















Segue código que utilizei no projeto.



// O código original é o que está no link abaixo, fiz algumas adaptações para poder visualizar no display de sete seguimentos.

// http://arduino.cc/en/Tutorial/ShftOut23

// Segue o código que adaptei. Janeiro de 2013

// Por: Marcelo Victorio.





//Pin connected to ST_CP of 74HC595

int latchPin = 8;

//Pin connected to SH_CP of 74HC595

int clockPin = 10;

////Pin connected to DS of 74HC595

int dataPin = 9;



////Pin Sensor Ultrasonic

#define trigPin 12

#define echoPin 13



//holders for infromation you're going to pass to shifting function

byte dataDISPA;

byte dataDISPB;

byte dataArrayDISPA[10];

byte dataArrayDISPB[10];



void setup() {

&nbsp; //set pins to output because they are addressed in the main loop

&nbsp; pinMode(latchPin, OUTPUT);

&nbsp; Serial.begin(9600);



&nbsp; pinMode(trigPin, OUTPUT);

&nbsp; pinMode(echoPin, INPUT);



//Marcelo - 26/08/2012



&nbsp; dataArrayDISPA[0] = 0x3F; //00111111

&nbsp; dataArrayDISPA[1] = 0x06; //00000110

&nbsp; dataArrayDISPA[2] = 0x5B; //01011011

&nbsp; dataArrayDISPA[3] = 0x4F; //01001111

&nbsp; dataArrayDISPA[4] = 0x66; //01100110

&nbsp; dataArrayDISPA[5] = 0x6D; //01101101

&nbsp; dataArrayDISPA[6] = 0x7D; //01111101

&nbsp; dataArrayDISPA[7] = 0x07; //00000111

&nbsp; dataArrayDISPA[8] = 0x7F; //01111111

&nbsp; dataArrayDISPA[9] = 0x67; //01100111



&nbsp; dataArrayDISPB[0] = 0x3F; //00111111

&nbsp; dataArrayDISPB[1] = 0x06; //00000110

&nbsp; dataArrayDISPB[2] = 0x5B; //01011011

&nbsp; dataArrayDISPB[3] = 0x4F; //01001111

&nbsp; dataArrayDISPB[4] = 0x66; //01100110

&nbsp; dataArrayDISPB[5] = 0x6D; //01101101

&nbsp; dataArrayDISPB[6] = 0x7D; //01111101

&nbsp; dataArrayDISPB[7] = 0x07; //00000111

&nbsp; dataArrayDISPB[8] = 0x7F; //01111111

&nbsp; dataArrayDISPB[9] = 0x67; //01100111



&nbsp; //function that blinks all the LEDs

&nbsp; //gets passed the number of blinks and the pause time

&nbsp; blinkAll_2Bytes(2,500);&nbsp;

}



void loop() {



&nbsp; int duration, distance;



&nbsp; digitalWrite(trigPin, LOW);&nbsp;

&nbsp; delayMicroseconds(2);&nbsp;

&nbsp; digitalWrite(trigPin, HIGH);&nbsp;

&nbsp; delayMicroseconds(10);&nbsp;

&nbsp; digitalWrite(trigPin, LOW);&nbsp;

&nbsp; duration = pulseIn(echoPin, HIGH);&nbsp;

&nbsp; distance = (duration/2) / 29.1;

&nbsp; if (distance &gt;= 200 || distance &lt;= 0){

&nbsp; &nbsp; &nbsp;distance = 0;&nbsp;

&nbsp; }

&nbsp; &nbsp;



&nbsp; //load the light sequence you want from array



&nbsp; int jj = distance;



&nbsp; int x = jj%10;

&nbsp; int y = jj/10;



&nbsp; &nbsp; &nbsp; dataDISPB = dataArrayDISPB[y];

&nbsp; &nbsp; &nbsp; dataDISPA = dataArrayDISPA[x];

&nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; //ground latchPin and hold low for as long as you are transmitting

&nbsp; &nbsp; &nbsp; digitalWrite(latchPin, 0);

&nbsp; &nbsp; &nbsp; //move 'em out

&nbsp; &nbsp; &nbsp; shiftOut(dataPin, clockPin, dataDISPB); &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; shiftOut(dataPin, clockPin, dataDISPA);

&nbsp; &nbsp; &nbsp; //return the latch pin high to signal chip that it&nbsp;

&nbsp; &nbsp; &nbsp; //no longer needs to listen for information

&nbsp; &nbsp; &nbsp; digitalWrite(latchPin, 1);



&nbsp; &nbsp; delay(800);

}







// the heart of the program

void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {

&nbsp; // This shifts 8 bits out MSB first,&nbsp;

&nbsp; //on the rising edge of the clock,

&nbsp; //clock idles low



&nbsp; //internal function setup

&nbsp; int i=0;

&nbsp; int pinState;

&nbsp; pinMode(myClockPin, OUTPUT);

&nbsp; pinMode(myDataPin, OUTPUT);



&nbsp; //clear everything out just in case to

&nbsp; //prepare shift register for bit shifting

&nbsp; digitalWrite(myDataPin, 0);

&nbsp; digitalWrite(myClockPin, 0);



&nbsp; //for each bit in the byte myDataOut

&nbsp; //NOTICE THAT WE ARE COUNTING DOWN in our for loop

&nbsp; //This means that 000001 or "1" will go through such

&nbsp; //that it will be pin Q0 that lights.&nbsp;

&nbsp; for (i=7; i&gt;=0; i--) &nbsp;{

&nbsp; &nbsp; digitalWrite(myClockPin, 0);



&nbsp; &nbsp; //if the value passed to myDataOut and a bitmask result&nbsp;

&nbsp; &nbsp; // true then... so if we are at i=6 and our value is

&nbsp; &nbsp; // %11010100 it would the code compares it to %01000000&nbsp;

&nbsp; &nbsp; // and proceeds to set pinState to 1.

&nbsp; &nbsp; if ( myDataOut &amp; (1&lt;&lt;i) ) {

&nbsp; &nbsp; &nbsp; pinState= 1;

&nbsp; &nbsp; }

&nbsp; &nbsp; else { 

&nbsp; &nbsp; &nbsp; pinState= 0;

&nbsp; &nbsp; }



&nbsp; &nbsp; //Sets the pin to HIGH or LOW depending on pinState

&nbsp; &nbsp; digitalWrite(myDataPin, pinState);

&nbsp; &nbsp; //register shifts bits on upstroke of clock pin &nbsp;

&nbsp; &nbsp; digitalWrite(myClockPin, 1);

&nbsp; &nbsp; //zero the data pin after shift to prevent bleed through

&nbsp; &nbsp; digitalWrite(myDataPin, 0);

&nbsp; }



&nbsp; //stop shifting

&nbsp; digitalWrite(myClockPin, 0);

}





//blinks the whole register based on the number of times you want to&nbsp;

//blink "n" and the pause between them "d"

//starts with a moment of darkness to make sure the first blink

//has its full visual effect.

void blinkAll_2Bytes(int n, int d) {

&nbsp; digitalWrite(latchPin, 0);

&nbsp; shiftOut(dataPin, clockPin, 0);

&nbsp; shiftOut(dataPin, clockPin, 0);

&nbsp; digitalWrite(latchPin, 1);

&nbsp; delay(200);

&nbsp; for (int x = 0; x &lt; n; x++) {

&nbsp; &nbsp; digitalWrite(latchPin, 0);

&nbsp; &nbsp; shiftOut(dataPin, clockPin, 255);

&nbsp; &nbsp; shiftOut(dataPin, clockPin, 255);

&nbsp; &nbsp; digitalWrite(latchPin, 1);

&nbsp; &nbsp; delay(d);

&nbsp; &nbsp; digitalWrite(latchPin, 0);

&nbsp; &nbsp; shiftOut(dataPin, clockPin, 0);

&nbsp; &nbsp; shiftOut(dataPin, clockPin, 0);

&nbsp; &nbsp; digitalWrite(latchPin, 1);

&nbsp; &nbsp; delay(d);

&nbsp; }

}