#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <Servo.h>
// Initialize variables
int x = 1; // Variable to store incoming
//integer from serial
char z; // Variable to store incoming
//character from I2C
int pot = 0; // Variable to store potentiometer
//value read from I2C
int Power = 11; // Pin number for Power
int PIN = 12; // Pin number for NeoPixel
int c = 1, v = 0; // Control variables for button state
//handling
int cont = 0; // Counter for button presses
#define NUMPIXELS 1 // Number of NeoPixels
Servo myservo; // Create Servo object
// Initialize NeoPixel with specified number of pixels and pin
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Wire.begin(); // Initialize I2C communication
Serial.begin(9600); // Initialize serial communication at
// 9600 baud rate
pixels.begin(); // Initialize NeoPixel library
pinMode(Power, OUTPUT); // Set Power pin as output
digitalWrite(Power, HIGH); // Set Power pin to HIGH to power on
myservo.attach(D2); // Attach servo to pin D2
}
void loop() {
delay(10); // Small delay for stability
// Request 1 byte of data from I2C device with address 2
Wire.requestFrom(2, 1);
if (Wire.available()) { // Check if data is available from I2C
z = Wire.read(); // Read the incoming byte
Serial.print("button "); // Print the received character
Serial.print(z);
}
// Request 1 byte of data from I2C device with address 1
Wire.requestFrom(1, 1);
if (Wire.available()) { // Check if data is available from I2C
pot = Wire.read(); // Read the incoming byte
Serial.print(" Pot "); // Print the potentiometer value
Serial.print(pot);
}
// Check if there's incoming data on serial port
if (Serial.available() >= 1) {
x = Serial.parseInt(); // Read the incoming integer
//from serial
Serial.println(x); // Print the integer to serial
}
myservo.write(pot); // Set servo position according to the
//potentiometer value
// Button press handling: increment counter on 'B' press
//and reset on 'N' press
if ((z == 'B') && (c == 1)) {
cont++;
v = 1;
c = 0;
}
if ((z == 'N') && (v == 1)) {
v = 0;
c = 1;
}
Serial.print(" cont "); // Print the counter value
Serial.println(cont);
// Change NeoPixel color based on the value of cont
if (cont == 1) {
pixels.clear(); // Clear previous color
pixels.setPixelColor(0, pixels.Color(255, 0, 0));
// Set color to Red
pixels.show(); // Update NeoPixel to show the color
}
if (cont == 2) {
pixels.clear(); // Clear previous color
pixels.setPixelColor(0, pixels.Color(0, 255, 0));
// Set color to Green
pixels.show(); // Update NeoPixel to show the color
}
if (cont == 3) {
pixels.clear(); // Clear previous color
pixels.setPixelColor(0, pixels.Color(0, 0, 255));
// Set color to Blue
pixels.show(); // Update NeoPixel to show the color
}
if (cont == 4) {
pixels.clear(); // Clear previous color
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
// Turn off NeoPixel
pixels.show(); // Update NeoPixel to show the color
cont = 0; // Reset counter
}
// Send the value of x to I2C device with address 2
//if x is between 4 and 6
if (x >= 4 && x <= 6) {
Wire.beginTransmission(2); // Begin I2C transmission
Wire.write(x); // Write the value of x
Wire.endTransmission(); // End the I2C transmission
}
}