Beginners Tutorials with sample codes - Part 6 CloudX

  • img
Project Details

1. Soil Moisture Sensor 

Brief Description

In this project, we’ll check out for the output from a Soil Moisture Sensor, and then use the data to switch OFF or ON a Relay connected to the Microcontroller. 

 

Wiring

 

Code

#define RELAY 1 
#define DO 2

setup(){
//setup here

pinMode(DO, INPUT);
pinMode(RELAY, OUTPUT);

loop(){
//Program here

//If the soil is dry, switch on the water irrigation system
//via the relay
if (readPin(DO))
digitalWrite(RELAY, HIGH);

//If the soil is wet, switch off the water irrigation
//system via the relay
else
digitalWrite(RELAY, LOW);

}
}
 

 

Simulation

 

 

2. CloudX Ohmmeter 

Brief Description

For this project, we’ll use the CloudX microcontroller to measure the resistance of a resistor with unknown resistance value. 

 

Wiring

 

Code

#include <CloudX/ADC.h>  
#include <CloudX/LCD.h>
#include <CloudX/conversion.h> //necessary for float to
//string conversion

#define R1 10000
#define Vin 5

float VoltageReading; //keeps decimal value inside
//VoltageReading variable
char *OhmDisplay;

//--------------------------------------------------------------//Ohm-Symbol Custom character code generated from the CloudX
// Generator
//--------------------------------------------------------------const byte ohmSymbol[] = {0x0E, 0x11, 0x11, 0x11, 0x0A, 0x0A, 0x11, 0x11};

setup(){
//setup here

Analog_setting();
LCD_setting(1, 2, 3, 4, 5, 6); //RS,EN,D4,D5,D6,D7
LCD_cmd(clear);
LCD_writeText(1, 1,"CLOUDX OHMMMETER");

loop(){
//Program here

//stores value of voltage across the unknown resistor
//inside VoltageReading variable
VoltageReading = Analog_read(A0);

delayMs(200); //waits for 200 milliseconds

//converts the digital value in VoltageReading variable
//to its analog value
VoltageReading = (VoltageReading * Vin / 1023);

//calculates the value of unknown resistor and stores it
//inside VoltageReading variable
VoltageReading = ((VoltageReading * R1) / (Vin - VoltageReading));

//converts float value in VoltageReading to string
OhmDisplay = floatTostr(VoltageReading);

OhmDisplay[6] = 0; //restricts string to 6 character
//text length
LCD_writeText(2, 1, "RES: "); //displasy text on LCD
LCD_writeTextCP(OhmDisplay); //shows string on LCD
//current cursor position

//------------------------------------------------------
//The below function-call writes the degree custom-
// character, (stored in CGRAM address 00),
//at the LCDs row2 col13.
//------------------------------------------------------
LCD_writeCustomChar(0, 2, 13, ohmSymbol);
//CGRAMAddress, row, col, customCharacter

delayMs(1000); //waits for 1 second

}
}
 

 

Simulation

 

 

3. Simple Smart Keypad Lock System 

Brief Description

In this project, we’ll create a simple keypad-controlled lock system that checks for Input from the user (using a keypad); and grants/denies access to the user depending on the information provided. 

Wiring

 

Code

#include <CloudX/Keypad.h>  
#include <CloudX/LCD.h>

#define keyPressIndicator 15
#define LED pin16


//-------------------------------------------------
//This section defines the relevant parameters for
//the keypad configuration.
//-------------------------------------------------
#define NumberOfRows 4 //sets keypad to four rows
#define NumberOfColumns 4 //sets keypad to four columns
char KeypadCharacters[NumberOfRows][NumberOfColumns] =
{
//key-layout of the Keypad
'1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'*', '0', '#', 'D'
};

char RowPins[NumberOfRows] = {7, 8, 9, 10};
char ColumnsPins[NumberOfColumns] = {11, 12, 13, 14};

char keys, times = 0, check = 0, i;

char Code[4] = {0,0,0,0}; //char array to store PIN Code
char Attempt[4] = {0,0,0,0}; //char array to store PIN Attempt

