Percobaan 2 Kondisi 7
Setiap 3 switch yang aktif akan melakukan counting 9 - 0 pada digit 1 dan 2 secara berseling
- Rangkailah semua komponen
- Buat program di aplikasi arduino IDE
- Setelah selesai, masukkan program ke arduino
- Jalankan program pada simulasi dan lakukan sesuai kondisi
2. Hardware dan diagram blok
[Kembali]
a. hardware
b. Digram Blok
3. Rangkaian Simulasi dan Prinsip kerja
[Kembali]
4. FlowChart
[Kembali]
a. Listing Program
Master Code
//Master Arduino
#include<SPI.h> //Library for SPI
int dip[] = {2,3,4,5,6,7,8,9};
int dipvalue[] = {};
void setup (){
Serial.begin(9600); //Starts Serial Communication at Baud Rate 115200
for(int i = 0; i < 8; i++){
pinMode(dip[i], INPUT_PULLUP);
}
SPI.begin(); //Begins the SPI commnuication
SPI.setClockDivider(SPI_CLOCK_DIV8); //Sets clock for SPI communication at 8 (16/8=2Mhz)
digitalWrite(SS,HIGH); // Setting SlaveSelect as HIGH (So master doesnt connnect with slave)
}
void loop(void) {
byte Mastersend;
int x = 1;
int activeSwitches = 0; // variabel untuk menghitung jumlah saklar yang aktif
// Membaca status saklar dan menghitung berapa yang aktif
for (int i = 0; i < 8; i++) {
dipvalue[i] = digitalRead(dip[i]);
if (dipvalue[i] == LOW) {
x = dip[i];
activeSwitches++;
}
}
if (activeSwitches == 3) {
digitalWrite(SS, LOW);
Mastersend = 3;
Serial.println(Mastersend);
SPI.transfer(Mastersend);
delay(100);
}
}
Slave Code
#include <SPI.h>
const int segmentPins[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
volatile boolean received = false;
volatile byte Slavereceived;
int index;
int dg1 = A0;
int dg2 = A1;
void displayCharacter(int ch) {
byte patterns[10][7] = {
{0, 0, 0, 0, 0, 0, 1}, // 0
{1, 0, 0, 1, 1, 1, 1}, // 1
{0, 0, 1, 0, 0, 1, 0}, // 2
{0, 0, 0, 0, 1, 1, 0}, // 3
{1, 0, 0, 1, 1, 0, 0}, // 4
{0, 1, 0, 0, 1, 0, 0}, // 5
{0, 1, 0, 0, 0, 0, 0}, // 6
{0, 0, 0, 1, 1, 1, 1}, // 7
{0, 0, 0, 0, 0, 0, 0}, // 8
{0, 0, 0, 0, 1, 0, 0} // 9
};
if ((ch >= 0 && ch <= 9)) {
// Get the digit index (0-9) from the character
int index = ch;
// Write the pattern to the segment pins
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], patterns[index][i]);
}
}
}
void setup() {
Serial.begin(9600);
for (int i = 0; i < 8; i++) {
pinMode(segmentPins[i], OUTPUT);
}
pinMode(dg1, OUTPUT);
pinMode(dg2, OUTPUT);
SPCR |= _BV(SPE); // Turn on SPI in Slave Mode
SPI.attachInterrupt(); // Interrupt ON is set for SPI communication
}
ISR (SPI_STC_vect) { // Interrupt routine function
Slavereceived = SPDR; // Value received from master is stored in variable Slavereceived
received = true; // Sets received as True
}
void loop() {
if (received) { // Logic to SET LED ON OR OFF depending upon the value received from master
Serial.println(Slavereceived);
if (Slavereceived == 3) {
for(int i=9;i>=0;i--){
if(i % 2 == 0 ){
digitalWrite(dg1,LOW);
digitalWrite(dg2,HIGH);
displayCharacter(i);
delay (100);
}
else{
digitalWrite(dg1, HIGH);
digitalWrite(dg2, LOW);
displayCharacter(i);
delay (100);
}
}
}
received = false; // Reset received flag
}
}
b. Flowchart
Setiap 3 switch yang aktif akan melakukan counting 9 - 0 pada digit 1 dan 2 secara berseling
6. Video Simulasi
[Kembali]
7. Download File
[Kembali]
Tidak ada komentar:
Posting Komentar