Mini Tinkercad simulated Arduino Projects

Omar Rady
3 min readMay 3, 2020

This post display some of my mini Arduino projects I simulated using Autodesk Tinkercad, each project starts with a short description, Tinckercad simulation, and the code.

2-line LCD display and 4 buttons
pressing one button write the following words to the display:
button 1 is UP
button 2 is DOWN
button 3 is LEFT
button 4 is RIGHT
it writes it to the first row, then to the second, and then the second word goes to the first row, and the new word goes to the second, and so on.
This task was part of the university advanced C programming course.

// include the library code:
#include <LiquidCrystal.h>
#include <string.h>
// Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(13, 12, 8, 9, 10, 11);
#define ARRAYSIZE 2
String results[ARRAYSIZE];
//Define the funtion return
String record_flag = "";
String current_flag = record_flag;
//Push buttons
const int button1Pin = 7;
const int button2Pin = 6;
const int button3Pin = 5;
const int button4Pin = 4;
// intiatial Buttons states&flags,
//we keep the flags all the same size 5 charachters
//so we don't have left over charachters when overwriting
int button1State = 0;
String flag1="Up ";
int button2State = 0;
String flag2="Down ";
int button3State = 0;
String flag3="Left ";
int button4State = 0;
String flag4="Right";
void setup()
{
// serial monitor
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Clears the LCD screen
lcd.clear();
//Buttons input
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(button4Pin, INPUT);
}
//recording lates button flag function
String buttons()
{
//Read buttons state (pressed or not pressed?)
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
button3State = digitalRead(button3Pin);
button4State = digitalRead(button4Pin);

switch (button1State)
{
case 0:
// do noting
break;
case 1:
record_flag = flag1;
break;
}
switch (button2State)
{
case 0:
// do noting
break;
case 1:
record_flag = flag2;
break;
}
switch (button3State)
{
case 0:
// do noting
break;
case 1:
record_flag = flag3;
break;
}
switch (button4State)
{
case 0:
// do noting
break;
case 1:
record_flag = flag4;
break;
}

return record_flag;
}
//Dispaly records function
void display_buffer()
{
while(current_flag != record_flag)
{
current_flag = record_flag;
//If button pressed...
if (results[0] == NULL) {
results[0] = record_flag;
Serial.println("first line/result[0]: " + results[0]);
Serial.println("Second line/ result[1]: " + results[1]);
}
else if (results[1] == NULL){
results[1] = record_flag;
Serial.println("first line/result[0]: " + results[0]);
Serial.println("Second line/ result[1]: " + results[1]);
}
else{
results[0] = results[1];
Serial.println("result[0]: " + results[0]);
results[1] = record_flag;
Serial.println("result[1]: " + results[1]);
}

lcd.setCursor(0,0);
lcd.print(results[0]);
lcd.setCursor(0,1);
lcd.print(results[1]);
delay(100); //Small delay

}
}
void loop()
{
// Print an Opening message.
lcd.print("2 line LCD displ");
lcd.setCursor(0, 1);
lcd.print("ay and 4 buttons");
delay(3000); //Small delay
lcd.clear();
while(true)
{
buttons();
display_buffer();
}

}

Lighting Automation mini project with PIR sensor, Photoresistor, and a relay.

PIR sensor detects whether there is motion or not (someone entered the room for example) after motion is detected the photoresistor senses the light and based on pre-defined “darkness” threshold value it triggers the relay pin to turn it on (turn on the light). The project was meant to be part of a bigger AI home automation.

This project was part of my supervision of a BSc undergraduate student while I and one of the department engineers helping in establishing a new lab.

#define LAMP  7  // choose the pin for the RELAY
#define PIR 4 // choose the input pin (for PIR sensor)
void setup()
{
Serial.begin(9600);
pinMode(LAMP, OUTPUT); // declare lamp as output
pinMode(PIR,INPUT); // declare sensor as input
}

void loop()
{


int value_ldr = analogRead(A0); // read LDR value
int value_pir = digitalRead(PIR); // read input value
Serial.println(value_ldr);
Serial.println(value_pir);
if((300>value_ldr) && ( value_pir==HIGH) ){
digitalWrite(LAMP,1); // Turn ON the light
delay(6000);


}
else {

digitalWrite(LAMP,0); // Turn OFF the light

}
}

--

--

Omar Rady

Engineer interested in Tech / Travelling / Economy