งาน ปฎิบัติ 3
7-Segmant
wiring diagram
Code 1
#include "LedControl.h"
LedControl lc=LedControl(5,7,6,1);
int num = 0,seg1,seg2;
void setup()
{
lc.shutdown(0,false);
lc.setIntensity(0,5);
lc.clearDisplay(0);
}
void loop()
{
seg1=num%10;
seg2=num/10;
lc.setDigit(0,0,seg1,false);
lc.setDigit(0,1,seg2,false);
delay(1000);
num=num+1;
if (num > 99)
{
num = 0;
}
}
Code 2
#include "LedControl.h"
LedControl lc=LedControl(5,7,6,1);
int num = 0,seg1,seg2;
void setup()
{
lc.shutdown(0,false);
lc.setIntensity(0,5);
lc.clearDisplay(0);
}
void loop()
{
seg1=num%10;
seg2=num/10;
lc.setDigit(0,0,seg1,false);
lc.setDigit(0,1,seg2,false);
delay(1000);
num=num+1;
if (num > 99)
{
num = 0;
}
}
Code 2
#include "LedControl.h"
int count=0;
int sw = 2;
LedControl lc=LedControl(5,7,6,1);
// Pin 5->DIN, 7->CLK, 6->CS(LOAD), 1 = No.of devices
void show3digit(int num)
{
int seg1,seg2,seg3;
seg1 = (num%100)%10;
seg2 = (num%100)/10;
seg3 = num/100;
lc.setDigit(0,0,seg1,false);
if (num>=10)
lc.setDigit(0,1,seg2,false);
if (num>=100)
lc.setDigit(0,2,seg3,false);
}
void setup()
{
Serial.begin(9600);
lc.shutdown(0,false);
lc.setIntensity(0,5);
lc.clearDisplay(0);
pinMode(sw,INPUT);
}
void loop()
{
int x = digitalRead(sw);
if (x==0)
{
delay(300);
if (x==0)
{
count=count+1;
if (count>999)
count=0;
}
}
show3digit(count);
Serial.print("count = ");Serial.println(count);
}
Code 3
#include "LedControl.h"
int count=0;
int sw = 2;
int number;
LedControl lc=LedControl(5,7,6,1);
// Pin 5->DIN, 7->CLK, 6->CS(LOAD), 1 = No.of devices
void show3digit(int num)
{
int seg1,seg2,seg3;
seg1 = (num%100)%10;
seg2 = (num%100)/10;
seg3 = num/100;
lc.setDigit(0,0,seg1,false);
if (num>=10)
lc.setDigit(0,1,seg2,false);
if (num>=100)
lc.setDigit(0,2,seg3,false);
}
void setup()
{
lc.shutdown(0,false);
lc.setIntensity(0,5);
lc.clearDisplay(0);
pinMode(sw,INPUT);
randomSeed(analogRead(0));
number = random(1000);
}
void loop()
{
int x = digitalRead(sw);
if (x==0)
{
delay(300);
if (x==0)
{
number = random(1000);
}
}
show3digit(number);
}
ความคิดเห็น
แสดงความคิดเห็น