setup(){
//setup here

pinMode(16, OUTPUT); //configures the LED MCU pin as OUTPUT

//----------------------------------------------------------
//The keypad configuration -proper, is eventually executed
//by this function-call, utilizing the above already defined
//parameters.
//----------------------------------------------------------
Keypad_setting (PULLDOWNCOL, RowPins, ColumnsPins, NumberOfRows, NumberOfColumns, KeypadCharacters, keyPressIndicator);


LCD_setting (1, 2, 3, 4, 5, 6); //RS,EN,D4,D5,D6,D7
LCD_cmd(clear); //clears LCD

LCD_writeText(1, 4, " WELCOME ");
delayMs(1500);
LCD_writeText(1, 1, " INPUT NEW PIN ");

while(times <= 4) //repeats this loop till times variable
//is greater than 4
{
keys = Keypad_getKey(); //checks for Keypress and Key
//pressed store in keys
//variable


//Execute this line of code, if ‘*’ key is pressed
if(keys is '*')
{
LCD_cmd(clear);
times = 0; //clears times variable
LCD_writeText(1, 1, " ENTER PIN ");
//displays " ENTER PIN " on LCD
}

//Execute this line of code, if ‘#’ key is pressed and
//value in times variable is 4
if(keys is '#' && times is 4 )
{
LCD_cmd(clear);
LCD_writeText(1, 1, " NEW PIN STORED ");
LCD_writeText(2, 1, " SUCCESSFULLY ");
delayMs(2000); //shows content currently on LCD
//for 2 seconds
LCD_cmd(clear);
LCD_writeText(1, 1, " ENTER PIN ");
times++; //increments times variable by one
}

//Do this, if times is less than 4 and any keypad key
//other than ‘#’ or ‘*’ is pressed
if (times < 4 && ((keys >= 'A' && keys <= 'D') || (keys >= '0' && keys <= '9')))
{
LCD_write(2, times+1, keys ); //shows key on (2nd
//Row and times+1
//column) of LCD
Code[times] = keys; //stores the present Key Pressed
//in the Code char array
times++; //increments times variable by one
}
}
times = 0; //fills times variable with zero

loop(){
//Program here

while(times <= 4)
{
keys = Keypad_getKey();
if(keys is '*')
{
LCD_cmd(clear);
times = 0;
LCD_writeText(1, 1, " ENTER PIN ");
}
if(keys is '#' && times is 4 )
{
for (i = 0; i < 4 ; i++ ) //runs this loop four
//times
{
//compares characters in Code and Attempt
//array
if(Attempt[i] is Code[i])
check++; //does this, if they match
}

//Do this, if the value in check variable is 4.
if (check == 4)
{
LCD_cmd(clear);
LCD_writeText(1, 1, " ACCESS GRANTED ");

LED = ~LED; //toggles the LED ON/OFF
//@ Access Grant

delayMs(2000);
LCD_writeText(1, 1, " ENTER PIN ");
times = 0;
}

//Do this, if the value in check variable is NOT 4.
else
{
LCD_cmd(clear);
LCD_writeText(1, 1, " ACCESS DENIED ");
delayMs(1000);
LCD_writeText(1, 1, " ENTER PIN ");
}
for (i = 0; i < 4; i++) Attempt[i] = 0;
//removes item in the Attempt array
check = 0;
times = 0;
}
if (times < 4 && ((keys >= 'A'&& keys <= 'D') || (keys >= '0' && keys <= '9')))
{
LCD_write(2, times+1, keys );
Attempt[times] = keys; //stores Key Pressed in
//the Attempt char array
times++;
}
}

}
}

 

 

Simulation

 

 

4. Traffic Light with 7-Segment Countdown Timer 

Brief Description

In this project, we,ll create a Two-Way traffic light that operates based on a timer that displays on two 7-segment displays. 

Note:

pinSelect(1, HIGH) and pinSelect(1, LOW) can also be used to set the state of any CloudX digital I/O pin to ON/HIGH or OFF/LOW respectively just like the digitalWrite function. 

Wiring

Code

#include <CloudX/Segment.h>  
#include <CloudX/conversion.h>


#define Red1 11 //digital pin 11 connects to the Red LED on
//the Left

#define Yellow1 12 //digital pin 12 connects to the Yellow LED
//on the Left

#define Green1 13 //digital pin 13 connects to the Green LED
//on the Left

#define Red2 14 //digital pin 14 connects to the Red LED on
//the Right

#define Yellow2 15 //digital pin 15 connects to the Yellow LED
//on the Right

#define Green2 16 //digital pin 16 connects to the Green LED
//on the Right


char txt[3] = "30" ;

countDownTimer(); //creates a Function to countdown 30 seconds
//on display

yellowPrompt(); //handles amber-light effect




setup(){
//setup here

portMode(1, OUTPUT); //sets up digital I/O port 1 as OUTPUT
portMode(2, OUTPUT); //sets up digital I/O port 2 as OUTPUT
portWrite(1, OFF_ALL); //clears/turns off port 1
portWrite(2, OFF_ALL); //clears/turns off port 2

//--------------------------------------------
//This section configures the segment display.
//--------------------------------------------
byte NumberOfDigit = 2;
char segmentDataPins[] = {1, 2, 3, 4, 5, 6, 7, 8};
char segmentScanPins[] = {9, 10};
Segment_setting(CCathode, NumberOfDigit, segmentScanPins, segmentDataPins);


pinSelect(Red1, LOW); //turns off Red on left traffic
//light
pinSelect(Yellow1, LOW); //turns off Yellow on left traffic
//light
pinSelect(Green1, HIGH); //turns on Green on left traffic
//light
pinSelect(Red2, HIGH); //turns on Red on right traffic
//light
pinSelect(Yellow2, LOW); //turns off Yellow on right
//traffic light
pinSelect(Green2, LOW); //turns off Green on right traffic
//light

loop(){
//Program here

countDownTimer(); //runs commands in the countDownTimer
//function
pinSelect(Yellow1, HIGH); //turns on Yellow on left
//traffic light
pinSelect(Yellow2, HIGH); //turns on Yellow on right
//traffic light
yellowPrompt(); //runs commands in the yellowPrompt
//function
pinSelect(Red1,HIGH);
pinSelect(Green1, LOW);
pinSelect(Red2, LOW);
pinSelect(Green2, HIGH);
pinSelect(Yellow2, LOW);
pinSelect(Yellow1, LOW);
countDownTimer();
pinSelect(Yellow1, HIGH);
pinSelect(Yellow2, HIGH);
yellowPrompt();
pinSelect(Red1,LOW);
pinSelect(Green1, HIGH);
pinSelect(Red2, HIGH);
pinSelect(Green2, LOW);
pinSelect(Yellow2, LOW);
pinSelect(Yellow1, LOW);
countDownTimer();

}
}

