Python Scoreboard global variable noob
Question:
I am receiving an error that the variable RedScore is not defined. Originally I created this using a keyboard for raw_input and have everything working, that is until I tried to include momentary switches to control everything.
The error I am receiving is around the while statement at the end. that does not necessarily mean the rest of the program is working. I just know there is an error down there.
Can someone find my noob mistake?
import RPi.GPIO as GPIO
import time
from time import sleep
import Adafruit_CharLCD as LCD
# Raspberry Pi configuration:
lcd_rs = 27 # Change this to pin 21 on older revision Raspberry Pi's
lcd_en = 22
lcd_d4 = 25
lcd_d5 = 24
lcd_d6 = 23
lcd_d7 = 18
lcd_red = 4
lcd_green = 17
lcd_blue = 7 # Pin 7 is CE1
# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows = 2
# Initialize the LCD using the pins above.
lcd = LCD.Adafruit_RGBCharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6,
lcd_d7, lcd_columns, lcd_rows, lcd_red, lcd_green, lcd_blue)
lcd.clear()
lcd.set_color(0.0,0.0,0.0)
GPIO.setmode(GPIO.BCM)
bluebutton = 5
blueresetbutton = 6
resetbutton = 13
redresetbutton = 19
redbutton = 26
global BlueScore
global RedScore
GPIO.setup(bluebutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(blueresetbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(resetbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(redresetbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(redbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def bluebutton(channel):
global BlueScore
BlueScore = BlueScore + 1
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('Red: '+str(RedScore)+' Blue:'+str(BlueScore))
def blueresetbutton(channel):
global BlueScore
BlueScore = 11
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('Red: '+str(RedScore)+' Blue:'+str(BlueScore))
def resetbutton(channel):
global RedScore
global BlueScore
RedScore = BlueScore = 0
lcd.clear()
lcd.set_color(1.0, 1.0, 0.0)
lcd.message('NEW GAME!!!nRed: '+str(RedScore)+'Blue:'+str(BlueScore))
def redresetbutton(channel):
global RedScore
RedScore = 11
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('Red: '+str(RedScore)+' Blue:'+str(BlueScore))
def redbutton(channel):
global RedScore
RedScore = Redscore + 1
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('Red: '+str(RedScore)+' Blue:'+str(BlueScore))
GPIO.add_event_detect(5, GPIO.FALLING, callback=bluebutton, bouncetime=300)
GPIO.add_event_detect(6, GPIO.FALLING, callback=blueresetbutton, bouncetime=300)
GPIO.add_event_detect(13, GPIO.FALLING, callback=resetbutton, bouncetime=300)
GPIO.add_event_detect(19, GPIO.FALLING, callback=redresetbutton, bouncetime=300)
GPIO.add_event_detect(26, GPIO.FALLING, callback=redbutton, bouncetime=300)
while RedScore <= 21 and BlueScore <= 21:
if RedScore == 21:
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('RED WINS!!!')
sleep(2)
RedScore = BlueScore = 0
lcd.clear()
lcd.set_color(1.0, 1.0, 0.0)
lcd.message('NEW GAME!!!nRed: '+str(RedScore)+' Blue:'+str(BlueScore))
continue
elif BlueScore == 21:
lcd.clear()
lcd.set_color(0.0, 0.0, 1.0)
lcd.message('BLUE WINS!!!')
sleep(2)
BlueScore = RedScore = 0
lcd.clear()
lcd.set_color(1.0, 1.0, 0.0)
lcd.message('NEW GAME!!!nRed: '+str(RedScore)+' Blue:'+str(BlueScore))
continue
else:
sleep(1)
Answers:
Assign a value to RedScore before the while loop. E.g.
RedScore = 0
You’ll need to give BlueScore (in the bluebutton function) and RedScore (in the redbutton function) a value (probably 0) just before your loops start to manipulate them in each of those functions.
If you do this in the same place you declare your other variables, then they will only be 0 the fist time they are used and it looks like you need them to start at 0 every time you use them. BTW this also means you do not need to declare them as Global variables, which is usually worth trying to avoid if you can.
If you need to bring in values for these variables from where they have been used elsewhere, it is probably worth bringing them in as arguments in the function calls.
I am receiving an error that the variable RedScore is not defined. Originally I created this using a keyboard for raw_input and have everything working, that is until I tried to include momentary switches to control everything.
The error I am receiving is around the while statement at the end. that does not necessarily mean the rest of the program is working. I just know there is an error down there.
Can someone find my noob mistake?
import RPi.GPIO as GPIO
import time
from time import sleep
import Adafruit_CharLCD as LCD
# Raspberry Pi configuration:
lcd_rs = 27 # Change this to pin 21 on older revision Raspberry Pi's
lcd_en = 22
lcd_d4 = 25
lcd_d5 = 24
lcd_d6 = 23
lcd_d7 = 18
lcd_red = 4
lcd_green = 17
lcd_blue = 7 # Pin 7 is CE1
# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows = 2
# Initialize the LCD using the pins above.
lcd = LCD.Adafruit_RGBCharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6,
lcd_d7, lcd_columns, lcd_rows, lcd_red, lcd_green, lcd_blue)
lcd.clear()
lcd.set_color(0.0,0.0,0.0)
GPIO.setmode(GPIO.BCM)
bluebutton = 5
blueresetbutton = 6
resetbutton = 13
redresetbutton = 19
redbutton = 26
global BlueScore
global RedScore
GPIO.setup(bluebutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(blueresetbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(resetbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(redresetbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(redbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def bluebutton(channel):
global BlueScore
BlueScore = BlueScore + 1
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('Red: '+str(RedScore)+' Blue:'+str(BlueScore))
def blueresetbutton(channel):
global BlueScore
BlueScore = 11
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('Red: '+str(RedScore)+' Blue:'+str(BlueScore))
def resetbutton(channel):
global RedScore
global BlueScore
RedScore = BlueScore = 0
lcd.clear()
lcd.set_color(1.0, 1.0, 0.0)
lcd.message('NEW GAME!!!nRed: '+str(RedScore)+'Blue:'+str(BlueScore))
def redresetbutton(channel):
global RedScore
RedScore = 11
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('Red: '+str(RedScore)+' Blue:'+str(BlueScore))
def redbutton(channel):
global RedScore
RedScore = Redscore + 1
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('Red: '+str(RedScore)+' Blue:'+str(BlueScore))
GPIO.add_event_detect(5, GPIO.FALLING, callback=bluebutton, bouncetime=300)
GPIO.add_event_detect(6, GPIO.FALLING, callback=blueresetbutton, bouncetime=300)
GPIO.add_event_detect(13, GPIO.FALLING, callback=resetbutton, bouncetime=300)
GPIO.add_event_detect(19, GPIO.FALLING, callback=redresetbutton, bouncetime=300)
GPIO.add_event_detect(26, GPIO.FALLING, callback=redbutton, bouncetime=300)
while RedScore <= 21 and BlueScore <= 21:
if RedScore == 21:
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('RED WINS!!!')
sleep(2)
RedScore = BlueScore = 0
lcd.clear()
lcd.set_color(1.0, 1.0, 0.0)
lcd.message('NEW GAME!!!nRed: '+str(RedScore)+' Blue:'+str(BlueScore))
continue
elif BlueScore == 21:
lcd.clear()
lcd.set_color(0.0, 0.0, 1.0)
lcd.message('BLUE WINS!!!')
sleep(2)
BlueScore = RedScore = 0
lcd.clear()
lcd.set_color(1.0, 1.0, 0.0)
lcd.message('NEW GAME!!!nRed: '+str(RedScore)+' Blue:'+str(BlueScore))
continue
else:
sleep(1)
Assign a value to RedScore before the while loop. E.g.
RedScore = 0
You’ll need to give BlueScore (in the bluebutton function) and RedScore (in the redbutton function) a value (probably 0) just before your loops start to manipulate them in each of those functions.
If you do this in the same place you declare your other variables, then they will only be 0 the fist time they are used and it looks like you need them to start at 0 every time you use them. BTW this also means you do not need to declare them as Global variables, which is usually worth trying to avoid if you can.
If you need to bring in values for these variables from where they have been used elsewhere, it is probably worth bringing them in as arguments in the function calls.