Arend-Jan van Hilten
Published © GPL3+

Basic IO Shield Start Project

This is a project to show how to use the things on the Basic IO Shield from Digilent for the ChipKIT family.

BeginnerProtip1,580
Basic IO Shield Start Project

Things used in this project

Hardware components

UC32
×1
chipKIT Basic I/O Shield
×1

Software apps and online services

MPIDE
To program the UC32(Arduino IDE didn't work for me)

Story

Read more

Code

Example code

Arduino
This is the code. Not really good code, but I tried to upgrade it(made it 2 years ago)
/*
**Written by Arend-Jan van Hilten( first code made for National Instruments)
 **Arend-Jan.com
 */


#include <IOShieldTemp.h>
#include <IOShieldOled.h>
#include <Wire.h>
int tensc = 0;
int onesc = 0;
int dot_tensc = 0;
int dot_onesc = 0;
int tensf = 0;
int onesf = 0;
int dot_tensf = 0;
int dot_onesf = 0;
int tempC=0;
int tempF=0;
int a=0;
int z=0;
void setup()
{   
  Serial.begin(9600);
  pinMode(2, INPUT);
  pinMode(8, INPUT);
  pinMode(7, INPUT);
  pinMode(35, INPUT);
  pinMode(36, INPUT);
  pinMode(37, INPUT);
  pinMode(34, INPUT);
  pinMode(4, INPUT);
  pinMode(A0, INPUT);
  pinMode(33, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(30, OUTPUT);
  pinMode(29, OUTPUT);
  pinMode(28, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(26, OUTPUT);
  for(int x=26;x<34;x++){
    digitalWrite(x, HIGH);
    delay(100);
  }
  IOShieldOled.begin();
  IOShieldOled.displayOn();
  IOShieldTemp.config(IOSHIELDTEMP_ONESHOT | IOSHIELDTEMP_RES12);
  digitalWrite(26, LOW);
  digitalWrite(27, LOW);
  digitalWrite(28, LOW);
  digitalWrite(29, LOW);
  digitalWrite(30, LOW);
  digitalWrite(31, LOW);
  digitalWrite(32, LOW);
  digitalWrite(33, LOW);

}

String getState(){//This is a function that returns the state of the slide switches, 
  //The output is as following: 
  //if slide 1 is up: 1..., if not: 0...
  //if slide 2 is up: .2.., if not: .0..
  //if slide 3 is up: ..3., if not: ..0.
  //if slide 4 is up: ...4, of not: ...0

  String output ="";
  if(digitalRead(35)){
    output+="1";
  }
  else{
    output+="0";
  }
  if(digitalRead(8)){
    output+="2";
  }
  else{
    output+="0";
  }
  if(digitalRead(7)){
    output+="3";
  }
  else{
    output+="0";
  }
  if(digitalRead(2)){
    output+="4";
  }
  else{
    output+="0";
  }
  return output;
}

int getAmount(){
  int output = 0;
  if(digitalRead(35)){
    output++;
  }
  if(digitalRead(7)){
    output++;
  }
  if(digitalRead(8)){
    output++;
  }
  if(digitalRead(2)){
    output++;
  }
  return output;
}

String getButtons(){
  //This is the same scheme as getState
  String output ="";
  if(String("0200")!=getState()){//If the slide isn't up or another order, the while loop should stop;
    return "asdf";//Doesn't matter what text here, but it can't be any possible output;
  }
  if(digitalRead(37)){
    output+="1";
  }
  else{
    output+="0";
  }
  if(digitalRead(36)){
    output+="2";
  }
  else{
    output+="0";
  }
  if(digitalRead(34)){
    output+="3";
  }
  else{
    output+="0";
  }
  if(digitalRead(4)){
    output+="4";
  }
  else{
    output+="0";
  }
  return output;
}
void loop(){
  while(String("0004") == getState()){//temperature
    float tempf, tempc;
    //This code is from some example, but yes, I know it is shit code;
    //Get Temperature in Celsius.
    tempc = IOShieldTemp.getTemp();

    // Convert the result to Fahrenheit.
    tempf = IOShieldTemp.convCtoF(tempc);

    //parse data for temperature in celcius
    tempC=tempc*100;
    dot_onesc = tempC%10;
    tempC = tempC/10;
    dot_tensc = tempC%10;
    tempC = tempC/10;
    onesc = tempC%10;
    tempC = tempC/10;
    tensc = tempC%10;

    //convert data to ASCII for temperature in celcius
    tensc = tensc+48;
    onesc = onesc+48;
    dot_tensc = dot_tensc+48;
    dot_onesc = dot_onesc+48; 

    //parse data for temperature in Fahrenheit
    tempF=tempf*100;
    dot_onesf = tempF%10;
    tempF = tempF/10;
    dot_tensf = tempF%10;
    tempF = tempF/10;
    onesf = tempF%10;
    tempF = tempF/10;
    tensf = tempF%10;

    //convert data to ASCII for temperature in Fahrenheit
    tensf = tensf+48;
    onesf = onesf+48;
    dot_tensf = dot_tensf+48;
    dot_onesf = dot_onesf+48;

    //Clear the virtual buffer
    IOShieldOled.clearBuffer();

    //Draw a rectangle over wrting then slide the rectagle
    //down slowly displaying all writing
    IOShieldOled.clearBuffer();
    IOShieldOled.setCursor(0, 0);
    IOShieldOled.putString("Temperature is");
    IOShieldOled.setCursor(0, 2);
    IOShieldOled.putChar(tensc);
    IOShieldOled.setCursor(1, 2);
    IOShieldOled.putChar(onesc);
    IOShieldOled.setCursor(2, 2);
    IOShieldOled.putString(".");
    IOShieldOled.setCursor(3, 2);
    IOShieldOled.putChar(dot_tensc);
    IOShieldOled.setCursor(4, 2);
    IOShieldOled.putChar(dot_onesc);
    IOShieldOled.setCursor(6, 2);
    IOShieldOled.putString(" C");


    delay(100);
  };
  z=0;
  while(String("0200") == getState()){ //buttongame
    if(String("0004")==getButtons()){  
      IOShieldOled.clearBuffer();
      IOShieldOled.setCursor(0, 0);
      IOShieldOled.putString("You are pressing the most right button");
      while(String("0004")==getButtons()){
      };
    };
    if(String("0030")==getButtons()){
      IOShieldOled.clearBuffer();
      IOShieldOled.setCursor(0, 0);
      IOShieldOled.putString("You are pressing the middle right button");
      while(String("0030")==getButtons()){
      };
    };
    if(String("0200")==getButtons()){
      IOShieldOled.clearBuffer();
      IOShieldOled.setCursor(0, 0);
      IOShieldOled.putString("You are pressing the middle left button");
      while(String("0200")==getButtons()){
      };
    };
    if(String("1000")==getButtons()){
      IOShieldOled.clearBuffer();
      IOShieldOled.setCursor(0, 0);
      IOShieldOled.putString("You are pressing the most left button");
      while(String("1000")==getButtons()){
      };
    };
    if(String("0230")==getButtons()){
      IOShieldOled.clearBuffer();
      IOShieldOled.setCursor(0, 0);
      IOShieldOled.putString("You are pressing the middle buttons");
      while(String("0230")==getButtons()){
      };
    };
    if(String("1004")==getButtons()){
      IOShieldOled.clearBuffer();
      IOShieldOled.setCursor(0, 0);
      IOShieldOled.putString("You are pressing the outer buttons");
      while(String("1004")==getButtons()){
      };
    };
    if(String("0000")==getButtons()){
      IOShieldOled.clearBuffer();
      IOShieldOled.setCursor(0, 0);
      IOShieldOled.putString("Press one or two buttons..");
      while(String("0000")==getButtons()){
      };
    };

    if(String("1200")==getButtons()){
      IOShieldOled.clearBuffer();
      IOShieldOled.setCursor(0, 0);
      IOShieldOled.putString("You are pressing the left buttons");
      while(String("1200")==getButtons()){
      };
    };
    if(String("0034")==getButtons()){
      IOShieldOled.clearBuffer();
      IOShieldOled.setCursor(0, 0);
      IOShieldOled.putString("You are pressing the right buttons");
      while(String("0034")==getButtons()){
      };
    };
    if(String("1030")==getButtons()){
      IOShieldOled.clearBuffer();
      IOShieldOled.setCursor(0, 0);
      IOShieldOled.putString("You are pressing buttons 1 & 3");
      while(String("1030")==getButtons()){
      };
    };
    if(String("0204")==getButtons()){
      IOShieldOled.clearBuffer();
      IOShieldOled.setCursor(0, 0);
      IOShieldOled.putString("You are pressing buttons 2 & 4");
      while(String("0204")==getButtons()){
      };
    };
  }; 

  while(String("0030") == getState()){//potentiometer
    z=analogRead(0);
    float tempf, tempc;
    tempc=z;
    //Im sorry for the random use of variables, need to change it maybe.
    //divide it into 4 digits
    tempC=tempc;
    dot_onesc = tempC%10;
    tempC = tempC/10;
    dot_tensc = tempC%10;
    tempC = tempC/10;
    onesc = tempC%10;
    tempC = tempC/10;
    tensc = tempC%10;

    //bad programming, but to change the number to a string, so you can print it on the oled display
    tensc = tensc+48;
    onesc = onesc+48;
    dot_tensc = dot_tensc+48;
    dot_onesc = dot_onesc+48;
    IOShieldOled.clearBuffer();
    IOShieldOled.setCursor(0, 2);
    IOShieldOled.putChar(tensc);
    IOShieldOled.setCursor(1, 2);
    IOShieldOled.putChar(onesc);
    IOShieldOled.setCursor(2, 2);
    IOShieldOled.putString("");
    IOShieldOled.setCursor(3, 2);
    IOShieldOled.putChar(dot_tensc);
    IOShieldOled.setCursor(4, 2);
    IOShieldOled.putChar(dot_onesc);
    IOShieldOled.setCursor(6, 2);
    IOShieldOled.putString(" ");
    delay(50);
  };
  while(String("1000") == getState()){//led with potentiometer
    z=analogRead(0);
    IOShieldOled.clearBuffer();
    IOShieldOled.setCursor(0, 0);
    IOShieldOled.putString("Twist the potentiometer.");
    //If the level is high enough, the lights go on
    //there is no break, and it should be in this order,
    // so if 8 leds should go on, it goes to case 8 and then goes through the other cases, because those leds should also light up
    switch(z/120){
    case 8:
      digitalWrite(26, HIGH);
    case 7:
      digitalWrite(27, HIGH);
    case 6:
      digitalWrite(28, HIGH);
    case 5:
      digitalWrite(29, HIGH);
    case 4:
      digitalWrite(30, HIGH);
    case 3:
      digitalWrite(31, HIGH);
    case 2:
      digitalWrite(32, HIGH);
    case 1:
      digitalWrite(33, HIGH);
      break;
    default: //Just to be sure, can't actually be more than 8, max = 8.5 =8, but for 0 this is used;
      break;
    }

    delay(100);//And then dim al the lights.
    //Not really good coding...
    digitalWrite(26,LOW );
    digitalWrite(27, LOW);
    digitalWrite(28, LOW);
    digitalWrite(29, LOW);
    digitalWrite(30, LOW);
    digitalWrite(31, LOW);
    digitalWrite(32, LOW);
    digitalWrite(33, LOW);

  };







  IOShieldOled.clearBuffer();
  if(getAmount()==0){    //If the amount of buttons up is 0, show the text:

    IOShieldOled.setCursor(0, 0);
    IOShieldOled.putString(" Please set one");
    IOShieldOled.setCursor(0, 2);
    IOShieldOled.putString(" or more           switches up");
    while(getAmount()==0){
    };
  };

  if(getAmount()==2){  //amount ==1 can't be, because that is handeled by the functions before;
    IOShieldOled.clearBuffer();
    IOShieldOled.setCursor(0, 0);
    IOShieldOled.putString("UC32 with     basic io shield");
    while(getAmount()==2){
    };
  };




  if(getAmount()==3){
    IOShieldOled.clearBuffer();
    IOShieldOled.setCursor(0, 0);
    IOShieldOled.putString("Hackster.io");
    while(getAmount()==3){
    };
  };



  if(getAmount()==4){
    IOShieldOled.clearBuffer();
    IOShieldOled.setCursor(0, 0);
    IOShieldOled.putString("Arend-Jan van   Hilten");
    IOShieldOled.setCursor(0, 2);
    IOShieldOled.putString("arend-jan.com");
    while(getAmount()==4){
    };
  };
}

Credits

Arend-Jan van Hilten

Arend-Jan van Hilten

5 projects • 12 followers
Labview, Arduino, Raspberry PI and other stuff for me!

Comments