This wiki has been archived and made read-only.
For up-to-date information about TkkrLab and it's projects please visit our main website at tkkrlab.nl.

Arduino Serial Lcd and Irc?

From

Revision as of 15:09, 16 December 2013 by Duality (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Goal:
learning what it takes to communicate between a pc/laptop and a arduino or simmilar device

python code in python2.x
python code:

#author Duality
 
import serial
import socket
import sys
ser = serial.Serial(str(raw_input("enter device for example: /dev/xx: ")), int(raw_input("and baud: ")))
 
server = "irc.freenode.net"
channel = raw_input("Enter channel: ")
#channel = "#test1123"
nick = "ArduDuaDisp"
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircDone = False
 
def ping():
	ircsock.send("PONG :Pong\n")
 
def sendmsg(chan, msg):
	ircsock.send("PRIVMSG "+chan+" :"+msg+"\n")
 
def joinchan(chan):
	ircsock.send("JOIN "+chan+"\n")
 
def connectIrc():
	ircsock.connect((server, 6667))
	ircsock.send("USER "+nick+" "+nick+" "+nick+" :Arduino Irc Thingy\n")
	ircsock.send("NICK "+nick+"\n")
	joinchan(channel)
 
def irc():
	ircmsg = ircsock.recv(2048)
	ircmsg = ircmsg.strip('\n\r')
	ircmsg = str(ircmsg).lower()
	if ircmsg.find("ping :") != -1:
		ping()
	print nick+" >> "+ircmsg
	if ircmsg.find("privmsg") != -1:
		return ircmsg
 
connectIrc()
 
while True:
	disp = irc()
	if disp != None:
		disp = disp.split(' ')
		for l in xrange(len(disp)):
			if disp[l] == "privmsg":
				disp = disp[l:len(disp)][2:]
				disp = ' '.join(disp)
				disp = disp[1:]
				break
		print ">> displayed >>: ",disp
		ser.write(str(disp))


arduino code:

/*
author: Duality
*/
 
#include <IOlcd.h>
#include <Wire.h>
 
IOlcd lcd;
int x;
int y;
 
void setup(){
	Serial.begin(115200);
	if(lcd.init(0x20, MCP23016)){
		Serial.println("Communication with IOlcd works!");
		}
	else{
		Serial.println("Not connected propperly!");
		}
	lcd.begin(16,2);
	lcd.clear();
	}
 
void loop(){
	if(Serial.available()){
		delay(1000);
		lcd.clear();
		x=0;
		y=0;
		while(Serial.available() > 0){
			if(x == 16){
				x=0;
				y=1;
				}
			else if(x == 16 && y == 1){
				lcd.clear();
				lcd.home();
				x=0;
				y=0;
				}
			lcd.setCursor(x,y);
			Serial.print(x);
			Serial.println(" ");
			lcd.write(Serial.read());
			x++;
			}
		}
	}


adjusted the code for longer lines, which made it more complicated :) and more fun...

/*
author: Duality
*/
 
#include <IOlcd.h>
#include <Wire.h>
 
IOlcd lcd;
 
int buffSize = 64;
char screenBuf[65];
int arrayIndex = 0;
 
long previousMillis = 0;
long interval = 1000;
 
int state = 1;
 
void setup(){
	Serial.begin(115200);
	if(lcd.init(0x20, MCP23016)){
		Serial.println("Communication with the IOlcd works!");
		}
	else{
		Serial.println("Not connected propperly!");
		}
	lcd.begin(16,2);
	lcd.clear();
	flushBuffer();
	}
 
void loop(){
	unsigned long currentMillis= millis();
	pinMode(13, OUTPUT);
	if(Serial.available()!=0){
		arrayIndex = 0;
		flushBuffer();
		while(Serial.available() != 0){
			delay(20);
			screenBuf[arrayIndex] = Serial.read();
			arrayIndex++;
			if(arrayIndex == buffSize){
				break;
				}
			}
		}
	else if(currentMillis - previousMillis > interval){
			previousMillis = currentMillis;
			//Serial.println("numelements: \n\r");
			//Serial.println(countElements());
			//Serial.println("currentMillis: \n\r");
			//Serial.println(currentMillis);
			//Serial.println("What is in screen buffer: \n\r");
			//Serial.println(screenBuf);
			printToLcd();
		}
	}
 
