Lab 2 - Uso de Display 7 Segmentos en Arduino. Entradas Analogicas
Lab 2 - Uso de Display 7 Segmentos en Arduino. Entradas Analogicas
Entradas Analogicas
byte seven_seg_digits[10][7] = {
{ 1,1,1,1,1,1,0 }, // = 0
{ 0,1,1,0,0,0,0 }, // = 1
{ 1,1,0,1,1,0,1 }, // = 2
{ 1,1,1,1,0,0,1 }, // = 3
{ 0,1,1,0,0,1,1 }, // = 4
{ 1,0,1,1,0,1,1 }, // = 5
{ 1,0,1,1,1,1,1 }, // = 6
{ 1,1,1,0,0,0,0 }, // = 7
{ 1,1,1,1,1,1,1 }, // = 8
{ 1,1,1,0,0,1,1 } // = 9
};
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
writeDot(0); // start with the "dot" off
}
void loop() {
for (byte count = 10; count > 0; --count) {
delay(1000);
sevenSegWrite(count - 1);
}
delay(4000);
}
1 Arduino .
8 resistencias de 1kOhm.
display de 7 segmentos (Cátodo común).
/*
Contador de 7-segmentos
*/
const int a = 2;
const int b = 3;
const int c = 4;
const int d = 5;
const int e = 6;
const int f = 7;
const int g = 8;
const int h = 9;
void setup() {
// initialize the digital pin as an output:
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(h, OUTPUT);
}
void Representar(int valor){
switch(valor){
case 0:
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(d, HIGH);
break;
case 1:
digitalWrite(c, HIGH);
digitalWrite(b, HIGH);
break;
case 2:
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(g, HIGH);
break;
case 3:
digitalWrite(a, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(b, HIGH);
digitalWrite(g, HIGH);
break;
case 4:
digitalWrite(b, HIGH);
digitalWrite(g, HIGH);
digitalWrite(c, HIGH);
digitalWrite(f, HIGH);
break;
case 5:
digitalWrite(a, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
break;
case 6:
digitalWrite(a, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(c, HIGH);
break;
case 7:
digitalWrite(a, HIGH);
digitalWrite(c, HIGH);
digitalWrite(b, HIGH);
break;
case 8:
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
break;
case 9:
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(g, HIGH);
digitalWrite(f, HIGH);
break;
default:
digitalWrite(h, HIGH);
break;
}
}
void LimpiarDisplay(){
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
void loop(){
for(int i=0;i<=9;i++){
LimpiarDisplay();
Representar(i);
delay(500);
}
}
Características
Especificaciones
Item Min Typical Max Unit
Current 0.2 27 80 mA
Dimensions 42x24x14 mm
Download the 4-Digit Display library and TimerOne library. Unzip and put them in the libraries file
of Arduino IDE by the path: ..\arduino-1.0\libraries.
a. ClockDisplay
b. NumberFlow
c. Stopwatch
III. MAX7219
It’s a integrated circuit from Maxim are for driving either 64 individual Led's, or up to 8 digits of 7-
segment displays. The drivers implement a SPI compatible slave interface that can be controlled
from the Arduino using only 3 of the digital output pins. An extensive datasheet for the IC's is
available from the Maxim homepage. Since both chips are very similar, I will use the term
MAX72XX for both the MAX7221 and the MAX7219.
void setup() {
pinMode(Rb5, OUTPUT);
pinMode(Rb6, OUTPUT);
pinMode(Rb7, OUTPUT);
void loop() {
init_MAX7219();
int digit;
int j = i;
writeMAX7219(1, digit);
j = j / 10;
writeMAX7219(2, digit);
writeMAX7219(3, digit);
j = j / 10;
writeMAX7219(4, digit);
delay(200);
} // end for
int j;
digitalWrite(Rb7, 1);
delayMicroseconds(20);
digitalWrite(Rb7, 0);
} // next j
}
void pulseCS(void) {
digitalWrite(Rb5, 1);
delay(1);
digitalWrite(Rb5, 0);
void init_MAX7219(void) {
ssrOut(0x09); // address
// ssrOut(0x00); // no decode
pulseCS();
// set intensity
ssrOut(0x0A); // address
ssrOut(0x0D); // 5/32s
pulseCS();
ssrOut(0x0B); // address
// ssrOut(0x07); // 8 digits
ssrOut(0x03); // 4 digits
pulseCS();
// clear MAX7219
ssrOut(i);
ssrOut(0x00);
pulseCS();
ssrOut(0x0C); // address
// ssrOut(0x00); // Off
ssrOut(0x01); // On
pulseCS();
ssrOut(data);
pulseCS();
Implementación
El código queda de la siguiente manera:
/* Analog Input
* Demonstrates analog input by reading an analog sensor on analog
* pin 0 and turning on and off a light emitting diode(LED) connected to
digital pin 13.
* The amount of time the LED will be on and off depends on the value
obtained by
* analogRead().
*/
int sensorPin = 0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT); //declare the ledPin as an OUTPUT:
}
void loop() {
sensorValue = analogRead(sensorPin);// read the value from the sensor:
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(sensorValue); // stop the program for <sensorValue> milliseconds:
digitalWrite(ledPin, LOW); // turn the ledPin off:
delay(sensorValue); // stop the program for for <sensorValue> milliseconds:
}
Codigo
Serial.begin(9600);
int sensorValue = analogRead(A0);
//PhotoResistor Pin
int lightPin = 0; //the analog pin the photoresistor is connected to
//the photoresistor is not calibrated to any units so
//this is simply a raw sensor
//value (relative light)
//LED Pin
int ledPin = 9;//the pin the LED is connected to we are controlling brightness
so
//we use one of the PWM (pulse
//width modulation pins)
void setup()
{
pinMode(ledPin, OUTPUT); //sets the led pin to output
}
/*
* loop() - this function will start after setup
* finishes and then repeat
*/
void loop()
{
int lightLevel = analogRead(lightPin); //Read the lightlevel
lightLevel = map(lightLevel, 0, 900, 0, 255); //adjust the value 0 to 900 to 0
to 255
lightLevel = constrain(lightLevel, 0, 255);
//make sure the value is betwween 0 and 255
analogWrite(ledPin, lightLevel); //write the value
}
Invertir la respuesta:
Tal vez sería más interesante una respuesta opuesta. Eso se puede hacer
fácilmente ya que sólo se cambiaria:
Luces nocturnas
En lugar de controlar el brillo del LED en respuesta a la luz, puede activar o
desactivar el led, basándose en un valor umbral. Cambie el código de loop ()
con.
void loop(){
int threshold = 300;
if(analogRead(lightPin) > threshold){
digitalWrite(ledPin, HIGH);
}else{
digitalWrite(ledPin, LOW);
}
}