/*
A simple Pong game.
https://github.com/eholk/Arduino-Pong/blob/master/pong.ino
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//Define buttons
#define UP_PIN 0
#define DOWN_PIN 2
#define OK_PIN 6
#define LEFT_PIN 3
#define RIGHT_PIN 4
String state = "home"; //This variable changes with interfaces --- used for identifying the interface
String tested_state = "not tested"; //If the tested component is ok or not ok. Default is "not tested"
String retest_mode = "off";
int mode_selected = 0; //This vriable is for store the mode(pin selection/BJT/Bluetooth) --- for choosing operating mode
int bjt_selected = 0; //NPN or PNP
int return_home = 0; //This variable is for selecting Retest or Home
int page = 0; //used for breaking the IC list to pages
int ic_sel = 0; //used for going up and down in a ic page
int ic_ind;
int pin_number = 0;
int max_pages;
int status_of_test = 0;
String pin_4[20] = {"comp1", "comp2", "comp3", "comp4","comp5","comp6","comp7","comp8","comp9","comp10","comp11","comp12","comp13","comp14","comp15","comp16","comp17","comp18","comp19","comp20"};
String pin_6[20] = {"comp1", "comp2", "comp3", "comp4","comp5","comp6","comp7","comp8","comp9","comp10","comp11","comp12","comp13","comp14","comp15","comp16","comp17","comp18","comp19","comp20"};
String pin_8[20] = {"comp1", "comp2", "comp3", "comp4","comp5","comp6","comp7","comp8","comp9","comp10","comp11","comp12","comp13","comp14","comp15","comp16","comp17","comp18","comp19","comp20"};
String pin_12[20] = {"comp1", "comp2", "comp3", "comp4","comp5","comp6","comp7","comp8","comp9","comp10","comp11","comp12","comp13","comp14","comp15","comp16","comp17","comp18","comp19","comp20"};
String pin_14[20] = {"comp1", "comp2", "comp3", "comp4","comp5","comp6","comp7","comp8","comp9","comp10","comp11","comp12","comp13","comp14","comp15","comp16","comp17","comp18","comp19","comp20"};
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup()
{
Serial.begin(9600);
pinMode(UP_PIN, INPUT_PULLUP);
pinMode(DOWN_PIN, INPUT_PULLUP);
pinMode(OK_PIN, INPUT_PULLUP);
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(20, 20); // position to display
oled.println("Welcome!"); // text to display
oled.setTextSize(1); // text size
oled.setCursor(85, 55); // position to display
oled.println("CodeRed"); // text to display
oled.display(); // show on OLED
oled.clearDisplay(); // clear display
delay (500);
}
void loop()
{
int up_val = digitalRead(UP_PIN);
int down_val = digitalRead(DOWN_PIN);
int left_val = digitalRead(LEFT_PIN);
int right_val = digitalRead(RIGHT_PIN);
//int ok_val = digitalRead(OK_PIN);
//Serial.println(ok_val);
if (state == "home"){
displayMenu();
selectMenu();
}
if (state == " Pin Selection"){
pinSelection();
pinnumSelect();
}
if (state == " BJT"){
//bjtModel();
//bjtSelect();
}
if ((state == "ok") || (state == "not ok")){
//testedOutput("BJT", right_val, left_val, ok_val);
}
if (state == " 4"){
if (retest_mode == "on"){
compOK();
retest_mode = "off";
}
else if (tested_state == "tested"){
homeorretest();
}
else{
icPages(page, pin_4, 20);
backFoward();
int ok_val =digitalRead(OK_PIN);
Serial.println(" OK: " + String(ok_val));
if (ok_val == 0){
compOK();
}
}
}
}
//this function is for homepage
void displayMenu()
{
int up_val = digitalRead(UP_PIN);
int down_val = digitalRead(DOWN_PIN);
const char *modes[3] = {
" Pin Selection",
" BJT",
" Bluetooth"
};
//Moving between modes
if (up_val == 0){
if (mode_selected == 0) {
mode_selected = 2;
}
else{
mode_selected = mode_selected - 1;
}
delay(150);
}
else if (down_val == 0){
if (mode_selected == 2) {
mode_selected = 0;
}
else{
mode_selected = mode_selected + 1;
}
delay(150);
}
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 10);
oled.println("Select the Mode");
oled.setCursor(0, 25);
for (int i = 0; i < 3; i++) {
if (i == mode_selected){
oled.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
oled.println(modes[i]);
}
else{
oled.setTextColor(SSD1306_WHITE);
oled.println(modes[i]);
}
}
oled.display();
}
//this function is for select item for home menu
void selectMenu()
{
int ok_val = digitalRead(OK_PIN);
const char *modes[3] = {
" Pin Selection",
" BJT",
" Bluetooth"
};
if (ok_val == 0){
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(5,10);
oled.println("--"+String(modes[mode_selected])+" --");
oled.display();
state = modes[mode_selected]; //state changes to the mode
delay(2000);
mode_selected = 0;
}
}
//this is for pin selection page
void pinSelection()
{
int up_val = digitalRead(UP_PIN);
int down_val = digitalRead(DOWN_PIN);
const char *pin[5] = {
" 4",
" 6",
" 8",
" 12",
" 16"
};
//Moving between modes
if (up_val == 0){
if (pin_number == 0) {
pin_number = 4;
delay(100);
}
else{
pin_number = pin_number - 1;
delay(100);
}
}
else if (down_val == 0){
if (pin_number == 4) {
pin_number = 0;
delay(100);
}
else{
pin_number = pin_number + 1;
delay(100);
}
}
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 0);
oled.println("Select the Number of Pins");
oled.setCursor(0, 20);
for (int i = 0; i < 5; i++) {
if (i == pin_number){
oled.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
oled.println(pin[i]);
}
else{
oled.setTextColor(SSD1306_WHITE);
oled.println(pin[i]);
}
}
oled.display();
}
//this is for selectig number of pins
void pinnumSelect()
{
int ok_val = digitalRead(OK_PIN);
const char *pin[5] = {
" 4",
" 6",
" 8",
" 12",
" 16"
};
if (ok_val == 0){
state = pin[pin_number];
}
delay(150);
}
//this is for bjt interface
void bjtModel(int up_val, int down_val)
{
const char *bjt[2] = {
"PNP",
"NPN"
};
if ((up_val == 0)||(down_val == 0)){
if (bjt_selected == 0){
bjt_selected = 1;
}
else if (bjt_selected == 1){
bjt_selected = 0;
}
delay(100);
}
if (bjt_selected == 0){
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 10);
oled.println("Select the BJT model");
oled.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
oled.setCursor(10, 30);
oled.println("PNP");
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(10, 45);
oled.println("NPN");
oled.display();
delay(100);
}
else if (bjt_selected == 1){
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 10);
oled.println("Select the BJT model");
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(10, 30);
oled.println("PNP");
oled.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
oled.setCursor(10, 45);
oled.println("NPN");
oled.display();
delay(100);
}
}
//this is for select pnp/npn
void bjtSelect()
{
int ok_val = digitalRead(OK_PIN);
const char *bjt[2] = {
"NPN",
"PNP"
};
if (ok_val == 0){
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 10);
oled.println(bjt[bjt_selected]);
oled.display();
state = bjt[bjt_selected]; //state changes to the mode
delay(100);
bjt_selected = 0;
}
}
//this is for connecting a device
/*void connectDevice(String component){
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(20, 0);
oled.println("Connect the "+component);
oled.setCursor(40, 0);
oled.println("Then press OK");
oled.display();
}*/
//this is for showing ics page by page
void icPages(int page, String pin_4[], int numIC)
{
int up_val = digitalRead(UP_PIN);
int down_val = digitalRead(DOWN_PIN);
int Start, End;
//Serial.println(sizeof(pin_4));
max_pages = ceil(numIC/6);
//Serial.println("max pages: "+ String(max_pages));
Start = page*6;
//const int numIC = 20;
if (numIC < Start+6){
End = numIC;
}
else{
End = Start+6;
}
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setCursor(0, 0); // position to display
for (int k=Start; k<End; k++){
if (k == (Start + ic_sel)){
ic_ind = k;
oled.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
oled.println(pin_4[k]);
}
else{
oled.setTextColor(SSD1306_WHITE);
oled.println(pin_4[k]);
}
}
if (page != max_pages){
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(104, 50);
oled.println(">");
}
if (page != 0){
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 50);
oled.println("<");
}
oled.display();
if (up_val==0){
if (ic_sel == 0){
if (page == max_pages){
ic_sel = numIC - 6*(max_pages) -1;
Serial.println("ic_sel: "+ String(ic_sel));
}
else{
ic_sel = 5;
}
}
else{
ic_sel = ic_sel - 1;
}
delay(100);
}
if (down_val==0){
if (page == max_pages){
if (max_pages*6 + ic_sel +1 == numIC){
ic_sel = 0;
}
else{
ic_sel = ic_sel + 1;
}
}
else if (ic_sel == 5){
ic_sel = 0;
}
else{
ic_sel = ic_sel + 1;
}
delay(100);
}
}
void backFoward()
{
int right_val = digitalRead(RIGHT_PIN);
int left_val = digitalRead(LEFT_PIN);
if (right_val == 0){
if (page != max_pages){
page= page+1;
ic_sel = 0;
}
delay(150);
}
if (left_val == 0){
if (page != 0){
page= page-1;
ic_sel = 0;
}
delay(150);
}
}
//select the component and do testing
void compOK(){
int ind = page*6 + ic_sel;
//index_sel[1] = ind;
//index_sel[0] = pin_number; //4,6,8,12,14
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setCursor(20, 25); // position to display
oled.println("Testing.... ");
oled.display();
delay(100);
//multi(index_sel[1]);
if (tested_state == "not tested"){
if (status_of_test != 0){
oled.clearDisplay();
oled.setTextSize(1);
oled.setCursor(5,10);
oled.println("Out of " );
oled.setCursor(25,20);
oled.println(" test cases");
oled.setCursor(35,30);
oled.println("passed!!!");
delay(500);
}
else{
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setCursor(20, 15);
oled.println("All test cases");
oled.setCursor(35, 25);
oled.println("passed!!!");
delay(500);
}
tested_state = "tested";
}
}
void homeorretest()
{
int right_val = digitalRead(RIGHT_PIN);
int left_val = digitalRead(LEFT_PIN);
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0,50);
oled.println("< Retest");
oled.setCursor(90,50);
oled.println("Home >");
oled.display();
if (right_val == 0){
state = "home";
tested_state = "not tested";
state = "home";
mode_selected = 0;
bjt_selected = 0;
return_home = 0;
page = 0;
ic_sel = 0;
pin_number = 0;
retest_mode = "off";
}
else if (left_val == 0){
tested_state = "not tested";
retest_mode = "on";
}
}