So I have an array of 3 motors and depending on the data I receive via I2C, certain motors will vibrate accordingly. I'm taking in an int between 0 and 180. from 0-60 motor 1 vibrates. from 61-120 motor 2 vibrates. from 121-180 motor 3 vibrates.
The problem im getting is my arduino prints out the value received and every time it writes out that value then a 0. None of my motors vibrate. I believe its because the value gets reset to 0 but im not sure why that happens. any help would be appreciated. thanks! Here is my code:
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
pinMode( 3 , OUTPUT); // PWM: left motor control
pinMode( 5 , OUTPUT); // PWM: middle motor control
pinMode( 9 , OUTPUT); // PWM: right motor control
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
if((x >= 0) && (x <= 60)){ //motor 1
analogWrite(3 , 153);
delay(500);
analogWrite(3 , 0);
delay(500);
}
else if((x >= 61) && (x <= 120)){ //motor 2
analogWrite(5 , 153);
delay(500);
analogWrite(5 , 0);
delay(500);
}
else if((x >= 121) && (x <= 180)){ //motor 3
analogWrite(9 , 153);
delay(500);
analogWrite(9 , 0);
delay(500);
}
}