basics of LED pin wiggling, button input using the Gertboard button paradigm,
and including a serial comm interface for monitoring the process; as well,
giving the AVR commands (which affect the AVR coding!)
In the pic above, I am powering the Gertboard from the PI 5v (pin2) and ground
(pin39); by piggy-backing on the 5v fan pin connectors.
The AVR JTAG connectors are connected to the PI SPI pins at the center of the
GPIO port (GP8, GP9, GP10, GP11).
I'm running the AVR outputs (13, 12, 11, 10) off-board because I ran out of
FF connectors! If I'd had enough connectors I might have used the Gertboard
LEDs...
Gertboard buttons 1 and 2 are on the red and yellow cable at the center of the
board and connect to AVR pins 2 and 3 respectively. I have the output jumpers
attached at channel 1 and 2 (optionally) so I get a visual indicator on my
button presses (for debugging).
The green and purple cable at the center of the board is the serial cable running
to the PI GPIO14 and GPIO15 pins, to allow serial comm interface with the AVR.
The following code is my counter.ino sketch coding just for fun
(and if anyone else wants to screw around with it that's good too).
Code: Select all
/*
Counter.ino
4 bit binary counter with serial interface
Mark H. Harris
5-29-2016
v0.02j
*/
int NOCHAR= -1;
int ESC= 27;
int False=0;
int True=1;
int count=0; // AVR cycle counter
int button1=2; // resets cycle counter
int button2=3; // reverses high-order low-order LED counter
int button3=4; // sets AVR in sleep mode
char keyin; // serial command character input
int pins[4] = {13, 12, 11, 10};
int REV[4] = {1, 2, 4, 8}; // pin 10 high-order
int FWD[4] = {8, 4, 2, 1}; // pin 13 high-order
int* FWDREV = FWD; // bin_display() digits
int swap_flag = False; // high-order low-order reversed
int button1_flag=False; // count reset request pending
int button2_flag=False; // swap request pending
int button3_flag=False; // sleep mode request pending
int button4_flag=False; // wiggle mode request pending
int button5_flag=False; // flash mode request pending
int button6_flag=False; // scanner mode request pending
int button7_flag=False; // strobe mode request pending
int button8_flag=False; // license display request pending
int button9_flag=False; // help commands menu request pending
void setup() {
int i = 0;
for (i=0; i<=3; i++) {
pinMode(pins[i], OUTPUT);
}
pinMode(button1, INPUT);
pinMode(button2, INPUT);
Serial.begin(9600);
digitalWrite(button1, HIGH); // button pullup
digitalWrite(button2, HIGH); // button pullup
digitalWrite(button3, HIGH); // button pullup
}
void loop() {
int i=0;
int j=0;
button1_flag=False;
button2_flag=False;
for (i=0; i<=15; i++) {
bin_display(i, pins, FWDREV); // LED counter display
keyin=Serial.read(); // request commands serially
if (keyin=='h') { // page 1 help commands menu
keyin=help_menu(pins, FWDREV);
button9_flag=True;
}
if (keyin=='h') { // page 2 help commands menu
keyin=help_menu_2(pins, FWDREV);
button9_flag=True;
}
if (keyin=='l') {
keyin=dsp_license(pins, FWDREV);
button8_flag=True;
}
if (!digitalRead(button1) && !button1_flag) {
Serial.println("AVR cycle Counter reset button");
button1_flag=True;
}
if (!digitalRead(button2) && !button2_flag) {
Serial.println("AVR orientation swap button");
button2_flag=True;
}
if (keyin=='q' || keyin=='Q') {
Serial.println("AVR sleep_mode request");
button3_flag=True;
}
if (keyin=='w' || keyin=='W') {
Serial.println("AVR wiggle mode request");
button4_flag=True;
}
if (keyin=='f' || keyin=='F') {
Serial.println("AVR flash mode request");
button5_flag=True;
}
if (keyin=='z' || keyin=='Z') {
Serial.println("AVR scanner mode request");
button6_flag=True;
}
if (keyin=='b' || keyin=='B') {
Serial.println("AVR strobe mode request");
button7_flag=True;
}
delay(198);
if (button8_flag || button9_flag)
break;
}
bin_display(0x0, pins, FWDREV);
delay(770);
for (i=0; i<=7; i++) {
keyin=Serial.read(); // process pending requests
if (button3_flag) {
sleep_mode(pins);
button3_flag=False;
}
if (button4_flag) {
wiggle(pins, FWDREV);
button4_flag=False;
}
if (button5_flag) {
flash(pins, FWDREV);
button5_flag=False;
}
if (button6_flag) {
scanner(pins, FWDREV);
button6_flag=False;
}
if (button7_flag) {
strobe(pins, FWDREV);
button7_flag=False;
}
bin_display(0xa, pins, FWDREV);
delay(128);
bin_display(0x5, pins, FWDREV);
delay(128);
if (keyin=='r' || button1_flag) {
Serial.println("AVR cycle counter reset command");
count=0;
button1_flag=False;
}
if (keyin=='s' || button2_flag) {
Serial.println("AVR binary orientation swap command");
button2_flag=False;
if (swap_flag) {
FWDREV = FWD;
swap_flag = False;
}
else {
FWDREV = REV;
swap_flag = True;
}
if (FWDREV==FWD) {
for (j=0; j<=3; j++) {
Serial.print(pins[j]);
Serial.print(", ");
}
Serial.println();
}
else {
for (j=3; j>=0; j--) {
Serial.print(pins[j]);
Serial.print(", ");
}
Serial.println();
}
}
}
count+=1;
Serial.print("AVR cycle counter ");
Serial.println(count);
button1_flag=False;
button2_flag=False;
button8_flag=False;
button9_flag=False;
if (!digitalRead(button3))
sleep_mode(pins);
}
// binary LED display function
void bin_display(int val, int pins[], int digits[]) {
int n=0;
for (n=0; n<=3; n++) {
if (val & digits[n])
digitalWrite(pins[n], HIGH);
else
digitalWrite(pins[n], LOW);
}
}
void sleep_mode(int pins[]) {
int keyin;
int i;
for (i=0; i<=3; i++) {
digitalWrite(pins[i], LOW);
}
while (True) {
for (i=7; i<=245; i++) {
analogWrite(10, i);
delay(4);
}
keyin = Serial.read();
if (keyin=='x' || keyin==ESC)
break;
if (!digitalRead(button1))
wiggle(pins, FWD);
if (!digitalRead(button2))
scanner(pins, FWD);
for (i=245; i>=7; i--) {
analogWrite(10, i);
delay(4);
}
keyin = Serial.read();
if (keyin=='x' || keyin==ESC || !digitalRead(button3))
break;
delay(70);
if (!digitalRead(button1))
flash(pins, FWD);
if (!digitalRead(button2))
strobe(pins, FWD);
}
}
void wiggle(int pins[], int digits[]) {
int keyin;
while (True) {
bin_display(0xc, pins, digits);
delay(127);
bin_display(0x6, pins, digits);
delay(127);
bin_display(0x3, pins, digits);
delay(127);
bin_display(0x6, pins, digits);
delay(120);
keyin = Serial.read();
if (keyin=='x' || keyin==ESC || !digitalRead(button1))
break;
}
bin_display(0x0, pins, digits);
}
void flash(int pins[], int digits[]) {
int keyin;
while (True) {
bin_display(0xf, pins, digits);
delay(256);
bin_display(0x0, pins, digits);
delay(250);
keyin = Serial.read();
if (keyin=='x' || keyin==ESC || !digitalRead(button1))
break;
}
bin_display(0x0, pins, digits);
}
void scanner(int pins[], int digits[]) {
int keyin;
int t_delay=128;
while (True) {
bin_display(0x8, pins, digits);
delay(t_delay);
bin_display(0x4, pins, digits);
delay(t_delay);
bin_display(0x2, pins, digits);
delay(t_delay);
bin_display(0x1, pins, digits);
delay(t_delay);
bin_display(0x2, pins, digits);
delay(t_delay);
bin_display(0x4, pins, digits);
delay(t_delay-7);
keyin = Serial.read();
if (keyin=='x' || keyin==ESC || !digitalRead(button1))
break;
}
bin_display(0x0, pins, digits);
}
void strobe(int pins[], int digits[]) {
int keyin;
while (True) {
bin_display(0x8, pins, digits);
delay(47);
bin_display(0x4, pins, digits);
delay(47);
bin_display(0x2, pins, digits);
delay(47);
bin_display(0x1, pins, digits);
delay(47);
bin_display(0x0, pins, digits);
delay(27);
keyin = Serial.read();
if (keyin=='x' || keyin==ESC || !digitalRead(button1))
break;
}
bin_display(0x0, pins, digits);
}
int help_menu(int pins[], int digits[]) {
int i;
char keyin;
bin_display(0x0, pins, digits);
for (i=0; i<=24; i++) {
Serial.println();
}
Serial.println(" ***********************************************");
Serial.println(" ** Commands Help Menu **");
Serial.println(" ***********************************************");
Serial.println(" ** 'b' -- strobe **");
Serial.println(" ** 'w' -- wiggle **");
Serial.println(" ** 'z' -- scanner **");
Serial.println(" ** 'f' -- flasher **");
Serial.println(" ** 'h' -- (help menu page 2) **");
Serial.println(" ** 'l' -- license **");
Serial.println(" ** 'q' -- quiescense sleep **");
Serial.println(" ** 'x' -- exit to counter **");
Serial.println(" ***********************************************");
for (i=0; i<=7; i++) {
Serial.println();
}
while (True) {
if (int(keyin=Serial.read())!= NOCHAR)
break;
delay(256);
}
for (i=0; i<=24; i++) {
Serial.println();
}
return keyin;
}
int help_menu_2(int pins[], int digits[]) {
int i;
char keyin;
bin_display(0x0, pins, digits);
for (i=0; i<=24; i++) {
Serial.println();
}
Serial.println(" ***********************************************");
Serial.println(" ** Commands Help Information **");
Serial.println(" ***********************************************");
Serial.println(" ** (2) LED Flasher Command Mode **");
Serial.println(" ** **");
Serial.println(" ** 'r' -- AVR cycle counter reset **");
Serial.println(" ** 's' -- AVR binary high-low order swap **");
Serial.println(" ** **");
Serial.println(" ** **");
Serial.println(" ** (press any key) **");
Serial.println(" ** **");
Serial.println(" ***********************************************");
for (i=0; i<=7; i++) {
Serial.println();
}
while (True) {
if (int(keyin=Serial.read())!= NOCHAR)
break;
delay(256);
}
for (i=0; i<=24; i++) {
Serial.println();
}
return keyin;
}
int dsp_license(int pins[], int digits[]) {
int i;
char keyin;
bin_display(0x0, pins, digits);
for (i=0; i<=24; i++) {
Serial.println();
}
Serial.println(" ***********************************************");
Serial.println(" ** Counter License **");
Serial.println(" ***********************************************");
Serial.println(" ** **");
Serial.println(" ** Mark H. Harris **");
Serial.println(" ** v.02j **");
Serial.println(" ** 05-28-2016 **");
Serial.println(" ** **");
Serial.println(" ** GPLv3 licesne **");
Serial.println(" ** **");
Serial.println(" ** (press any key) **");
Serial.println(" ** **");
Serial.println(" ***********************************************");
for (i=0; i<=7; i++) {
Serial.println();
}
while (True) {
if (int(keyin=Serial.read())!= NOCHAR)
break;
delay(256);
}
for (i=0; i<=24; i++) {
Serial.println();
}
return keyin;
}
from 0x0-0xf (the high-low order is selected with serial console command or
button press); and 2) the four LEDs will wiggle during which time
the AVR is in serial command mode.
Reset can be selected during counter mode while the LEDs are in binary
succession with button 1. Button 2 will reverse the high-low order blinking
of the binary LED display.
During binary LED display counting the following serial commands are accepted:
'h' keypress -- 'hello, world'
'r' keypress -- AVR cycle counter reset request
'l' keypress -- 'LED counter = <current counter>'
's' keypress -- AVR LED counter high-low order swap request
WARNING: For what its worth, while this was running I popped the J7 3v3
jumper off the Gertboard to make the input switch connections. Doing so
created some kind or surge on the 5v current and the poly fuse on the
PI tripped! The red light went out, and although the PI did not crash
(unbelievably) the 5v dropped to 4.18 volts. This returned to normal
the following day, after the poly fuse healed.
NOTE: This board is fun to play with; and I think highly useful. I am indeed
thankful to Gert van Loo for developement of the board, and for Gordon @ Drogon
for the software updates to make the Arduino IDE work with the PI and the
Gertboard (very nice piece of engineering, to both you guys-- you rock!)