void printToLcd(){
	int numElements = countElements();
	if(numElements >32){
		//Serial.println("the state: ");
		//Serial.println(state);
		state = switchLines();
		//digitalWrite(13, state);
		toggleLines(state);
		}
	else{
		singleLineDisp();
		}
	}
 
void singleLineDisp(void){
	for(int i=0;i<32;i++){
			if(i>=16 && screenBuf[i]!='\0'){
				lcd.setCursor((i-16), 1);
				lcd.print(screenBuf[i]);
				}
			else if(screenBuf[i]!='\0'){
				lcd.setCursor(i,0);
				lcd.print(screenBuf[i]);
				}
			}
	}
 
void toggleLines(int _state){
	lcd.clear();
	if(state == 1){
		singleLineDisp();
		}
	else if(state == 0){
		for(int i=32;i<64;i++){
			if(i>=48 && screenBuf[i]!='\0'){
				lcd.setCursor((i-48), 1);
				lcd.print(screenBuf[i]);
				}
			else if(screenBuf[i]!='\0'){
				lcd.setCursor((i-32), 0);
				lcd.print(screenBuf[i]);
				}
			}
		}
	}
 
int switchLines(){
	if(state == 1){
		state = 0;
		}
	else{
		state = 1;
		}
	return state;
	}
 
int countElements(void){
	int count = 0;
	for(int i = 0;i<buffSize;i++){
		if(screenBuf[i] != '\0'){
			count ++;
			}
		}
	return count;
	}
 
void flushBuffer(void){
	for(int i = 0;i<buffSize;i++){
		screenBuf[i] = '\0';
		}
	lcd.clear();
	}

little update latest code/sketch:

/*
author: Duality
*/
 
#include <IOlcd.h>
#include <Wire.h>
 
IOlcd lcd;
 
int buffSize = 256;
char screenBuf[257];
int arrayIndex = 0;
 
long previousMillis = 0;
long interval = 1500;
 
int first = 0;
int lineLen = 32;
 
void setup(){
	pinMode(13, OUTPUT);
	Serial.begin(115200);
	if(lcd.init(0x20, MCP23016)){
		Serial.println("Communication with the IOlcd works!");
		}
	else{
		Serial.println("Not connected propperly!");
		}
	lcd.begin(16,2);
	lcd.clear();
	flushBuffer();
	}
 
void loop(){
	unsigned long currentMillis = millis();
	if(Serial.available()!=0){
		arrayIndex = 0;
		flushBuffer();
		while(Serial.available() != 0){
			delay(20);
			screenBuf[arrayIndex] = Serial.read();
			arrayIndex++;
			if(arrayIndex == buffSize){
				arrayIndex = 0;
				}
			}
		}
	else if(currentMillis - previousMillis > interval){
			previousMillis = currentMillis;
			printScreen();
		}
	}
 
void printScreen(){
	int numElements = countElements();
	if(first == buffSize || first >= numElements){
		first = 0;
		}
	else{
		lineDisp(first, lineLen);
		first += 32;
		}
	}
void lineDisp(int startline ,int lineSize){
	for(int i = startline;i<(startline+lineSize);i++){
		if(i>=(startline+lineSize-16)&&screenBuf[i]=='\0'){
			lcd.setCursor((i-(startline+lineSize-16)),1);
			lcd.print(" ");
			}
		else if(screenBuf[i]=='\0'){
			lcd.setCursor((i-startline),0);
			lcd.print(" ");
			}
		if(i>=(startline+lineSize-16)&&screenBuf[i]!='\0'){
			lcd.setCursor((i-(startline+lineSize-16)),1);
			lcd.print(screenBuf[i]);
			}
		else if(screenBuf[i]!='\0'){
			lcd.setCursor((i-startline),0);
			lcd.print(screenBuf[i]);
			}
		}
	}
 
int countElements(void){
	int count = 0;
	for(int i = 0;i<buffSize;i++){
		if(screenBuf[i] != '\0'){
			count ++;
			}
		}
	return count;
	}
 
void flushBuffer(void){
	for(int i = 0;i<buffSize+1;i++){
		screenBuf[i] = '\0';
		}
	lcd.clear();
	}