Lab_interaccio/2015/PAONESA/PAONESA.ino

88 lines
2.6 KiB
Arduino
Raw Permalink Normal View History

2025-02-25 21:29:42 +01:00
/***************************************************
This is a library for our I2C LED Backpacks
Designed specifically to work with the Adafruit LED 7-Segment backpacks
----> http://www.adafruit.com/products/881
----> http://www.adafruit.com/products/880
----> http://www.adafruit.com/products/879
----> http://www.adafruit.com/products/878
These displays use I2C to communicate, 2 pins are required to
interface. There are multiple selectable I2C addresses. For backpacks
with 2 Address Select pins: 0x70, 0x71, 0x72 or 0x73. For backpacks
with 3 Address Select pins: 0x70 thru 0x77
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
// Enable one of these two #includes and comment out the other.
// Conditional #include doesn't work due to Arduino IDE shenanigans.
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
#include <EEPROM.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include "pitches.h"
Adafruit_7segment matrix = Adafruit_7segment();
#define ECHOPIN 2 // Pin to receive echo pulse
#define TRIGPIN 3 // Pin to send trigger pulse
#define distancia 60 // Distancia en cm
int counter = 0;
float sensor(){
delay(50); // Wait 50mS before next ranging
return analogRead(A0)*2.54/2.;
}
void setup() {
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
counter = EEPROM.read(0);
matrix.begin(0x70);
matrix.println(counter);
matrix.writeDisplay();
}
unsigned long time = 0;
void loop() {
if (sensor()<distancia)
{
if ((millis()-time)>=2000)
{
if (counter>=99) counter = 0;
else counter++;
EEPROM.write(0, counter);
tone(8, NOTE_C4,100);
matrix.println(counter);
matrix.writeDisplay();
Serial.println(sensor());
while(sensor()<distancia);
}
}
else
{
time = millis();
}
Serial.println(sensor());
// for (uint16_t counter = 0; counter < 9999; counter++) {
// matrix.println(counter);
// matrix.writeDisplay();
// delay(1000);
// }
}