zondag 9 december 2012

Arduino - Experiment 6 - 16x2 I2C LCD scherm

In dit experiment heb ik een LCD schermpje aangesloten op de Arduino. Dit is handig voor toekomstige experimenten om bijv. bepaalde output te kunnen weergeven.
In veel voorbeelden wordt gebruik gemaakt van seriele output, maar helaas is een laptop met een com-poort steeds lastiger te vinden. Daarom de keus voor een LCD scherm.

Nu heeft zo'n LCD scherm ook wat nadelen, onder andere dat er 8 datalijntjes nodig zijn om iets weer te kunnen geven. Daarom heb ik gekozen voor een seriele versie: een LCD op basis van I2C: een seriele databus voor IC's.


I2C is een bus met een klok (SCL) en data (SDA) lijnen met 7-bits adressering. De bus heeft twee rollen voor nodes: master en slave:
Master node - Node die de klok en slave adressen verstuurt
Slave node - Node die de klok en het adres ontvangt.

De bus is een multi-master bus: een aantal Master nodes kunnen aanwezig zijn. Bovendien is het mogelijk master en slave rollen worden gewijzigd tussen de berichten (nadat een STOP wordt verzonden).

Dit is dus ideaal voor de Arduino, in toekomstige experimenten kunnen er nog een aantal componenten toegevoegd worden aan de Arduino terwijl we het schermpje kunnen blijven gebruiken.

Aansluitingen (LCD - Arduino UNO):
Vcc - 5V
GND - GND
SCL - analoog 5
SDA - analoog 4 

Voor dit experiment heb ik gebruik gemaakt van een stukje sample code, aangepast voor mijn scherm (adres aangepast):

//DFRobot.com

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x20,16,2);  // set the LCD address to 0x20 for a 16 chars and 2 line display

void setup()

{

 lcd.init();                      // initialize the lcd

 // Print a message to the LCD.

 lcd.backlight();

 lcd.print("Het Arduino - de");
 lcd.setCursor(0, 1);
 lcd.print("Lange Programma");

}
void loop()
{
}


En dat ziet er dan zo uit:



zondag 3 juni 2012

Arduino - Experiment 5 - Numerical Keypad

In experiment numero 5 heb ik een numeriek toetsenbordje op de arduino aangesloten. Om zichtbaar te maken welke toets ingedrukt is, gebruikte ik een 7 segments display.

Allereerst heb ik het toetsenbordje aangepast zodat er, als je op een toets drukt, op twee draden 5V komt te staan. De combinatie van HIGH en LOW op de draden is hieronder weergegeven:



De code in de Arduino is als volgt:



const int DraadZwart = 13;
const int DraadGrijs = 12;
const int DraadBruin = 11;
const int DraadPaars = 10;
const int DraadRood = 9;
const int DraadBlauw = 8;
const int DraadOranje = 7;
int StatusZwart = 0;
int StatusGrijs = 0;
int StatusBruin = 0;
int StatusPaars = 0;
int StatusRood = 0;
int StatusBlauw = 0;
int StatusOranje = 0;
#define A 0
#define B 1
#define C 2
#define D 3


void setup() {
pinMode(DraadZwart, INPUT);
pinMode(DraadGrijs, INPUT);
pinMode(DraadBruin, INPUT);
pinMode(DraadPaars, INPUT);
pinMode(DraadRood, INPUT);
pinMode(DraadBlauw, INPUT);
pinMode(DraadOranje, INPUT);
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(C,OUTPUT);
pinMode(D,OUTPUT);    
}




