I am trying to connect my Adafruit MotorHat to my Ardiuno Uno, but it won't locate a i2c address and it wont run my motors. Here is my code:
`#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *motor4 = AFMS.getMotor(4);
int photocellPin = A0; // Mini photocell connected to analog pin A0
int threshold1 = 90; // Threshold value for trash
int threshold2 = 270; // Threshold value for opaque
const int led1 = 4; // LED pin for threshold 1
const int led2 = 3; // LED pin for threshold 2
const int led3 = 2; // LED pin for threshold 3
const int sensor_pin = A0; // Sensor pin
int sensor; // Sensor reading
void setup() {
Serial.begin(9600); // Initialize Serial communication
AFMS.begin(); // Create motor shield object
motor2->setSpeed(255); // Set initial speed for motors
motor3->setSpeed(255);
motor4->setSpeed(255);
pinMode(led1, OUTPUT); // Set LED pins as output
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop() {
int photocellValue = analogRead(photocellPin); // Read photocell value
Serial.print("Photocell Value: ");
Serial.println(photocellValue);
sensor = analogRead(sensor_pin); // Read sensor value
Serial.print("Sensor Value: ");
Serial.println(sensor);
// Control the linear actuators based on photocell value
if (photocellValue > threshold2) {
Serial.println("Object is Translucent - Extending First Actuator");
motor2->run(FORWARD); // Extend first actuator
motor3->run(BACKWARD); // Keep second actuator forward
motor4->run(BACKWARD); // Keep third actuator forward
} else if (photocellValue > threshold1) {
Serial.println("Object is Opaque - Extending Second Actuator");
motor2->run(BACKWARD); // Keep first actuator forward
motor3->run(FORWARD); // Retract second actuator
motor4->run(BACKWARD); // Keep third actuator forward
} else {
Serial.println("Object is Trash - Extending Third Actuator");
motor2->run(BACKWARD); // Keep first actuator forward
motor3->run(BACKWARD); // Keep second actuator forward
motor4->run(FORWARD); // Retract third actuator
}
// Control LEDs based on sensor values
if (sensor < threshold1) {
Serial.println("RED LED");
digitalWrite(led1, HIGH); // Turn on LED 1 if light levels drop too low
digitalWrite(led2, LOW); // Turn off LED 2
digitalWrite(led3, LOW); // Turn off LED 3
} else if (sensor >= threshold1 && sensor < threshold2) {
Serial.println("CLEAR LED");
digitalWrite(led1, LOW); // Turn off LED 1
digitalWrite(led2, HIGH); // Turn on LED 2
digitalWrite(led3, LOW); // Turn off LED 3
} else {
Serial.println("GREEN LED");
digitalWrite(led1, LOW); // Turn off LED 1
digitalWrite(led2, LOW); // Turn off LED 2
digitalWrite(led3, HIGH); // Turn on LED 3
}
delay(350); // Delay for stability
}`