countDownTimer() //creates a Function to countdown 30 seconds
//on display.
{
for(int i = 30; i >= 0; i--)
{
intTostr(txt, i, DEC); //converts value in i to a
//string in base 10

if((i >= 0) && (i <= 9)) //does this, if the value in i
//is less than 10
{
txt[1] = txt[0]; //replaces txt[1] with the
//content of the txt[0]

txt[0] = '0'; //replaces txt[0] with zer0
}
Segment_writeText(txt, 1000); //display the content in
//txt array for 1 sec
}
}

yellowPrompt() //handles amber-light effect
{
for(int i = 30; i >= 0; i--)
{
intTostr(txt, i, DEC);
if((i >= 0) && (i <= 9))
{
txt[1] = txt[0];
txt[0] = '0';
}
if(i <= 5 && i >= 0)
{
pinSelect(Yellow1, LOW); //turns off Yellow on left
//traffic light

pinSelect(Yellow2, LOW); //turns off Yellow on
//right traffic light
Segment_writeText(txt, 500);
pinSelect(Yellow1, HIGH); //turns on Yellow on left
//traffic light

pinSelect(Yellow2, HIGH); //turns on Yellow on
//right traffic light
}
Segment_writeText(txt, 1000);
}
}
 

 

Simulation

 

 

5. Visitor In/Out Counter 

Brief Description

In this project, we’ll create a system that counts the number of incoming and outgoing individuals in a Hall; whilst also keeping track of the net-total number of people in the place. 

Wiring

Code

#include <CloudX\LCD.h> 


#define visitorIN pin7 //connects VISITOR IN Button(sensor) to
//pin 7
#define visitorOUT pin8 //connects VISITOR OUT Button(sensor)
//to pin 8
#define greenLED pin9 //connects Green LED to pin 9
#define redLED pin10 //connects Red LED to pin 10

char IN[4] = {0, 0, 0, 0};
char OUT[4] = {0, 0, 0, 0};
char total[4] = {0, 0, 0, 0};

signed int inCount, outCount, totalCount;
unsigned char i;

setup(){
//setup here

pin7Mode = INPUT;
pin8Mode = INPUT;
pin9Mode = OUTPUT;
pin10Mode = OUTPUT;
greenLED = LOW;
redLED = LOW;

LCD_setting(1, 2, 3, 4, 5, 6); //RS,EN,D4,D5,D6,D7
LCD_cmd(clear);
LCD_writeText(1, 1, "VISITOR IN & OUT");
LCD_writeText(2, 6, "COUNTER");
delayMs(2000);
LCD_cmd(clear);

loop(){
//Program here

if(visitorOUT is HIGH) //does this, if a person leaves
//the hall
{
redLED = HIGH; //turns on Red LED
delayMs(200);
outCount++;

//control difference between variables
if (inCount < outCount) outCount--;
if(outCount > 9999) outCount = 0;

//gets 1st character of item IN the OUT array
OUT[0] = (outCount / 1000) + 48;

//gets second character of value
OUT[1] = ((outCount % 1000) / 100) + 48;

//gets third character of value
OUT[2] = ((outCount % 100) / 10) + 48;

//gets fourth character of value
OUT[3] = ((outCount % 100) % 10) + 48;

redLED = LOW; //turns off Red LED
}

//does this, if a person enters the hall
if(visitorIN is HIGH)
{
greenLED = HIGH;
delayMs(200);
inCount++;
if(inCount > 9999) inCount = 0;
IN[0] = (inCount / 1000) + 48;
IN[1] = ((inCount % 1000) / 100) + 48;
IN[2] = ((inCount % 100) / 10) + 48;
IN[3] = (inCount % 10) + 48;
greenLED = LOW;
}

//calculates number of persons IN Hall
totalCount = inCount - outCount;

//controls totalCount not to go below zero
if (totalCount <= 0) totalCount = 0;

total[0] = (totalCount / 1000) + 48;
total[1] = ((totalCount % 1000) / 100) + 48;
total[2] = ((totalCount % 100) / 10) + 48;
total[3] = (totalCount % 10) + 48;

LCD_writeText(1, 1, "IN:");

//displays content inside IN array
LCD_writeText(1, 4, IN);

LCD_writeText(1, 9, "OUT:");

//displays content inside OUT array
LCD_writeText(1, 13, OUT);

LCD_writeText(2, 1, "TOTAL: ");

//displays content inside total array
LCD_writeText(2, 8, total);

}
}
 

 

You can drop your comment below.

Share this with friends