Mattfox27
Posts: 10
Joined: Thu Feb 27, 2014 11:51 am

Help with printing 2 messages on LCD

Mon Mar 03, 2014 12:40 am

Im trying to run a python code that will show the price of bitcoins to USD and Dogecoins to Bitcoins, i have the 2 different scripts and they work fine i want to combine them and make it rotate the 2 outputs at a set times like every 15 seconds or so.

Or just how can i have it show one line, then switch to another line, basically show one message then another one?

BTC/USD

Code: Select all

 #!/usr/bin/python
 
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
from subprocess import *
from time import sleep, strftime
from datetime import datetime
 
import json
import urllib2
import time

lcd = Adafruit_CharLCDPlate()
lcd.begin(16,1)
 
def BTC():
	stamp = json.load(urllib2.urlopen('https://www.bitstamp.net/api/ticker/'))
	current_price = stamp["last"]
	# > $1000 rollover, Rounds/Converts to int
	if (int(round(float(current_price))) > 1000):
		stamp_price = int(round(float(current_price)))
	else:
		stamp_price = current_price
	return stamp_price

def DOGE():
        cryptsy = json.load(urllib2.urlopen('http://data.mtgox.com/api/1/BTCUSD/ticker'))
        if cryptsy["result"] != "1":
                raise Exception("Cryptsy API failed!")

        current_price = cryptsy["return"]["markets"]["DOGE"]["lasttradeprice"]
        # No rollover necessary, but need to figure out how to deal with decimal places
        return current_price

#Add support for switching between currencies by using button code
while 1:
        lcd.clear()
        #Calls BTC function, gets time and formats.
        try:
                price = BTC()
        except Exception:
                lcd.message("BitStamp API failed! :(")

        time = datetime.now().strftime( '%x %I:%M%p\n' )
        #Displays time on first line, BTC/USD rate on next line
        lcd.message(time)
        lcd.message( "BTC/USD: " + "$" + price)
        #Sleeps until next API call is possible
        #Needs to be customized per API, add support next
        sleep(30)


DOGE to BTC

Code: Select all

 #!/usr/bin/python

from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
from subprocess import *
from time import sleep, strftime
from datetime import datetime

import json
import urllib2
import time

lcd = Adafruit_CharLCDPlate()
lcd.begin(16,1)

def DOGE():
        cryptsy = json.load(urllib2.urlopen('http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=132'))
        #if cryptsy["success"] != "1":
        #                raise Exception("Cryptsy API failed!")
        current_price = cryptsy["return"]["markets"]["DOGE"]["lasttradeprice"]
        # No rollover necessary, but need to figure out how to deal with decimal places
        return current_price

#Add support for switching between currencies by using button code
while 1:
        lcd.clear()
        #Calls BTC function, gets time and formats
        price = DOGE()
        time = datetime.now().strftime( '%x %I:%M%p\n' )
        #Displays time on first line, BTC/USD rate on next line
        lcd.message(time)
        lcd.message( "DOGE: " + price)
        #Sleeps until next API call is possible
        #Needs to be customized per API, add support next
        sleep(30)
What can i do here to make it rotate between the 2 outputs?

Code: Select all

   #Displays time on first line, BTC/USD rate on next line
        lcd.message(time)
        lcd.message( "BTC/USD: " + "$" + price)
        #Sleeps until next API call is possible
        #Needs to be customized per API, add support next
        sleep(30)

Return to “Python”