Hi all.
I'm trying to make an ultrasonic sensor display to an LCD screen (well more complicated than that, but I should be able to handle the rest).
But the pin readouts don't match up with most of the tutorials I can find.
I found a really confusing example from google fu, and combined it with my ultrasonic sketch:
//Demo for 2.4in 6-digit Segment LCD display module
//by Open-Smart Team and Catalex Team
//catalex_inc@163.com
//Store: http://www.aliexpress.com/store/1199788
// http://dx.com
//Demo Function:
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;#define CS 13 //Pin 13 as chip selection output
#define WR 12 //Pin 12 as read clock output
#define DATA 7 //Pin 7 as Serial data output#define CS1 digitalWrite(CS, HIGH)
#define CS0 digitalWrite(CS, LOW)
#define WR1 digitalWrite(WR, HIGH)
#define WR0 digitalWrite(WR, LOW)
#define DATA1 digitalWrite(DATA, HIGH)
#define DATA0 digitalWrite(DATA, LOW)#define sbi(x, y) (x |= (1 << y)) /��λ����x�ĵ�yλ/
#define cbi(x, y) (x &= ~(1 <<y )) /�������x�ĵ�yλ/
#define uchar unsigned char
#define uint unsigned int//����HT1621������
#define ComMode 0x52 //4COM,1/3bias 1000 010 1001 0
#define RCosc 0x30 //�ڲ�RC����(�ϵ�Ĭ��)1000 0011 0000
#define LCD_on 0x06 //��LCD ƫѹ������1000 0000 0 11 0
#define LCD_off 0x04 //�ر�LCD��ʾ
#define Sys_en 0x02 //ϵͳ������ 1000 0000 0010
#define CTRl_cmd 0x80 //�������
#define Data_cmd 0xa0 //�������/0,1,2,3,4,5,6,7,8,9,A,b,C,c,d,E,F,H,h,L,n,N,o,P,r,t,U,-, ,/
const char num[]={0x7D,0x60,0x3E,0x7A,0x63,0x5B,0x5F,0x70,0x7F,0x7B,0x77,0x4F,0x1D,0x0E,0x6E,0x1F,0x17,0x67,0x47,0x0D,0x46,0x75,0x37,0x06,0x0F,0x6D,0x02,0x00,};
//0 1 2 3 4 5 6 7 8 9
char dispnum[6]={0x00,0x00,0x00,0x00,0x00,0x00};void SendBit_1621(uchar sdata,uchar cnt)//�����ݳ���
{
//data �ĸ�cnt λд��HT1621����λ��ǰ
uchar i;
for(i=0;i<cnt;i++)
{
WR0;
if(sdata&0x80) DATA1;
else DATA0;
WR1;
sdata<<=1;
}
}void SendCmd_1621(uchar command) //������
{
CS0;
SendBit_1621(0x80,4); //д���־�롰100����9 λcommand �������
SendBit_1621(command,8); //û��ʹ�е�����ʱ����������Ϊ�˱�̷���
CS1; //ֱ�ӽ�command �����λд��0��
}void Write_1621(uchar addr,uchar sdata) //�����ݺ��������
{
addr<<=2;
CS0;
SendBit_1621(0xa0,3); //д���־�롰101��
SendBit_1621(addr,6); //д��addr �ĸ�6λ
SendBit_1621(sdata,8); //д��data ��8λ
CS1;
}void HT1621_all_off(uchar num) //ȫ��Ϩ��1621
{
uchar i;
uchar addr=0;
for(i=0;i<num;i++)
{
Write_1621(addr,0x00);
addr+=2;
}
}void HT1621_all_on(uchar num) //ȫ������1621
{
uchar i;
uchar addr=0;
for(i=0;i<num;i++)
{
Write_1621(addr,0xff);
addr+=2;
}
}void Init_1621(void)//��ʼ��1621
{
SendCmd_1621(Sys_en);
SendCmd_1621(RCosc);
SendCmd_1621(ComMode);
SendCmd_1621(LCD_on);
}void displaydata(int p) //��ʾ��������pΪС����λ��
{
uchar i=0;
switch(p)
{
case 1:
sbi(dispnum[0],7);
break;
case 2:
sbi(dispnum[1],7);
break;
case 3:
sbi(dispnum[2],7);
break;
default:break;
}
for(i=0;i<=5;i++)
{
Write_1621(i2,dispnum);*
}*
}
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communicationpinMode(CS, OUTPUT); //*
pinMode(WR, OUTPUT); //*
pinMode(DATA, OUTPUT); //*
CS1;*
DATA1;*
WR1;*
delay(50);*
Init_1621();*
HT1621_all_on(16); //ȫ������1621*
delay(1000);*
HT1621_all_off(16); //ȫ��Ϩ��1621 *
delay(1000);*
displaydata(1);//light on the first decimal point starting from the right side*
dispnum[5]=num[5];*
dispnum[4]=num[4];*
dispnum[3]=num[3];*
dispnum[2]=num[2];*
dispnum[1]=num[1];*
dispnum[0]=num[0];*
sbi(dispnum[5],7); //��ʾ���ͼ�궥��*
//cbi(dispnum[5],7); //������ͼ�궥��*
sbi(dispnum[4],7); //��ʾ���ͼ���в�*
//cbi(dispnum[4],7); //������ͼ���в�*
sbi(dispnum[3],7); //��ʾ���ͼ��ײ�*
//cbi(dispnum[3],7); //������ͼ��ײ�*
//Write_1621(0,num[0]); //��1λ��ʾ0*
//Write_1621(2,num[28]); //��2λ��ʾ' '*
//Write_1621(4,num[2]); //��3λ��ʾ2*
//Write_1621(6,num[28]); //��4λ��ʾ' '*
//Write_1621(8,num[4]); //��5λ��ʾ4*
//Write_1621(10,num[5]); //��6λ��ʾ5*
}
void loop() {// put your main code here, to run repeatedly:*
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
}[/quote]
I'd think I'd need to add:
> if (Serial.available()) {
> // wait a bit for the entire message to arrive
> delay(100);
> // clear the screen
> lcd.clear();
> // read all the available characters
> while (Serial.available() > 0) {
> // display each character to the LCD
> lcd.write(Serial.read());
But there isn't a library used which sets up 'lcd', and I couldn't find an equivalent in the sketch, unless I'm blind.
Anybody have a better sketch example that'd work on my display, or point me in the direction of what I'm doing wrong?
Thanks in advance.