https://cinvintom.github.io/Kerala-IoT-Challenge/
"Kerala IoT Challenge 2021" is a program launched by Foxlab Makerspace in association with GTech - Group of Technology Companies in Kerala to mold IoT experts in Kerala.
Hello everyone , I am Cinvin Tom, a 3rd-year Computer Science and Engineering student from College of Engineering Chengannur, I am participating in this IoT challenge to learn more about the IoT and help others to learn IoT and complete the Kerala IoT Challenge 2021.
In level 1 we are using Arduino for the experiments, Arduino is an open-source electronics platform based on easy-to-use hardware and software. It's intended for anyone making interactive projects. I am using Arduino UNO and Arduino IDE from https://www.arduino.cc/en/software for level 1
Arduino Uno is an open-source microcontroller board developed by Arduino.cc. It has several advantages over conventional microcontrollers. It comes with pre-tested software and hardware libraries and has an integrated development environment (IDE). Also, it is less expensive & beginner-friendly.
we are starting our IoT journey by Hello World LED Blinking using the microcontroller Arduino UNO
- Arduino Uno Board
- USB Cable
- LED (Any Color) x 1 Nos
- 220 OHM Resistor X 1 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 2 Nos
int ledPin = 9; //for define digital pin 9
void setup() {
pinMode(ledPin, OUTPUT);//define ledPin as an OUTPUT
}
void loop() {
digitalWrite(ledPin, HIGH);//set LED on
delay(1000);//wait for 1000 millisecond or 1 second
digitalWrite(ledPin, LOW);//set the LED off
delay(1000);
}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/146659989-f970ace2-8ddc-40f1-a1d1-3a8c3bb18319.mp4" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>The LED is blinked with a time interval of 1 second
we have done the LED blinking experiment with one LED. Now its time for an upgrade, we use 3 LEDs with different colors to simulate the working of a Traffic light
- Arduino Uno Board
- USB Cable
- LED - Red M5 LED x 1 Nos - Yellow M5 LED x 1 Nos - Green M5 LED x 1 Nos
- 220 OHM Resistor X 3 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 4 Nos
int redled = 8; // initialize digital pin 8.
int yellowled =5; // initialize digital pin 5.
int greenled = 2; // initialize digital pin 2.
void setup()
{
pinMode(redled, OUTPUT); // set the pin with red LED as “output”
pinMode(yellowled, OUTPUT); // set the pin with yellow LED as “output”
pinMode(greenled, OUTPUT); // set the pin with green LED as “output”
}
void loop()
{
digitalWrite(greenled, HIGH); // turn on green LED
delay(5000); // wait 5 seconds
digitalWrite(greenled, LOW); // turn off green LED
for(int i=0;i<3;i++) // blinks for 3 times
{
delay(500); // wait 0.5 second
digitalWrite(yellowled, HIGH); // turn on yellow LED
delay(500);
digitalWrite(yellowled, LOW); // turn off yellow LED
}
delay(500);
digitalWrite(redled, HIGH); // turn on red LED
delay(5000);
digitalWrite(redled, LOW); // turn off red LED
}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/147979458-9a51dab9-e971-4ce1-92a9-4c6d830328d5.mp4" frameborder="0" allow="autoplay;" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>The LED has blinked alternatively like a traffic light
Let's create something different than just blinking. Now, we are going to do an LED chasing Effect which often we see in billboards composed of colorful LEDs.Let us simulate the LED chasing effect.
- Arduino Uno Board
- USB Cable
- LED (Any Color) x 6 Nos
- 220 OHM Resistor X 6 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 7 Nos
int BASE = 2; // the I/O pin for the first LED
int NUM = 6; // number of LEDs
void setup()
{
for(int i= BASE; i < BASE + NUM; i++)
{
pinMode(i,OUTPUT); // set I/O pins as output
}
}
void loop()
{
for(int i = BASE; i < BASE+NUM; i++)
{
digitalWrite(i, LOW); // set I/O pins as “low”, turn off LEDs one by one.
delay(200); // delay
}
for(int i = BASE; i < BASE+NUM; i++)
{
digitalWrite(i, HIGH); // set I/O pins as “high”, turn on LEDs one by one.
delay(200);
}
}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/147984271-15c6ac84-86ce-4f27-a67e-bfb62ce1d0c4.mp4" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>The LEDs are turning ON and OFF one by one giving a chasing effect.
In this experiment we are going to learn how to add a Button Switch as INPUT in the Arduino circuit.
The Button is called a pushbutton, tactile button, or momentary switch, It is a basic and simple component used in Arduino to give an INPUT.
Button switch usually has four pins. BUT These pins are internally connected in pairs, So we only need to use TWO of the four pins, WHICH ARE NOT INTERNALLY CONNECTED
When using in an Arduino circuit One button's pin is connected to VCC or GND. The other pin is connected to an Arduino pin. By reading the state of Arduino's pin(configured as INPUT pin), we can detect the button is PRESSED or NOT
- Arduino Uno Board
- USB Cable
- Red M5 LED x 1 Nos
- 220 OHM Resistor X 1 Nos
- 10k OHM Resistor X 1 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 6 Nos
int ledpin=11;// initialize pin 11
int inpin=7;// initialize pin 7
int val;// define val
void setup()
{
pinMode(ledpin,OUTPUT);// set LED pin as “output”
pinMode(inpin,INPUT);// set button pin as “input”
}
void loop()
{
val=digitalRead(inpin);// read the level value of pin 7 and assign to val
if(val==LOW)// check if the button is pressed, if yes, turn on the LED
{
digitalWrite(ledpin,LOW);
}
else
{
digitalWrite(ledpin,HIGH);
}
}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/149671162-1ec5bcbf-2e64-4d7f-8e36-45534cfa6994.mp4" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>The LED turns ON when the Button switch is PRESSED and turns OFF when the button is released.
In this experiment we are going to learn how to add a Buzzer as OUTPUT in the Arduino circuit for sound feedback.
An Arduino buzzer is also called a piezo buzzer. It’s a tiny speaker that you can connect to an Arduino board and make it sound the tone we set. The buzzer generates sound based on the reverse of the piezoelectric effect. Buzzers are used to make beep alarms and tones. They were designed to help technicians integrate their alarm systems or other automated systems for sound feedback.
- Arduino Uno Board
- USB Cable
- Buzzer x 1 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 2 Nos
int buzzer=8;// initialize digital IO pin that controls the buzzer
void setup()
{
pinMode(buzzer,OUTPUT);// set pin mode as “output”
}
void loop()
{
digitalWrite(buzzer,HIGH);// produce sound
}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/149672340-460ec5e3-e8bc-4bc4-b402-690c4b059678.mp4" frameborder="1" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>The buzzer starts making a sound when the Arduino is connected to the power supply. When the RESET switch has pressed the buzzer restarted after 1 sec.
In this experiment we are going to learn how to add an RGB LED as OUTPUT in the Arduino circuit.
The RGB led consists of three different led’s, from the name you can guess that these LEDs are red, green, and blue. We can obtain many other colors by mixing up these colors. The Arduino has an analog write function which will help us in obtaining different colors for Arduino RGB led.
- Arduino Uno Board
- USB Cable
- RGB LED x 1 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 5 Nos
int redpin = 11; //select the pin for the red LED
int bluepin = 10; //select the pin for the blue LED
int greenpin = 9; //select the pin for the green LED
int val;
void setup()
{
pinMode(redpin,OUTPUT);
pinMode(redpin,OUTPUT);
pinMode(redpin,OUTPUT);
Serial.begin(9600);//to begin serial communication
}
void loop()
{
for(val=0;val<255;val++)
{
analogWrite(11, val);
analogWrite(10, 255-val);
analogWrite(9, 128-val);
delay(1);
}
Serial.println(val, DEC);
}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/149674754-a64eba40-5a82-42ab-af19-88cd843172a8.mp4" frameborder="1" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>The RGB LED started blinking in different colors.
In this experiment, we are going to learn how to use an LDR light sensor to create an Arduino circuit that gives a response to variation in light.
An LDR is a component that has a (variable) resistance that changes with the light intensity that falls upon it. This allows them to be used in light sensing circuits. Light Dependent Resistors (LDR) is also called photoresistors. They are made of high-resistance semiconductor material.
when light falls on the LDR then the resistance decreases and increases in the dark. When it is placed in a dark room then the resistance will be infinite(high) and act as an open circuit. As the intensity of light increases, the resistance will decrease and act as a closed circuit.
- Arduino Uno Board
- USB Cable
- Red M5 LED x 1 Nos
- 10KΩ Resistor x 1 Nos
- 220Ω Resistor x 1 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 5 Nos
int potpin = 0; // initialize analog pin 0, connected with photovaristor
int ledpin = 11; // initialize digital pin 11
int val = 0; // initialize variable val
void setup()
{
pinMode (ledpin,OUTPUT); // set digital pin 11 as “output”
Serial.begin(9600); //to begin serial communication
}
void loop()
{
val = analogRead(potpin); // read the value of the sensor and assign it to val
Serial.println(val); // display the value of val
analogWrite(ledpin,val/4); // set up brightness(maximum value 255)
delay(10); // wait for 0.01s
}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/151670993-696ff0b8-09d6-4fdb-b587-78ca38bed8ee.mp4" frameborder="1" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>The brightness of the LED reduced as the intensity of the light source increased. When the light source is removed, the LED lights up in full brightness.
In this experiment, we are going to learn how to use an Infrared Receiver (IR) for detecting Flame.
the receiver outputs a code to uniquely identify the infrared signal that it receives.
- Arduino Uno Board
- USB Cable
- Infrared Receiver x 1 Nos
- 10KΩ Resistor x 1 Nos
- Buzzer x 1 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 5 Nos
int flame=0;// select analog pin 0 for the sensor
int Beep=9;// select digital pin 9 for the buzzer
int val=0;// initialize variable
void setup()
{
pinMode(Beep,OUTPUT);// set LED pin as “output”
pinMode(flame,INPUT);// set buzzer pin as “input”
Serial.begin(9600);// set baud rate at “9600”
}
void loop()
{
val=analogRead(flame);// read the analog value of the sensor
Serial.println(val);// output and display the analog value
if(val>=20)// when the analog value is larger than 20, the buzzer will buzz
{
digitalWrite(Beep,HIGH);
}
else
{
digitalWrite(Beep,LOW);
}
delay(500);
}
<iframe width="400" height="300" src="https://user-images.githubusercontent.com/65575529/151684443-2c4f57e9-4e8f-44bc-b2cf-f835c27b82b9.mp4" frameborder="1" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>When the FLAME source came near the IR sensor the buzzer started to make a sound.
In this experiment, we are going to learn how to use an LM35 Temperature Sensor for measuring room temperature.
LM35 temperature sensor can produce different voltage by different temperature
When temperature is 0 ℃, it outputs 0V;
if increasing 1 ℃, the output voltage will increase 10 mv.
The output temperature is 0℃~100℃,
the conversion formula is as follows:
LM35 is a widely used temperature sensor with many different package types. At room temperature, it can achieve the accuracy of ±1/4°C without additional calibration processing.
- Arduino Uno Board
- USB Cable
- LM35 x 1 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 3 Nos
int potPin = 0; // initialize analog pin 0 for LM35 temperature sensor
void setup()
{
Serial.begin(9600);
}
void loop()
{
int val; // define variable
int dat; // define variable
val = analogRead(potPin); // read the analog value of the sensor and assign it to val
dat = (125*val)>>8; // temperature calculation formula
Serial.print("Tep"); // output and display characters beginning with Tep
Serial.print(dat); // output and display value of dat
Serial.println("C"); // display “C” characters
delay(500); // wait for 0.5 second
}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/151684495-220477f0-c9b4-4df6-b758-d10d72f11e60.mp4" frameborder="1" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>When the Arduino starts working the temperature of the room is displayed on the serial monitor in celsius.
In this experiment, we are going to learn how to use a remote to control the circuit.
The signal from the infrared remote controller is a series of binary pulse code. To avoid the other infrared signal interference during the wireless transmission, the signal is pre-modulated at a specific carrier frequency and then send out by an infrared emission diode. The infrared receiving device needs to filter out other waves and receive signals at that specific frequency and to modulate it back to binary pulse code, known as demodulation.
The built-in receiver converts the light signal it received from the sender into feeble electrical signal. The signal will be amplified by the IC amplifier. After automatic gain control, band-pass filtering, demodulation, wave shaping, it returns to the original code. The code is then input to the code identification circuit by the receiver's signal output pin.
- Arduino Uno Board
- USB Cable
- Infrared Remote Controller(We can use TV Remote or any other remote) x 1 Nos
- Infrared Receiver
- LED - Red M5 LED x 1 Nos - Yellow M5 LED x 1 Nos - Green M5 LED x 1 Nos
- 220Ω Resistor x 3 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 8 Nos
#include <IRremote.h>
int RECV_PIN = 9;
int red = 5;
int green = 6;
int yellow = 3;
int val=85;
bool r=false;
bool w=false;
bool y=false;
bool a=false;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
irrecv.enableIRIn();
irrecv.blink13(true);
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value,HEX);
delay(100);
if(results.value==0x8041A701)
{
r=!r;
digitalWrite(red, r);
}
else if(results.value==0x8041A702)
{
w=!w;
digitalWrite(green, w);
}
else if(results.value==0x8041A703)
{
y=!y;
digitalWrite(yellow, y);
}
else if(results.value==0x80412704)
{
if(r||w||y)
{
a=true;
r=false;
w=false;
y=false;
Serial.println("a=true");
}
a=!a;
digitalWrite(red, a);
digitalWrite(green, a);
digitalWrite(yellow, a);
if(a)
{
r=true;
w=true;
y=true;
}
}
irrecv.resume(); // Receive the next value
}
}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/151683786-cdffd466-a9c8-4b43-a6db-3faf24b1b38e.mp4" frameborder="1" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>When one key is pressed in the remote corresponding led turns ON. again the same key is pressed the LED turns OFF.
In this experiment, we are going to learn how to read analog value from Potentiometer.
a variable resistor with a third adjustable terminal. The potential at the third terminal can be adjusted to give any fraction of the potential across the ends of the resistor. Potentiometer made using plastic and having a small size is called Preset
- Arduino Uno Board
- USB Cable
- 10KΩ Potentiometer x 1 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 3 Nos
int potpin = 0; // initialize analog pin 0
int ledpin = 13; // initialize digital pin 13
int val=0; // define val, assign initial value 0
void setup()
{
pinMode (ledpin, OUTPUT); // set digital pin as “output”
Serial.begin(9600); // set baud rate at 9600
}
void loop()
{
digitalWrite(ledpin, HIGH); // turn on the LED on pin 13
delay(50); // wait for 0.05 second
digitalWrite(ledpin, LOW); // turn off the LED on pin 13
delay(50);
val = analogRead(potpin); // read the analog value of analog pin 0, and assign it to val
Serial.println(val); // display val’s value
}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/151683741-3aac358d-849f-4be4-aa80-061f24833a2d.mp4" frameborder="1" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>As the knob of the potentiometer turns the value displaying on the serial monitor is also changing.
In this experiment, we are going to learn how to add an 7 Segment Display as OUTPUT in the Arduino circuit.
7 segment display is basically a combination of 7 different LED arranged in a manner so that we can use their combination to produce numerical values from 0 to 9.
Some of them have the 8th segment which helps to display decimal points.
- Arduino Uno Board
- USB Cable
- 1-digit LED Segment Display x 1 Nos
- 220Ω Resistor x 8 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 9 Nos
int a=7;// set digital pin 7 for segment a
int b=6;// set digital pin 6 for segment b
int c=5;// set digital pin 5 for segment c
int d=10;// set digital pin 10 for segment d
int e=11;// set digital pin 11 for segment e
int f=8;// set digital pin 8 for segment f
int g=9;// set digital pin 9 for segment g
int dp=4;// set digital pin 4 for segment dp
void digital_0(void) // display number 5
{
unsigned char j;
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
digitalWrite(dp,LOW);
}
void digital_1(void) // display number 1
{
unsigned char j;
digitalWrite(c,HIGH);// set level as “high” for pin 5, turn on segment c
digitalWrite(b,HIGH);// turn on segment b
for(j=7;j<=11;j++)// turn off other segments
digitalWrite(j,LOW);
digitalWrite(dp,LOW);// turn off segment dp
}
void digital_2(void) // display number 2
{
unsigned char j;
digitalWrite(b,HIGH);
digitalWrite(a,HIGH);
for(j=9;j<=11;j++)
digitalWrite(j,HIGH);
digitalWrite(dp,LOW);
digitalWrite(c,LOW);
digitalWrite(f,LOW);
}
void digital_3(void) // display number 3
{digitalWrite(g,HIGH);
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(dp,LOW);
digitalWrite(f,LOW);
digitalWrite(e,LOW);
}
void digital_4(void) // display number 4
{digitalWrite(c,HIGH);
digitalWrite(b,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
digitalWrite(a,LOW);
digitalWrite(e,LOW);
digitalWrite(d,LOW);
}
void digital_5(void) // display number 5
{
unsigned char j;
digitalWrite(a,HIGH);
digitalWrite(b, LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e, LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
void digital_6(void) // display number 6
{
unsigned char j;
for(j=7;j<=11;j++)
digitalWrite(j,HIGH);
digitalWrite(c,HIGH);
digitalWrite(dp,LOW);
digitalWrite(b,LOW);
}
void digital_7(void) // display number 7
{
unsigned char j;
for(j=5;j<=7;j++)
digitalWrite(j,HIGH);
digitalWrite(dp,LOW);
for(j=8;j<=11;j++)
digitalWrite(j,LOW);
}
void digital_8(void) // display number 8
{
unsigned char j;
for(j=5;j<=11;j++)
digitalWrite(j,HIGH);
digitalWrite(dp,LOW);
}
void digital_9(void) // display number 5
{
unsigned char j;
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e, LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
void setup()
{
int i;// set variable
for(i=4;i<=11;i++)
pinMode(i,OUTPUT);// set pin 4-11as “output”
}
void loop()
{
while(1)
{
digital_0();// display number 0
delay(1000);// wait for 1s
digital_1();// display number 1
delay(1000);// wait for 1s
digital_2();// display number 2
delay(1000); // wait for 1s
digital_3();// display number 3
delay(1000); // wait for 1s
digital_4();// display number 4
delay(1000); // wait for 1s
digital_5();// display number 5
delay(1000); // wait for 1s
digital_6();// display number 6
delay(1000); // wait for 1s
digital_7();// display number 7
delay(1000); // wait for 1s
digital_8();// display number 8
delay(1000); // wait for 1s
digital_9();// display number 9
delay(1000); // wait for 1s
}}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/151683685-d111f43e-1073-457c-91a7-eaff375b8dcb.mp4" frameborder="1" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>As the power supply is connected in the circuit, the 7 segment display started to count from 0 to 9.
Create an automatic night lamp model using LDR and LED
- Arduino Uno Board
- USB Cable
- LDR module x 1 Nos
- LED x 1 Nos
- 220Ω resistor x 1 Nos
- 10kΩ resistor x 1 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 12 Nos
int potpin=0;// initialize analog pin 0, connected with photovaristor
int ledpin=11;// initialize digital pin 11,
int val=0;// initialize variable val
void setup()
{
pinMode(ledpin,OUTPUT);// set digital pin 11 as “output”
Serial.begin(9600);// set baud rate at “9600”
}
void loop()
{
val=analogRead(potpin);// read the value of the sensor and assign it to val
Serial.println(val);// display the value of val
if(val<=(255))
digitalWrite(ledpin,LOW);// set up brightness(maximum value 255)
else
digitalWrite(ledpin,HIGH);
delay(10);// wait for 0.01
}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/151692616-0d13c05c-1a18-4251-a976-fa7944d0c1e7.mp4" frameborder="1" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>When the room becomes dark LED turns ON automatically and turns OFF when dark is over.
Create a Digital Dice using 7 Segment Display and Push Button
- Arduino Uno Board
- USB Cable
- 1-digit LED Segment Display x 1 Nos
- 220Ω resistor x 8 Nos
- 10kΩ resistor x 1 Nos
- Breadboard
- Jumper Wires (Male to Male ) X 12 Nos
int button = 2; //specifying button
int a=7;// set digital pin 7 for segment a
int b=6;// set digital pin 6 for segment b
int c=5;// set digital pin 5 for segment c
int d=10;// set digital pin 10 for segment d
int e=11;// set digital pin 11 for segment e
int f=8;// set digital pin 8 for segment f
int g=9;// set digital pin 9 for segment g
int dp=4;// set digital pin 4 for segment dp
long num;
int buttonstate;
void digital_1(void) // display number 1
{
unsigned char j;
digitalWrite(c,HIGH);// set level as “high” for pin 5, turn on segment c
digitalWrite(b,HIGH);// turn on segment b
for(j=7;j<=11;j++)// turn off other segments
digitalWrite(j,LOW);
digitalWrite(dp,LOW);// turn off segment dp
}
void digital_2(void) // display number 2
{
unsigned char j;
digitalWrite(b,HIGH);
digitalWrite(a,HIGH);
for(j=9;j<=11;j++)
digitalWrite(j,HIGH);
digitalWrite(dp,LOW);
digitalWrite(c,LOW);
digitalWrite(f,LOW);
}
void digital_3(void) // display number 3
{digitalWrite(g,HIGH);
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(dp,LOW);
digitalWrite(f,LOW);
digitalWrite(e,LOW);
}
void digital_4(void) // display number 4
{digitalWrite(c,HIGH);
digitalWrite(b,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
digitalWrite(a,LOW);
digitalWrite(e,LOW);
digitalWrite(d,LOW);
}
void digital_5(void) // display number 5
{
unsigned char j;
digitalWrite(a,HIGH);
digitalWrite(b, LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e, LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
void digital_6(void) // display number 6
{
unsigned char j;
for(j=7;j<=11;j++)
digitalWrite(j,HIGH);
digitalWrite(c,HIGH);
digitalWrite(dp,LOW);
digitalWrite(b,LOW);
}
void setup()
{
int i;// set variable
for(i=4;i<=11;i++)
pinMode(i,OUTPUT);// set pin 4-11as “output”
}
void loop()
{
buttonstate = digitalRead(button);
if(buttonstate == HIGH)
{
num = random(1,7); //Generate random number from 1 to 6.
if (num == 1)
digital_1();// display number 1
if (num == 2)
digital_2();// display number 2
if (num == 3)
digital_3();// display number 3
if (num == 4)
digital_4();// display number 4
if (num == 5)
digital_5();// display number 5
if (num == 6)
digital_6();// display number 6
}
}
<iframe width="560" height="315" src="https://user-images.githubusercontent.com/65575529/151693045-b3247f5b-57ae-4514-b706-805f41e04b0d.mp4" frameborder="1" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>When the button is pressed a random number is displayed in the 7 segment display.