void loop(){
StatusZwart = digitalRead(DraadZwart);
StatusGrijs = digitalRead(DraadGrijs);
StatusBruin = digitalRead(DraadBruin);
StatusPaars = digitalRead(DraadPaars);
StatusRood = digitalRead(DraadRood);
StatusBlauw = digitalRead(DraadBlauw);
StatusOranje = digitalRead(DraadOranje);




if (StatusZwart == HIGH && StatusRood == HIGH && StatusGrijs == LOW && StatusBruin == LOW && StatusPaars == LOW && StatusBlauw == LOW && StatusOranje == LOW) {     
digitalWrite(A,HIGH);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);  
}   
if (StatusZwart == HIGH && StatusBlauw == HIGH && StatusRood == LOW && StatusGrijs == LOW && StatusBruin == LOW && StatusPaars == LOW && StatusOranje == LOW) {     
digitalWrite(A,LOW);
digitalWrite(B,HIGH);
digitalWrite(C,LOW);
digitalWrite(D,LOW);  

if (StatusZwart == HIGH && StatusOranje == HIGH && StatusRood == LOW && StatusGrijs == LOW && StatusBruin == LOW && StatusPaars == LOW && StatusBlauw == LOW) {     
digitalWrite(A,HIGH);
digitalWrite(B,HIGH);
digitalWrite(C,LOW);
digitalWrite(D,LOW);  

if (StatusGrijs == HIGH && StatusRood == HIGH && StatusZwart == LOW && StatusBruin == LOW && StatusPaars == LOW && StatusBlauw == LOW && StatusOranje == LOW) {     
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,HIGH);
digitalWrite(D,LOW);  

if (StatusGrijs == HIGH && StatusBlauw == HIGH && StatusZwart == LOW && StatusBruin == LOW && StatusPaars == LOW && StatusRood == LOW && StatusOranje == LOW) {     
digitalWrite(A,HIGH);
digitalWrite(B,LOW);
digitalWrite(C,HIGH);
digitalWrite(D,LOW);  

if (StatusGrijs == HIGH && StatusOranje == HIGH && StatusZwart == LOW && StatusBruin == LOW && StatusPaars == LOW && StatusBlauw == LOW && StatusRood == LOW) {     
digitalWrite(A,LOW);
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(D,LOW);  

if (StatusBruin == HIGH && StatusRood == HIGH && StatusZwart == LOW && StatusGrijs == LOW && StatusPaars == LOW && StatusBlauw == LOW && StatusOranje == LOW) {     
digitalWrite(A,HIGH);
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(D,LOW);  

if (StatusBruin == HIGH && StatusBlauw == HIGH && StatusZwart == LOW && StatusGrijs == LOW && StatusPaars == LOW && StatusRood == LOW && StatusOranje == LOW) {     
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);  

if (StatusBruin == HIGH && StatusOranje == HIGH && StatusZwart == LOW && StatusGrijs == LOW && StatusPaars == LOW && StatusBlauw == LOW && StatusRood == LOW) {     
digitalWrite(A,HIGH);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);  

if (StatusPaars == HIGH && StatusRood == HIGH && StatusZwart == LOW && StatusBruin == LOW && StatusGrijs == LOW && StatusBlauw == LOW && StatusOranje == LOW) {     
digitalWrite(A,LOW);
digitalWrite(B,HIGH);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);  

if (StatusPaars == HIGH && StatusBlauw == HIGH && StatusZwart == LOW && StatusBruin == LOW && StatusGrijs == LOW && StatusRood == LOW && StatusOranje == LOW) {     
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);  

if (StatusPaars == HIGH && StatusOranje == HIGH && StatusZwart == LOW && StatusBruin == LOW && StatusGrijs == LOW && StatusBlauw == LOW && StatusRood == LOW) {     
digitalWrite(A,HIGH);
digitalWrite(B,HIGH);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);  
}
if (StatusZwart == LOW && StatusOranje == LOW && StatusRood == LOW && StatusGrijs == LOW && StatusBruin == LOW && StatusPaars == LOW && StatusBlauw == LOW) {     
digitalWrite(A,HIGH);
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(D,HIGH);  

}


En uiteindelijk ziet het er dan zo uit, wanneer er niets ingedrukt wordt is het display uit. Zo kun je ook zien dat er een ´0´ ingedrukt wordt.

zondag 22 april 2012

Arduino - Experiment 4 - Melodie

Het vierde experiment heeft twee nieuwe elementen: een input (drukknop) en geluid!

Ik heb geprobeerd een melodietje te programmeren, helaas ben ik niet zo goed in muzieknoten lezen :-)

Voor degene die het herkent en muzikaal is; verbeteringen zijn welkom!

De code:

*
  Melody

 Plays a melody when you push a button

 circuit:
 * piezo speaker on digital pin 8
 * pus botton on digital pin 2


 */
 #include "pitches.h"


// notes in the melody:
int melodystart[] = { 
  NOTE_D5, NOTE_D5, NOTE_C5, NOTE_F4, NOTE_G4};


int melody[] = {
  NOTE_G4, NOTE_G4, NOTE_B4, NOTE_D5, NOTE_C5, NOTE_G4, NOTE_G4, NOTE_G4, NOTE_F4, NOTE_B4, NOTE_G4, 
  NOTE_G4, NOTE_G4, NOTE_B4, NOTE_D5, NOTE_C5, NOTE_F5, NOTE_D5, NOTE_D5, NOTE_D5, NOTE_B4, NOTE_F5, 
  NOTE_A5, NOTE_F5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_G4, NOTE_F4, NOTE_E4, 
  NOTE_C6, NOTE_A5, NOTE_F5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_F4, NOTE_G4, 
  NOTE_D5, NOTE_D5, NOTE_C5, NOTE_F4, NOTE_G4};


// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurationsstart[] = {
  8, 8, 8, 4, 2 };
  
int noteDurations[] = {
  8, 8, 8, 8, 2, 2, 8, 8, 8, 8, 2,
  8, 8, 8, 8, 2, 2, 8, 8, 8, 8, 2,
  8, 8, 8, 8, 8, 8, 8, 2,
  8, 8, 8, 8, 8, 4, 8, 2,
  8, 8, 8, 4, 2 };


const int buttonPin = 2;
int buttonState = 0;


void setup() {
  pinMode(buttonPin, INPUT);
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 5; thisNote++) {


    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDurationstart = 1000/noteDurationsstart[thisNote];
    tone(8, melodystart[thisNote],noteDurationstart);


    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDurationstart * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}


void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);


  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    for (int thisNote = 0; thisNote < 43; thisNote++) {


    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);


    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }  
  } 
  else {
    }
}


De input button is een schakelaar die met één pootje aan de + zit en het andere pootje aan de  input. Om te zorgen dat in de 'open' stand de input 'laag' is, is de input met een pull-down weerstand verbonden met de -.




Wanneer de Arduino opstart hoor je een kort melodietje, wanneer je op de knop drukt speelt het hele melodietje.


En dat klinkt dan als volgt:


donderdag 23 februari 2012

Arduino - Experiment 3c - mm:ss teller

Als laatste in deze reeks komt de mm:ss teller.

Omdat er maar 14 uitgangen zijn heb ik de BCD tellers aangepast, de tientallen hoeven immers maar tot 5 te tellen (BCD 101). Hierdoor kon ik voor de tientallen de D uitsparen. Ik hoef dan geen 16 pinnen te hebben, maar 14. (komt dat even mooi uit!)

De code is als volgt:


#define BIT_A1 0
#define BIT_B1 1
#define BIT_C1 2
#define BIT_D1 3
#define BIT_A2 4
#define BIT_B2 5
#define BIT_C2 6
#define BIT_A3 7
#define BIT_B3 8
#define BIT_C3 9
#define BIT_D3 10
#define BIT_A4 11
#define BIT_B4 12
#define BIT_C4 13




int a=0;   // A1
int b=0;   // B1
int c=0;   // C1
int d=0;   // D1
int x=0;   // counter
int a1=0;   // A2
int b1=0;   // B2
int c1=0;   // C2
int x1=0;   // counter
int a2=0;   // A3
int b2=0;   // B3
int c2=0;   // C3
int d2=0;   // D3
int x2=0;   // counter
int a3=0;   // A4
int b3=0;   // B4
int c3=0;   // C4




int new_a=0;
int new_b=0;
int new_c=0;
int new_d=0;
int new_a1=0;
int new_b1=0;
int new_c1=0;
int new_a2=0;
int new_b2=0;
int new_c2=0;
int new_d2=0;
int new_a3=0;
int new_b3=0;
int new_c3=0;




void setup(){
  
  pinMode(BIT_A1,OUTPUT);    //set all pin in OUTPUT mode
  pinMode(BIT_B1,OUTPUT);
  pinMode(BIT_C1,OUTPUT);
  pinMode(BIT_D1,OUTPUT);
  pinMode(BIT_A2,OUTPUT);
  pinMode(BIT_B2,OUTPUT);
  pinMode(BIT_C2,OUTPUT);
  pinMode(BIT_A3,OUTPUT);
  pinMode(BIT_B3,OUTPUT);
  pinMode(BIT_C3,OUTPUT);
  pinMode(BIT_D3,OUTPUT);
  pinMode(BIT_A4,OUTPUT);
  pinMode(BIT_B4,OUTPUT);
  pinMode(BIT_C4,OUTPUT);  
}


void loop(){


do
{
  do
{
do
{


 delay(1000);                
  if  (!a) new_a=1; 
  else new_a=0;
  if ((b && !a) || (!b && a && !d)) new_b=1;
  else new_b=0;
 if ((!c && b && a) || (c && !a) || ( c && !b)) new_c=1;
 else new_c=0;
 if ((!d && c && b && a) || (d && !a)) new_d=1;
 else new_d=0;


  
 a=new_a;
 b=new_b;  
 c=new_c;
 d=new_d; 
  


 if(a==1) digitalWrite(BIT_A1,HIGH);
 else digitalWrite(BIT_A1,LOW);
 if(b==1) digitalWrite(BIT_B1,HIGH);
 else digitalWrite(BIT_B1,LOW);
 if(c==1) digitalWrite(BIT_C1,HIGH);
 else digitalWrite(BIT_C1,LOW);
 if(d==1) digitalWrite(BIT_D1,HIGH);
 else digitalWrite(BIT_D1,LOW);
  
  
 x = x + 1; 


} while (x < 10);
x=0;                
  if  (!a1) new_a1=1; 
  else new_a1=0;
  if ((b1 && !a1) || (!b1 && a1 && !c1)) new_b1=1;
  else new_b1=0;
  if ((!c1 && b1 && a1) || (c1 && !a1 && !b1)) new_c1=1;
  else new_c1=0;
 
  
  a1=new_a1;
  b1=new_b1;  
  c1=new_c1;
  


  if(a1==1) digitalWrite(BIT_A2,HIGH);
  else digitalWrite(BIT_A2,LOW);
  if(b1==1) digitalWrite(BIT_B2,HIGH);
  else digitalWrite(BIT_B2,LOW);
  if(c1==1) digitalWrite(BIT_C2,HIGH);
  else digitalWrite(BIT_C2,LOW);
  
  x1 = x1 + 1;


} while (x1 < 6);

x1=0;
  if  (!a2) new_a2=1; 
  else new_a2=0;
  if ((b2 && !a2) || (!b2 && a2 && !d2)) new_b2=1;
  else new_b2=0;
  if ((!c2 && b2 && a2) || (c2 && !a2) || ( c2 && !b2)) new_c2=1;
  else new_c2=0;
  if ((!d2 && c2 && b2 && a2) || (d2 && !a2)) new_d2=1;
  else new_d2=0;


  
  a2=new_a2;
  b2=new_b2;  
  c2=new_c2;
  d2=new_d2; 
  


  if(a2==1) digitalWrite(BIT_A3,HIGH);
  else digitalWrite(BIT_A3,LOW);
  if(b2==1) digitalWrite(BIT_B3,HIGH);
  else digitalWrite(BIT_B3,LOW);
  if(c2==1) digitalWrite(BIT_C3,HIGH);
  else digitalWrite(BIT_C3,LOW);
  if(d2==1) digitalWrite(BIT_D3,HIGH);
  else digitalWrite(BIT_D3,LOW);
  
  
  x2 = x2 + 1;


} while (x2 < 10);


x2=0;


if  (!a3) new_a3=1; 
else new_a3=0;
if ((b3 && !a3) || (!b3 && a3 && !c3)) new_b3=1;
else new_b3=0;
if ((!c3 && b3 && a3) || (c3 && !a3 && !b3)) new_c3=1;
else new_c3=0;


  
a3=new_a3;
b3=new_b3;  
c3=new_c3;



if(a3==1) digitalWrite(BIT_A4,HIGH);
else digitalWrite(BIT_A4,LOW);
if(b3==1) digitalWrite(BIT_B4,HIGH);
else digitalWrite(BIT_B4,LOW);
if(c3==1) digitalWrite(BIT_C4,HIGH);
else digitalWrite(BIT_C4,LOW);
}


