Beginners Tutorials with sample codes - Part 2 CloudX

  • img
Project Details

1. 7-Segment 0 - 9 Counter with Button 

Brief Description

For this project, we’ll explore our 7-segment display further by interfacing a button-switch to its display.  Here, two buttons are used to increment and decrement the number on display respectively.

Wiring

 

Code

#include <CloudX\Segment.h>  

#define INCREASE 8 //digital pin 8 to INCREASE Button
#define DECREASE 9 //digital pin 9 to DECREASE Button

char i = 0;
char NumberOfDigit = 1;
char segmentDataPins[] = {1, 2, 3, 4, 5, 6, 7, NULL};
char segmentScanPins[] = 0;

setup(){
//setup here

pinMode(DECREASE, INPUT);
pinMode(INCREASE, INPUT);

Segment_setting(CCathode,NumberOfDigit,segmentScanPins,segmentDataPins);

loop(){
//Program here

//------------------------------------------------
//Execute this section of code, if DECREASE button
//is pressed.
//------------------------------------------------
if(readPin(DECREASE) == 0)
{
delayMs(250); //removes the possibility of
//switch bounce

i--; //reduce the value assigned
//to the i variable by one
}

//------------------------------------------------
//Execute this section of code, if DECREASE button
//is pressed.
//-------------------------------------------------

if(readPin(INCREASE) == 0)
{
delayMs(250); //removes the possibility of
//switch bounce

i++; //increases the value assigned to
//the i variable by one
}

//------------------------------------------------------
//The below conditions ensure i stays within 0-9 range.
//------------------------------------------------------
if(i == 255) i = 9; //if the value inside the i variable
//is 255, it changes it to 9

if(i == 10) i = 0; //if the value inside the i variable
//is 10, it changes it to 0


Segment_write(i, 1000); //writes current value in i the
//variable on 7-segment
}
}

 

Simulation

 

 

2. Count with Multiple 7 Segment Display Counter 

Brief Description

This Project demonstrates how to display data on multiple segments using the CloudX Segment library. In this example, we’ll be using two 7-segments to effectively illustrate that. 

Wiring

 

Code

#include <CloudX\Segment.h>  
#include <CloudX\conversion.h> //necessary for the integer to
//string conversion



char counter[2] = {'0', '0'};

setup() {
//setup here

char NumberOfDigit = 2; //sets number of 7-segment displays
//to be used

//---------------------------------------------------------
//Connect these CloudX pins to the Data Pins A,B,C,D,E,F,G
// Dp pins of the Display.
//---------------------------------------------------------
char segmentDataPins[] = {1, 2, 3, 4, 5, 6, 7, 8};

//---------------------------------------------------------
//Connect these CloudX pins to the Common Anode or Cathode
//of each 7-segment display.
//---------------------------------------------------------
char segmentScanPins[] = {9, 10};


//------------------------------------------------
//Initialize the 7 segment Display with these data
//------------------------------------------------
Segment_setting(CCathode, NumberOfDigit, segmentScanPins, segmentDataPins);

loop(){
//Program here

//------------------------------------------------------
//Calculate i/10 and add 48, (to convert it to a
//character), then load in counter[0].
//------------------------------------------------------
for(byte i = 0; i < 100; i++)
{
counter[0] = (i/10) + 48 ;
counter[1] = (i%10) + 48 ; //loads this
//calculation into
//counter[1]

Segment_writeText(counter, 1000); //displays the
//content in
//counter array
//on 7segment
}


for (byte i = 99; i > 0; i--)
{

//-------------------------------------------------
//Convert the value in the i variable (input) to a
//string (output) in base 10.
//-------------------------------------------------
intTostr(counter, i, DEC);

//---------------------------------------------
//Execute this section of code, if the value in
//the i variable is less than 10.
//---------------------------------------------
if( i < 10)
{
counter[1] = counter[0]; //replaces counter[1]
//with the content of
//the counter[0]

counter[0] = '0'; //replaces counter[0]
//variable with the
//character ‘0’
}
Segment_writeText(counter, 1000); //displays the
//content in
//counter array
//for 1 sec
}

}
}

 

Simulation

 

 

 

3. 74HC595 Shift-Register with LED

Brief Description

This project is based on using the 74HC595 Shift-Register IC Chip to control an array of 8 LEDs. 

(This shift register chip works in a SIPO (Serial In Parallel Out) mode).

 

Meanwhile, for projects where you need more pins than what obtain on your CloudX board; with shift registers, you can address that issue easily. 

 

Wiring

 

Code

#include <CloudX/74HC595.h>  

char data, mask;
char i;

setup(){
//setup here

//Initialize the Shift Register
HC595_setting(1, 2, 3); //DAT,CLK,LAT

data = 0x80;

loop(){
//Program here

for(i = 0; i < 8; i++)
{
mask = data >>i; //shifts value in data to the
//right by “i” and load in mask

//--------------------------------------------------
//Load content of mask into shift register from the
//LSB up to the MSB.
//--------------------------------------------------
HC595_shiftOut(mask, LSBFIRST);
HC595_latch(); //changes shift output to match the
//data received(mask)

delayMs(500); //keeps shift output for half a
//second

//--------------------------------------------------
//Load content of mask into shift register from the
//MSB down to the LSB.
//--------------------------------------------------
HC595_shiftOut(mask, MSBFIRST);
HC595_latch(); //changes shift output to match the
//new data received
delayMs(500);
}
}
}
 

 

Simulation

4. LCD “Hello, World!”

Brief Description

This Project shows how to write data on a Liquid Crystal Display (LCD) using the CloudX LCD library.

 

The LCD is used in a popular 4-bit communication mode.

LED Pin-Out

 

Wiring

 

Code

#include <CloudX/LCD.h>  //loads the text from this header file  
//into your code

setup(){
//setup here

//--------------------------------------------------------
//The LCD is set up with their relevant pins connected to
//the MCU pins as follows.
//--------------------------------------------------------
LCD_setting(1, 2, 3, 4, 5, 6); //RS,EN,D4,D5,D6,D7

LCD_cmd(clear); //clears LCD display

loop(){
//Program here

//------------------------------------------
//Display "Hello, World!" on the 1st row and
//from the 2nd column of the LCD
//------------------------------------------
LCD_writeText(1, 2, "Hello, World!");

}
}
 

 

Simulation

 

You can drop comment below.

Share this with friends