...and the remaining 1/4:
//This function uses that fact that each bit in a byte
//is 2 times greater than the one before it to
//shift the bits higher
void lightShiftPinB(int p) {
//start with the pin = 1 so that if 0 is passed to this
//function pin 0 will light.
pin = 1;
for (int x = 0; x < p; x++) {
pin = pin * 2;
}
//move 'em out
shiftOut(dataPin, clockPin, pin);
}
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//for each bit in the byte myDataOut?
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}
//blinks both registers based on the number of times you want to
//blink "n" and the pause between them "d"
//starts with a moment of darkness to make sure the first blink
//has its full visual effect.
void blinkAll_2Bytes(int n, int d) {
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
digitalWrite(latchPin, 1);
delay(200);
for (int x = 0; x < n; x++) {
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 255);
shiftOut(dataPin, clockPin, 255);
digitalWrite(latchPin, 1);
delay(d);
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
digitalWrite(latchPin, 1);
delay(d);
}
}
void Clear() {
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
digitalWrite(latchPin, 1);
}
void Win() {
}
void DIAGNOSTICS() {
for(i = 0; i < 15; i++) {
digitalWrite(13, HIGH);
delay(50);
digitalWrite(13, LOW);
delay(50);
}
delay(100);
blinkAll_2Bytes(2,500);
delay(500);
digitalWrite(rdPin, HIGH);
delay(1000);
digitalWrite(rdPin, LOW);
delay(1000);
digitalWrite(gnPin, HIGH);
delay(1000);
digitalWrite(gnPin, LOW);
delay(100);
digitalWrite(blPin, HIGH);
delay(100);
digitalWrite(blPin, LOW);
delay(100);
End();
}
And here's the BASIC Stamp II code:
' {$STAMP BS2e}
' {$PBASIC 2.5}
OK PIN 0
Pzo PIN 7
LCD PIN 8
READY PIN 13
ATX PIN 15
' -----[LCD Commands]----------------------
' CMD followed by:
' -128 - 143 (Line 1), 144 - 159 (Line 2) = Set Cursor Position
' -ASCII letters 'K'-'P' = Set baud to 2.4K, 4.8K, 9.6K, 14.4K, 19.2K, and 38.4K bps respectively
CMD CON $FE ' COMMAND Instruction
CLLCD CON $01 ' Clear LCD
CURR CON $14 ' Cursor Right
CURL CON $10 ' Cursor Left
SCR CON $1C ' Scroll Right
SCL CON $18 ' Scroll Left
DON CON $0C ' Display ON
DOFF CON $08 ' Display OFF
BBRT CON $7C ' Backlight Brightness (PWM)
BTURBO CON 6
TMOUT CON 10000 ' Serial timeout
B CON $54 ' 9600 Bps
Yes CON 1
No CON 0
i VAR Word
id VAR Word
datta VAR Word
p VAR Word
LOW 9
LOW 10
LOW 11
'OUTPUT READY
INPUT READY
LOW READY
IF IN6 = 0 THEN
HIGH 11
SERIN ATX\READY,BTURBO,TMOUT,Serial_Error,[id] ' OK is LOW when ready for data
GOSUB DIAGNOSTICS
ENDIF
SEROUT 8, B, [CMD, CLLCD]
PAUSE 4000
SEROUT 8, B, [CMD, 131, "Welcome to"]
PAUSE 1000
SEROUT 8, B, [CMD, CLLCD]
PAUSE 100
SEROUT 8, B, [CMD, 131, "Lightning Reflex!!"]
PAUSE 2000
FOR i = 1 TO 14
SEROUT 8, B, [CMD, SCL]
PAUSE 100
NEXT
Main:
DO
DEBUG CR, "Main"
GOSUB Get_Data
GOSUB Determine_Operation
LOOP
Get_Data:
DEBUG CR,"Getting data..."
SERIN ATX\READY,BTURBO,TMOUT,Serial_Error,[id] ' OK is LOW when ready for data
PAUSE 1
SERIN ATX\READY,BTURBO,TMOUT,Serial_Error,[datta] ' OK is LOW when ready for data
'DEBUG CR,id, CR, DEC datta
RETURN
Serial_Error:
DEBUG CR,"Serial timeout."
GOTO Main
Determine_Operation:
DEBUG CR, "Determine Operation"
SELECT id
CASE "B":
DEBUG CR, "Buttons"
GOSUB Read_Buttons
ENDSELECT
RETURN
Read_Buttons:
DEBUG CR, "Reading Buttons"
IF IN6 = 0 THEN
HIGH 11 ' Select Button
DO
PAUSE 1
LOOP WHILE READY = 0
LOW 11
GOTO Main
ENDIF
IF IN5 = 0 THEN ' Left Button
HIGH 9
DO
PAUSE 1
LOOP WHILE READY = 0
LOW 9
GOTO Main
ENDIF
IF IN4 = 0 THEN ' Right Button
HIGH 10
DO
PAUSE 1
LOOP WHILE READY = 0
LOW 10
GOTO Main
ENDIF
GOTO Read_Buttons
DIAGNOSTICS:
PAUSE 2000
DEBUG CR, "DIAGNOSTICS MODE"
SEROUT 8, B, [CMD, CLLCD]
SEROUT 8, B, ["DIAGNOSTICS MODE"]
END