Om het filmpje wat sneller te laten lopen heb ik wat met de delay gespeeld, eerst is delay 100, daarna 10.

Het ziet er dan als volgt uit:


zaterdag 11 februari 2012

Arduino - Experiment 3b - BCD naar 7-segments display

En van experiment 3a is het een kleine stap naar 3b. Wanneer de arduino van 0 t/m 9 telt in BCD code kunnen we een 74LS47 gebruiken om met de BCD code de juiste segmenten aan te sturen van het display.

Onderstaande schema's geven weer hoe h.e.e.a. aangesloten moet worden:

 En dat ziet er dan als volgt uit:



Zoals je misschien opgevallen is gebruikt de 74LS47 voor de 6 en de 9 één segment minder dan in experiment 2b.

donderdag 9 februari 2012

Arduino - Experiment 3a - BCD teller

Het vorige experiment was een beetje omslachtig, dat kan natuurlijk veel beter. Voordat we naar Arduino - Experiment 3b - BCD naar 7-segments display gaan, maken we eerst een BCD teller van 0 t/m 9.


De waarheidstabel hiervoor is:


D
C
B
A
Decimaal
0
0
0
0
0
0
0
0
1
1
0
0
1
0
2
0
0
1
1
3
0
1
0
0
4
0
1
0
1
5
0
1
1
0
6
0
1
1
1
7
1
0
0
0
8
1
0
0
1
9


De code hiervoor is als volgt:

/*
Binary Counter with four LED's

The circuit:
* 4 LED's
       * 1 220ohm resistor
       
Created 09/02/2012
By Reinoud de Lange (reinouddl@gmail.com)
adopted some code from Vito Magnanimo

*/

#define LED1 0    // pin of LED1
#define LED2 1    // pin of LED2
#define LED3 2    // pin of LED3

#define LED4 3    // pin of LED4

int a=0;   // state of LED1
int b=0;   // state of LED2
int c=0;   // state of LED3

int d=0;   // state of LED4

//there are auxiliary variables
int new_a=0;  // used to store new LED1 state
int new_b=0;  // used to store new LED2 state
int new_c=0;  // used to store new LED3 state

int new_d=0;  // used to store new LED4 state

void setup(){
 
 pinMode(LED1,OUTPUT);    //set all pin in OUTPUT mode
 pinMode(LED2,OUTPUT);
 pinMode(LED3,OUTPUT);

 pinMode(LED4,OUTPUT);
 
}
void loop(){
 
 delay(2000);   // wait 2s before to begin every cicle
                // this is useful because if there isnt any
                // delay we will not see anything
               
 if  (!a) new_a=1;
 else new_a=0;
 if( (b && !a) || (!b && a && !d)) new_b=1;
 else new_b=0;
 if( (!c && b && a) || (c && !a) || ( c && !b)) new_c=1;

 else new_c=0;
 if( (!d && c && b && a) || (d && !a)) new_d=1;
 else new_d=0;

 
 a=new_a;   //now new value can be stored in the state variables
 b=new_b;  
 c=new_c;

 d=new_d;
 
 //turn ON or turn OFF  LED 1 2 3 4
 if(a==1) digitalWrite(LED1,HIGH);
 else digitalWrite(LED1,LOW);
 if(b==1) digitalWrite(LED2,HIGH);
 else digitalWrite(LED2,LOW);
 if(c==1) digitalWrite(LED3,HIGH);
 else digitalWrite(LED3,LOW);
 if(d==1) digitalWrite(LED4,HIGH);
 else digitalWrite(LED4,LOW);
}


En dat ziet er dan als volgt uit:



Hierna kunnen we met de BCD to 7-segment driver het display aansturen. Maar dat is voor het volgende experiment: "Arduino - Experiment 3b - BCD naar 7-segments display".