Skip to content

Instantly share code, notes, and snippets.

@TRNT7
Created January 26, 2018 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TRNT7/fd664f49eb11472acbf13c6d043115ad to your computer and use it in GitHub Desktop.
Save TRNT7/fd664f49eb11472acbf13c6d043115ad to your computer and use it in GitHub Desktop.
/*
Code Development Team
DIGF 2005 - Atelier II
Professor Nick Puckett
Tuesday January 23, 2018
NANO COMMUNICATION CODE
This code allows Nanos to read information from the serial port sent
from the control panel, and display corresponding images/animations on
the OLEDs.
THE CIRCUIT
RX is digital pin 8 (connect to TX of other device)
TX is digital pin 9 (connect to RX of other device)
SDA connected to A4
SCL connected to A5
GND connected to GND
VCC connected to VTN
REFERENCES:
https://www.arduino.cc/en/Tutorial/SoftwareSerialExample
https://www.arduino.cc/reference/en/language/variables/data-types/bool/
http://forum.arduino.cc/index.php?topic=42603.0
*/
// include necessary libraries
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include <SoftwareSerial.h> // * NEW
SoftwareSerial mySerial(8, 9); // RX, TX // * NEW
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
//// GLOBAL VARIABLES
const int framesPerSecond = 3;
int incoming;
// *** PICK THE ENVIRONMENT YOUR CREATURE LIVES IN ***
// 1 = Desert, 2 = Forest, 3 = Water
int environment = 1;
int petStatus = 1; // 0=HAPPY, 1=SAD
////
////////// HERE ARE WHERE THE INSTRUCTIONS FOR ANIMATION FRAMES GO
//NOTE: screen dimensions: 128x64
//NOTE: use the u8g2 library to write instructions for drawing images (for example using the shape and line functions)
//NOTE: only add your desired drawing functions, buffering/clearing/timing is handled for you :)
// https://github.com/olikraus/u8g2/wiki/u8g2reference#drawbox
//
void happyFrame1(void) { //THE FIRST FRAME OF THE 'HAPPY' ANIMATION
u8g2.drawTriangle(46,4,46,16,55,16); // left ear
u8g2.drawTriangle(81,4,73,16,82,16); // right ear
u8g2.drawFrame(46,15,36,33); //face
u8g2.drawFilledEllipse(56.5, 25.5, 2, 2, U8G2_DRAW_ALL); // left eye
u8g2.drawFilledEllipse(71.5, 25.5, 2, 2, U8G2_DRAW_ALL); // right eye
u8g2.drawTriangle(61.5,34, 67.5,34, 64.5, 36.67); // nose
u8g2.drawLine(64.5,36.5,64.5,40.5); // middle nose
u8g2.drawLine(59.5,35.5,52.5,33.5); // left top wisk
u8g2.drawLine(59.5,37.5,52.5,37.5); //left bottom wisk
u8g2.drawLine(69.5,35.5,76.5,33.5); // right top wisk
u8g2.drawLine(69.5,37.5,76.5,37.5); // right bottom wisk
u8g2.drawLine(59.5,40.5,62,43); // mouth 1
u8g2.drawLine(64.5,40.5,62,43); // mouth 2
u8g2.drawLine(64.5,40.5,67,43); // mouth 3
u8g2.drawLine(69.5,40.5,67,43); // mouth 4
}
void happyFrame2(void) { //THE SECOND FRAME OF THE 'HAPPY' ANIMATION
u8g2.drawTriangle(46,4,46,16,55,16); // left ear
u8g2.drawTriangle(81,4,73,16,82,16); // right ear
u8g2.drawFrame(46,15,36,33); //face
u8g2.drawFilledEllipse(56.5, 25.5, 2, 2, U8G2_DRAW_ALL); // left eye
u8g2.drawFilledEllipse(71.5, 25.5, 2, 2, U8G2_DRAW_ALL); // right eye
u8g2.drawTriangle(61.5,33, 67.5,34, 64.5, 36.67); // nose
u8g2.drawLine(64.5,35.5,64.5,39.5); // middle nose
u8g2.drawLine(59.5,35.5,52.5,31.5); // left top wisk
u8g2.drawLine(59.5,37.5,52.5,36.5); //left bottom wisk
u8g2.drawLine(69.5,35.5,76.5,31.5); // right top wisk
u8g2.drawLine(76.5,36.5,69.5,37.5); // right bottom wisk
u8g2.drawLine(59.5,39.5,62,42); // mouth 1
u8g2.drawLine(64.5,39.5,62,42); // mouth 2
u8g2.drawLine(64.5,39.5,67,42); // mouth 3
u8g2.drawLine(69.5,39.5,67,42); // mouth 4
}
void happyFrame3(void) { //THE THIRD FRAME OF THE 'HAPPY' ANIMATION
u8g2.drawTriangle(46,4,46,16,55,16); // left ear
u8g2.drawTriangle(81,4,73,16,82,16); // right ear
u8g2.drawFrame(46,15,36,33); //face
u8g2.drawFilledEllipse(56.5, 25.5, 2, 2, U8G2_DRAW_ALL); // left eye
u8g2.drawFilledEllipse(71.5, 25.5, 2, 2, U8G2_DRAW_ALL); // right eye
u8g2.drawTriangle(61.5,34, 67.5,34, 64.5, 36.67); // nose
u8g2.drawLine(64.5,36.5,64.5,40.5); // middle nose
u8g2.drawLine(59.5,35.5,52.5,33.5); // left top wisk
u8g2.drawLine(59.5,37.5,52.5,37.5); //left bottom wisk
u8g2.drawLine(69.5,35.5,76.5,33.5); // right top wisk
u8g2.drawLine(69.5,37.5,76.5,37.5); // right bottom wisk
u8g2.drawLine(59.5,40.5,62,43); // mouth 1
u8g2.drawLine(64.5,40.5,62,43); // mouth 2
u8g2.drawLine(64.5,40.5,67,43); // mouth 3
u8g2.drawLine(69.5,40.5,67,43); // mouth 4
}
void sadFrame1(void) { //THE FIRST FRAME OF THE 'SAD' ANIMATION
u8g2.drawTriangle(46,4,46,16,55,16); // left ear
u8g2.drawTriangle(81,4,73,16,82,16); // right ear
u8g2.drawFrame(46,15,36,33); //face
u8g2.drawFilledEllipse(56.5, 25.5, 2, 2, U8G2_DRAW_ALL); // left eye
u8g2.drawFilledEllipse(71.5, 25.5, 2, 2, U8G2_DRAW_ALL); // right eye
u8g2.drawTriangle(61.5,34, 67.5,34, 64.5, 36.67); // nose
u8g2.drawLine(64.5,36.5,64.5,40.5); // middle nose
u8g2.drawLine(59.5,35.5,52.5,33.5); // left top wisk
u8g2.drawLine(59.5,37.5,52.5,37.5); //left bottom wisk
u8g2.drawLine(69.5,35.5,76.5,33.5); // right top wisk
u8g2.drawLine(69.5,37.5,76.5,37.5); // right bottom wisk
u8g2.drawLine(59.5,40.5,62,43); // mouth 1
u8g2.drawLine(64.5,40.5,62,43); // mouth 2
u8g2.drawLine(64.5,40.5,67,43); // mouth 3
u8g2.drawLine(69.5,40.5,67,43); // mouth 4
}
void sadFrame2(void) { //THE SECOND FRAME OF THE 'SAD' ANIMATION
u8g2.drawTriangle(46,4,46,16,55,16); // left ear
u8g2.drawTriangle(81,4,73,16,82,16); // right ear
u8g2.drawFrame(46,15,36,33); //face
u8g2.drawLine(59.5,22.5,52.5,24.5); // low brow left
u8g2.drawLine(69.5,22.5,76.5,24.5); // low brow right
u8g2.drawFilledEllipse(56.5, 25.5, 1, 1, U8G2_DRAW_ALL); // left eye
u8g2.drawFilledEllipse(71.5, 25.5, 1, 1, U8G2_DRAW_ALL); // right eye
u8g2.drawTriangle(61.5,34, 67.5,34, 64.5, 36.67); // nose
u8g2.drawLine(64.5,36.5,64.5,40.5); // middle nose
u8g2.drawLine(59.5,35.5,52.5,35.5); // left top wisk
u8g2.drawLine(59.5,37.5,52.5,39.5); //left bottom wisk
u8g2.drawLine(69.5,35.5,76.5,35.5); // right top wisk
u8g2.drawLine(69.5,37.5,76.5,39.5); // right bottom wisk
u8g2.drawLine(58.5,41.5,62,43); // mouth 1
u8g2.drawLine(64.5,40.5,62,43); // mouth 2
u8g2.drawLine(64.5,40.5,67,43); // mouth 3
u8g2.drawLine(70.5,41.5,67,43); // mouth 4
}
void sadFrame3(void) { //THE THIRD FRAME OF THE 'SAD' ANIMATION
u8g2.drawTriangle(46,4,46,16,55,16); // left ear
u8g2.drawTriangle(81,4,73,16,82,16); // right ear
u8g2.drawFrame(46,15,36,33); //face
u8g2.drawFilledEllipse(56.5, 25.5, 2, 2, U8G2_DRAW_ALL); // left eye
u8g2.drawFilledEllipse(71.5, 25.5, 2, 2, U8G2_DRAW_ALL); // right eye
u8g2.drawTriangle(61.5,34, 67.5,34, 64.5, 36.67); // nose
u8g2.drawLine(64.5,36.5,64.5,40.5); // middle nose
u8g2.drawLine(59.5,35.5,52.5,33.5); // left top wisk
u8g2.drawLine(59.5,37.5,52.5,37.5); //left bottom wisk
u8g2.drawLine(69.5,35.5,76.5,33.5); // right top wisk
u8g2.drawLine(69.5,37.5,76.5,37.5); // right bottom wisk
u8g2.drawLine(59.5,40.5,62,43); // mouth 1
u8g2.drawLine(64.5,40.5,62,43); // mouth 2
u8g2.drawLine(64.5,40.5,67,43); // mouth 3
u8g2.drawLine(69.5,40.5,67,43); // mouth 4
}
//////////END OF ANIMATION FRAME INSTRUCTIONS
void setup(void) {
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB14_tr);
// set pins for OLED
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
u8g2.begin(4, 5, 6);
mySerial.begin(4800);
mySerial.flush();
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop(void) {
// Determines pet status before animations are executed
if (mySerial.available()) {
incoming = mySerial.read();
Serial.println(incoming);
if (incoming == environment) {
petStatus = 0;
} else {
petStatus = 1;
}
}
animateScreen();
}
//// YOU DO NOT HAVE TO MODIFY THE REST OF THE CODE:
void animateScreen() {
// the pet's status is checked every time animateScreen() is called
if (petStatus == 0) { //IF PET IS HAPPY, DO THIS CODE:
u8g2.clearBuffer();
happyFrame1();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
u8g2.clearBuffer();
happyFrame2();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
u8g2.clearBuffer();
happyFrame3();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
u8g2.clearBuffer();
happyFrame2();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
}
else { //IF PET IS NOT HAPPY, IT'S SAD. DO THIS CODE:
u8g2.clearBuffer();
sadFrame1();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
u8g2.clearBuffer();
sadFrame2();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
u8g2.clearBuffer();
sadFrame3();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
u8g2.clearBuffer();
sadFrame2();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
}
}
/*
Code Development Team
DIGF 2005 - Atelier II
Professor Nick Puckett
Tuesday January 23, 2018
NANO COMMUNICATION CODE
This code allows Nanos to read information from the serial port sent
from the control panel, and display corresponding images/animations on
the OLEDs.
THE CIRCUIT
RX is digital pin 8 (connect to TX of other device)
TX is digital pin 9 (connect to RX of other device)
SDA connected to A4
SCL connected to A5
GND connected to GND
VCC connected to VTN
REFERENCES:
https://www.arduino.cc/en/Tutorial/SoftwareSerialExample
https://www.arduino.cc/reference/en/language/variables/data-types/bool/
http://forum.arduino.cc/index.php?topic=42603.0
*/
// include necessary libraries
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include <SoftwareSerial.h> // * NEW
SoftwareSerial mySerial(8, 9); // RX, TX // * NEW
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
//// GLOBAL VARIABLES
const int framesPerSecond = 3;
int incoming;
// *** PICK THE ENVIRONMENT YOUR CREATURE LIVES IN ***
// 1 = Desert, 2 = Forest, 3 = Water
int environment = 1;
int petStatus = 1; // 0=HAPPY, 1=SAD
////
////////// HERE ARE WHERE THE INSTRUCTIONS FOR ANIMATION FRAMES GO
//NOTE: screen dimensions: 128x64
//NOTE: use the u8g2 library to write instructions for drawing images (for example using the shape and line functions)
//NOTE: only add your desired drawing functions, buffering/clearing/timing is handled for you :)
// https://github.com/olikraus/u8g2/wiki/u8g2reference#drawbox
//
void happyFrame1(void) { //THE FIRST FRAME OF THE 'HAPPY' ANIMATION
u8g2.drawTriangle(46,4,46,16,55,16); // left ear
u8g2.drawTriangle(81,4,73,16,82,16); // right ear
u8g2.drawFrame(46,15,36,33); //face
u8g2.drawFilledEllipse(56.5, 25.5, 2, 2, U8G2_DRAW_ALL); // left eye
u8g2.drawFilledEllipse(71.5, 25.5, 2, 2, U8G2_DRAW_ALL); // right eye
u8g2.drawTriangle(61.5,34, 67.5,34, 64.5, 36.67); // nose
u8g2.drawLine(64.5,36.5,64.5,40.5); // middle nose
u8g2.drawLine(59.5,35.5,52.5,33.5); // left top wisk
u8g2.drawLine(59.5,37.5,52.5,37.5); //left bottom wisk
u8g2.drawLine(69.5,35.5,76.5,33.5); // right top wisk
u8g2.drawLine(69.5,37.5,76.5,37.5); // right bottom wisk
u8g2.drawLine(59.5,40.5,62,43); // mouth 1
u8g2.drawLine(64.5,40.5,62,43); // mouth 2
u8g2.drawLine(64.5,40.5,67,43); // mouth 3
u8g2.drawLine(69.5,40.5,67,43); // mouth 4
}
void happyFrame2(void) { //THE SECOND FRAME OF THE 'HAPPY' ANIMATION
u8g2.drawTriangle(46,4,46,16,55,16); // left ear
u8g2.drawTriangle(81,4,73,16,82,16); // right ear
u8g2.drawFrame(46,15,36,33); //face
u8g2.drawFilledEllipse(56.5, 25.5, 2, 2, U8G2_DRAW_ALL); // left eye
u8g2.drawFilledEllipse(71.5, 25.5, 2, 2, U8G2_DRAW_ALL); // right eye
u8g2.drawTriangle(61.5,33, 67.5,34, 64.5, 36.67); // nose
u8g2.drawLine(64.5,35.5,64.5,39.5); // middle nose
u8g2.drawLine(59.5,35.5,52.5,31.5); // left top wisk
u8g2.drawLine(59.5,37.5,52.5,36.5); //left bottom wisk
u8g2.drawLine(69.5,35.5,76.5,31.5); // right top wisk
u8g2.drawLine(76.5,36.5,69.5,37.5); // right bottom wisk
u8g2.drawLine(59.5,39.5,62,42); // mouth 1
u8g2.drawLine(64.5,39.5,62,42); // mouth 2
u8g2.drawLine(64.5,39.5,67,42); // mouth 3
u8g2.drawLine(69.5,39.5,67,42); // mouth 4
}
void happyFrame3(void) { //THE THIRD FRAME OF THE 'HAPPY' ANIMATION
u8g2.drawTriangle(46,4,46,16,55,16); // left ear
u8g2.drawTriangle(81,4,73,16,82,16); // right ear
u8g2.drawFrame(46,15,36,33); //face
u8g2.drawFilledEllipse(56.5, 25.5, 2, 2, U8G2_DRAW_ALL); // left eye
u8g2.drawFilledEllipse(71.5, 25.5, 2, 2, U8G2_DRAW_ALL); // right eye
u8g2.drawTriangle(61.5,34, 67.5,34, 64.5, 36.67); // nose
u8g2.drawLine(64.5,36.5,64.5,40.5); // middle nose
u8g2.drawLine(59.5,35.5,52.5,33.5); // left top wisk
u8g2.drawLine(59.5,37.5,52.5,37.5); //left bottom wisk
u8g2.drawLine(69.5,35.5,76.5,33.5); // right top wisk
u8g2.drawLine(69.5,37.5,76.5,37.5); // right bottom wisk
u8g2.drawLine(59.5,40.5,62,43); // mouth 1
u8g2.drawLine(64.5,40.5,62,43); // mouth 2
u8g2.drawLine(64.5,40.5,67,43); // mouth 3
u8g2.drawLine(69.5,40.5,67,43); // mouth 4
}
void sadFrame1(void) { //THE FIRST FRAME OF THE 'SAD' ANIMATION
u8g2.drawTriangle(46,4,46,16,55,16); // left ear
u8g2.drawTriangle(81,4,73,16,82,16); // right ear
u8g2.drawFrame(46,15,36,33); //face
u8g2.drawFilledEllipse(56.5, 25.5, 2, 2, U8G2_DRAW_ALL); // left eye
u8g2.drawFilledEllipse(71.5, 25.5, 2, 2, U8G2_DRAW_ALL); // right eye
u8g2.drawTriangle(61.5,34, 67.5,34, 64.5, 36.67); // nose
u8g2.drawLine(64.5,36.5,64.5,40.5); // middle nose
u8g2.drawLine(59.5,35.5,52.5,33.5); // left top wisk
u8g2.drawLine(59.5,37.5,52.5,37.5); //left bottom wisk
u8g2.drawLine(69.5,35.5,76.5,33.5); // right top wisk
u8g2.drawLine(69.5,37.5,76.5,37.5); // right bottom wisk
u8g2.drawLine(59.5,40.5,62,43); // mouth 1
u8g2.drawLine(64.5,40.5,62,43); // mouth 2
u8g2.drawLine(64.5,40.5,67,43); // mouth 3
u8g2.drawLine(69.5,40.5,67,43); // mouth 4
}
void sadFrame2(void) { //THE SECOND FRAME OF THE 'SAD' ANIMATION
u8g2.drawTriangle(46,4,46,16,55,16); // left ear
u8g2.drawTriangle(81,4,73,16,82,16); // right ear
u8g2.drawFrame(46,15,36,33); //face
u8g2.drawLine(59.5,22.5,52.5,24.5); // low brow left
u8g2.drawLine(69.5,22.5,76.5,24.5); // low brow right
u8g2.drawFilledEllipse(56.5, 25.5, 1, 1, U8G2_DRAW_ALL); // left eye
u8g2.drawFilledEllipse(71.5, 25.5, 1, 1, U8G2_DRAW_ALL); // right eye
u8g2.drawTriangle(61.5,34, 67.5,34, 64.5, 36.67); // nose
u8g2.drawLine(64.5,36.5,64.5,40.5); // middle nose
u8g2.drawLine(59.5,35.5,52.5,35.5); // left top wisk
u8g2.drawLine(59.5,37.5,52.5,39.5); //left bottom wisk
u8g2.drawLine(69.5,35.5,76.5,35.5); // right top wisk
u8g2.drawLine(69.5,37.5,76.5,39.5); // right bottom wisk
u8g2.drawLine(58.5,41.5,62,43); // mouth 1
u8g2.drawLine(64.5,40.5,62,43); // mouth 2
u8g2.drawLine(64.5,40.5,67,43); // mouth 3
u8g2.drawLine(70.5,41.5,67,43); // mouth 4
}
void sadFrame3(void) { //THE THIRD FRAME OF THE 'SAD' ANIMATION
u8g2.drawTriangle(46,4,46,16,55,16); // left ear
u8g2.drawTriangle(81,4,73,16,82,16); // right ear
u8g2.drawFrame(46,15,36,33); //face
u8g2.drawFilledEllipse(56.5, 25.5, 2, 2, U8G2_DRAW_ALL); // left eye
u8g2.drawFilledEllipse(71.5, 25.5, 2, 2, U8G2_DRAW_ALL); // right eye
u8g2.drawTriangle(61.5,34, 67.5,34, 64.5, 36.67); // nose
u8g2.drawLine(64.5,36.5,64.5,40.5); // middle nose
u8g2.drawLine(59.5,35.5,52.5,33.5); // left top wisk
u8g2.drawLine(59.5,37.5,52.5,37.5); //left bottom wisk
u8g2.drawLine(69.5,35.5,76.5,33.5); // right top wisk
u8g2.drawLine(69.5,37.5,76.5,37.5); // right bottom wisk
u8g2.drawLine(59.5,40.5,62,43); // mouth 1
u8g2.drawLine(64.5,40.5,62,43); // mouth 2
u8g2.drawLine(64.5,40.5,67,43); // mouth 3
u8g2.drawLine(69.5,40.5,67,43); // mouth 4
}
//////////END OF ANIMATION FRAME INSTRUCTIONS
void setup(void) {
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB14_tr);
// set pins for OLED
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
u8g2.begin(4, 5, 6);
mySerial.begin(4800);
mySerial.flush();
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop(void) {
// Determines pet status before animations are executed
if (mySerial.available()) {
incoming = mySerial.read();
Serial.println(incoming);
if (incoming == environment) {
petStatus = 0;
} else {
petStatus = 1;
}
}
animateScreen();
}
//// YOU DO NOT HAVE TO MODIFY THE REST OF THE CODE:
void animateScreen() {
// the pet's status is checked every time animateScreen() is called
if (petStatus == 0) { //IF PET IS HAPPY, DO THIS CODE:
u8g2.clearBuffer();
happyFrame1();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
u8g2.clearBuffer();
happyFrame2();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
u8g2.clearBuffer();
happyFrame3();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
u8g2.clearBuffer();
happyFrame2();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
}
else { //IF PET IS NOT HAPPY, IT'S SAD. DO THIS CODE:
u8g2.clearBuffer();
sadFrame1();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
u8g2.clearBuffer();
sadFrame2();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
u8g2.clearBuffer();
sadFrame3();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
u8g2.clearBuffer();
sadFrame2();
u8g2.sendBuffer();
delay(1000 / framesPerSecond);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment