Volos Rakic
Published

Binary Computer / Calculator / Game

I created this naked ATmega328 mini computer to help kids understand binary to decimal conversion, as well as to explain ASC to them.

IntermediateProtip12 hours1,190
Binary Computer / Calculator / Game

Story

Read more

Code

binaryDecimal.ino

C/C++
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> 
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSans18pt7b.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

const unsigned char PROGMEM calc [] = {
0x00, 0x00, 0x07, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFE, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF,
0xC0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x7F,
0xFF, 0xFF, 0xFE, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x80,
0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xC0, 0x07, 0xF8, 0x00, 0x00, 0x3F, 0xE0, 0x07, 0xF0, 0x00, 0x00,
0x0F, 0xE0, 0x0F, 0xF0, 0x00, 0x00, 0x0F, 0xF0, 0x1F, 0xE0, 0x00, 0x00, 0x0F, 0xF8, 0x1F, 0xE0,
0xFF, 0xFF, 0x0F, 0xF8, 0x3F, 0xE1, 0xFF, 0xFF, 0x8F, 0xFC, 0x3F, 0xE1, 0xFF, 0xFF, 0x8F, 0xFC,
0x3F, 0xE1, 0xFF, 0xFF, 0x8F, 0xFC, 0x7F, 0xE1, 0xFF, 0xFF, 0x8F, 0xFE, 0x7F, 0xE1, 0xFF, 0xFF,
0x8F, 0xFE, 0x7F, 0xE0, 0x00, 0x00, 0x0F, 0xFE, 0x7F, 0xE0, 0x00, 0x00, 0x0F, 0xFE, 0xF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 0xFF, 0xFF, 0xFE, 0xF7, 0xFF, 0xFF, 0xFF,
0xFF, 0xFE, 0xE3, 0xFC, 0x00, 0x00, 0x00, 0x00, 0xF7, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFE,
0x03, 0xFE, 0x00, 0x00, 0xFF, 0xFE, 0x03, 0xFE, 0x00, 0x00, 0xFF, 0xFE, 0x03, 0xFE, 0x00, 0x00,
0xFF, 0xFE, 0x03, 0xFF, 0x00, 0x00, 0xFF, 0xFE, 0x07, 0xFF, 0x00, 0x00, 0xFF, 0xFE, 0x0F, 0xFF,
0x00, 0x00, 0xFF, 0xFE, 0x3F, 0xFF, 0xE0, 0x00, 0x7F, 0xFC, 0x7F, 0xFF, 0xE0, 0x00, 0x3F, 0xF8,
0x7F, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

const unsigned char PROGMEM youtube[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFC, 0x7F, 0xFE, 0x7F, 0xFE, 0x7C, 0xFE, 0x7C, 0x3E,
0x7C, 0x3E, 0x7C, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x3F, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};







void setup()   {                
 
for(int i=5;i<13;i++)
pinMode(i,INPUT_PULLUP);

pinMode(A1,INPUT_PULLUP);
pinMode(A2,INPUT_PULLUP);
 
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  display.display();
  display.clearDisplay();
display.setTextSize(0);

    display.setFont(&FreeSans9pt7b);
    int pom=4;
  display.setTextColor(WHITE);
  display.setCursor(0,14);
  display.println("VoloS");
  display.setCursor(0,30);
  display.println("ProjectS");
  display.drawBitmap(82, 10,pc, 48,48, 1);
  display.drawBitmap(0, 46+pom,youtube, 16,16, 1);
  display.setFont();
  display.drawLine(0,36,22,36,1);
  display.setCursor(0,38+pom);
  display.println("ELECTRONICS");
    display.setCursor(22,50+pom);
  display.println("CHANNEL");
    
   display.display();
   delay(3500);
}
int calcp=0;
int bx=60;
int by=32;
int sco=0;
int px=2;
int py=2;
int duz=122;
int pom=0;
String bb; 
byte broj=0;
int poz=0;
byte mode=0;
byte fase=0;
byte first=0;
byte second=0;
int result=0;
char operation;
char o[4]={'+','-','*','='};
String txt="";
int show=0;
void loop() {

 broj=0;

 
  display.clearDisplay();
  poz=map(analogRead(A0),0,900,0,3);

    for(int i=5;i<13;i++)
    {
    if(digitalRead(i)==0)
    bitWrite(broj,13-(i+1),1);
    }

  if(digitalRead(A1)==0 && mode==0)
  mode=poz;
 

  if(digitalRead(A2)==0)
  mode=0;




  if(mode==0)
  {
    calcp=0;
    show=0;
    fase=0;
    txt="";
  display.setFont(&FreeSans9pt7b);
  

  

  


  

 display.setCursor(1,12);
  display.println(broj,BIN);

  

    display.setCursor(1,36);
  display.println(broj);
    display.setCursor(1,58);
  
  display.println(broj,HEX);

  display.setFont();
  display.setCursor(88,0);
  display.println("BINARY");

 display.setCursor(36,28);
  display.println("DECIMAL");
  display.setCursor(36,50);
  display.println("HEXA");

 

   display.setFont(&FreeSans9pt7b);
  
  
   display.setFont();


   
   display.setCursor(100,22);
   display.println("CONV");
   display.setCursor(100,32);
   display.println("CALC");
   display.setCursor(100,42);
   display.println("TEXT");
    display.setCursor(100,52);
   display.println("GAME");

   display.setCursor(90,22+(poz*10));
   display.println(">");

  
    display.drawLine(0,16,124,16,1);
    display.drawLine(0,17,124,17,1);
    display.drawLine(86,16,86,64,1);
    display.drawLine(84,16,84,64,1);
    display.drawLine(36,42,78,42,1);
    display.drawLine(0,63,128,63,1);
    
  
    
  }

  if(mode==1)
  {
    if(calcp==0)
    {
      calcp=1;
      display.clearDisplay();
      display.drawBitmap(42, 10,calc, 48,48, 1);
      display.display();
      delay(1400);
    }
    
    if(digitalRead(A1)==0)
    {
     if(pom==0)
     {
      pom=1;

      if(fase==2 && poz==3)
      {
        second=broj;
        if(operation=='+')
        result=first+second;
        if(operation=='-')
        result=first-second;
        if(operation=='*')
        result=first*second;
        fase=3;
        }

      if(fase==1)
      {
        fase=2;
        first=broj;
        bb=bb+String(o[poz]);
        operation=o[poz];
        }
      if(fase==0)
      fase=1;
      
     }}else{pom=0;}
     
      
      
    
    display.setFont();
     display.setCursor(110,25);
     display.println("+");
   display.setCursor(110,35);
   display.println("-");
   display.setCursor(110,45);
   display.println("*");
    display.setCursor(110,55);
   display.println("=");

   display.setCursor(94,25+(poz*10));
   display.println(">");

   display.drawLine(0,16,124,16,1);
    display.drawLine(0,17,124,17,1);
    display.drawLine(86,16,86,64,1);

   display.setFont(&FreeSans9pt7b);
    display.setCursor(0,12);

    if(fase==0)
    { display.setFont();
      display.setCursor(10,7);
  display.println("BINARY CALCULATOR:");
      }

    if(fase==1){
    bb=String(broj);
    display.println(bb);
    display.setFont();
    display.setCursor(16,28);
    display.println(broj,BIN);
    display.setCursor(92,7);
  display.println("FIRST");
    }

    if(fase==2)
    {display.print(bb);
    display.print(broj);
    display.setFont();
    display.setCursor(90,7);
  display.println("SECOND");
    display.setCursor(16,28);
    display.println(first,BIN);
    display.setCursor(8,38);
    display.println(operation);
    display.setCursor(16,38);
    display.println(broj,BIN);}

 if(fase==3)
  {
  String finis=String(first)+String(operation)+String(second)+"="+String(result);
  display.print(finis);
      display.setFont();
    display.setCursor(16,28);
    display.println(first,BIN);
    display.setCursor(8,38);
    display.println(operation);
    display.setCursor(16,38);
    display.println(second,BIN);
    display.drawLine(8,48,70,48,1);
    display.setCursor(16,52);
    display.println(result,BIN);
  }
    
  
    
  }

  if(mode==2)
  {

    if(show==0)
    {
  if(digitalRead(A1)==0 )
  {
    if(pom==0){
      pom=1;
        if(poz==0)
        txt=txt+String(char(broj));
        if(poz==1)
        txt.remove(txt.length()-1);
        if(poz==3)
        show=1;
   }
    
    }else {pom=0;}

     
    
        display.setFont();
        display.setCursor(94,25);
        display.println("ADD");
   display.setCursor(94,35);
   display.println("DEL");
   display.setCursor(94,45);
   display.println("SAVE");
    display.setCursor(94,55);
    display.println("SHOW");
    display.setCursor(2,39);
    display.print("DEC:");
     
   display.print(broj);
   
   display.setCursor(2,56);
   display.println("ASCII");

   display.drawLine(0,52,48,52,1);
   display.drawLine(48,52,48,38,1);
     display.drawLine(48,38,78,38,1);
     display.drawLine(82,16,82,64,1);
     display.drawLine(84,16,84,64,1);
  

   display.setCursor(86,25+(poz*10));
   display.println(">");

   display.drawLine(0,16,124,16,1);

   display.setFont(&FreeSans9pt7b);
   display.setCursor(0,32);
   display.println(broj,BIN);
   display.setFont(&FreeSans12pt7b);
   display.setCursor(54,62);
   display.println(char(broj));
   display.setFont(&FreeSans9pt7b);
    display.setCursor(0,12);
    display.println(txt);
    }
    if(show==1)
    {
      if(txt.length()==0)
      txt="Subscribe Please :)";
      display.setFont(&FreeSans18pt7b);
      display.setCursor(duz,55);
     display.println(txt);
     duz=duz-2;
     if(duz<-260)
     duz=122;
     delay(poz*1);
     }
    
   
    }

    if(mode==3)
    {
      display.setFont();
      display.setCursor(0,0);
      display.println("SCORE:");
      display.setCursor(38,0);
      display.println(sco);
     
      display.drawRect(-2,8,130,56,1);
      int pp=map(analogRead(A0),0,1024,8,50);
      display.fillRect(0,pp,4,14,1);

      display.fillCircle(bx,by,3,1);

      if(bx>128-6)
      px=px*-1;

       if(by>64-6)
      py=py*-1;

        if(by<13)
      py=py*-1;

      if(bx<7)
        if(by>=pp && by<=pp+15)
        {px=px*-1;
        sco=sco+1;}

        if(bx<-2)
        {
          display.clearDisplay();
          display.setFont(&FreeSans9pt7b);
      display.setCursor(6,35);
      display.println("GAME OVER!");
      display.setCursor(36,55);
     display.println(sco);
     display.display();
     delay(2000);
     bx=random(20,45);
     by=random(20,55);
     sco=0;
     px=px*-1;
          
          }

      bx=bx+px;
      by=by+py;
      
      }
       display.display();
}

Credits

Volos Rakic

Volos Rakic

5 projects • 3 followers

Comments