#include "SevSeg.h"
#include <Wire.h> // Include Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include library for I2C LCD
#define SEG_A 2
#define SEG_B 3
#define SEG_C 4
#define SEG_D 5
#define SEG_E 6
#define SEG_F 7
#define SEG_G 8
#define NUM_DIGITS 1
#define DIGIT1_PIN 10
#define POTENTIOMETER_PIN A0
SevSeg sevseg;
int previousValue = -1;
int frozenValue = -1;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16x2 display
int receivedNumber = 0; // Variable to store the received number from Arduino R
void receiveEvent(int numBytes) {
if (numBytes == 1) {
receivedNumber = Wire.read(); // Read the received number
}
}
void setup() {
byte numDigits = NUM_DIGITS;
byte digitPins[] = {DIGIT1_PIN};
byte segmentPins[] = {SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G};
bool resistorsonSegments = true;
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins, resistorsonSegments);
sevseg.setBrightness(100);
Serial.begin(9600);
Wire.begin(); // Initialize I2C communication
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
Wire.begin(8); // Set Arduino D as slave with address 8
Wire.onReceive(receiveEvent); // Register the receive event
}
void loop() {
int analogValue = analogRead(POTENTIOMETER_PIN);
int outputValue = map(analogValue, 0, 1023, -1, 9);
if (outputValue < previousValue) {
sevseg.setChars("-");
sevseg.refreshDisplay();
delay(500);
// Send the output value via I2C
Wire.beginTransmission(8); // Address of Arduino D
Wire.write(outputValue);
Wire.endTransmission();
}
if (outputValue == -1) {
sevseg.setChars("-");
sevseg.refreshDisplay();
frozenValue = previousValue;
Serial.print("Previous frozen value: ");
Serial.println(frozenValue);
Serial.println(frozenValue);
}
if (outputValue != -1 && outputValue > previousValue) {
sevseg.setNumber(outputValue);
sevseg.refreshDisplay();
}
previousValue = outputValue;
// Display the received number on the LCD
lcd.setCursor(0, 0);
lcd.print("Received Number:");
lcd.setCursor(0, 1);
lcd.print(receivedNumber);
// Wait for 0.25 seconds
delay(250);
// Display the output from the "Warm-up Coding Problem" on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Warm-up Output:");
lcd.setCursor(0, 1);
lcd.print("YOUR_OUTPUT_HERE"); // Replace "YOUR_OUTPUT_HERE" with the actual output
delay(2000); // Wait for 2 seconds before repeating the process
}