Hi everyone,
My name is Parvin and I'm new to this forum. I'm building an I2C communication between 2 Arduinos. The master Arduino sends numbers 0 to 5 with 2 seconds between each message. in Arduino slave, if the message received is 3, it triggers a servo for 5 seconds. then stops and waits for the next message to come. the problem I'm facing right now is that the slave Arduino can trigger Servo for a couple of times. then it stops triggering. here is the code. I would be delighted if you can help me with the problem.
Cheers,
Parvin
#include <Servo.h>
int updateInterval;
unsigned long lastUpdate;
int i;
class Sweeper
{
Servo servo;
int posLength;
int p[120] = {-1}; // indicate servo angle array
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
public:
Sweeper() {
}
void sAssign(int interval, int pos[], int posNumber)
{
posLength = posNumber;
updateInterval = interval;
if ( p != -1) {
delete p;
}
for (int i = 0; i < posLength; i++) {
p = pos*;*
_ //Serial.println(pos*);
}
}
void Attach(int pin)
{
servo.attach(pin);
}
void Detach()
{
servo.detach();
}
void Update()
{
if ((millis() - lastUpdate) > updateInterval) // time to update*
* {
lastUpdate = millis();
servo.write(p);
i = (i + 1) % posLength;
//Serial.println(p);
}
}
};
#include <Wire.h>
int val;
int x;
boolean Active = false;
unsigned long ActiveTime = 5000;
int previousUpdate;
int servoPos[] = {0, 120, 180};
int servoPos1[] = {20, 120, 60, 180, 120};
Sweeper sweeper1;
Sweeper sweeper2;
void receiveEvent() {
while (1 < Wire.available()) { // loop through all but the last*
* char c = Wire.read(); // receive byte as a character*
* Serial.print(c); // print the character*
* }
x = Wire.read(); // receive byte as an integer*
* Serial.println(x); // print the integer*
}
void setup() {
* Wire.begin( 8 ); // join i2c bus with address #8*
* Wire.onReceive(receiveEvent); // register event*
* Serial.begin(9600); // start serial for output*
* lastUpdate = millis();
sweeper1.Attach(9);
//sweeper2.Attach( 8 );
sweeper1.sAssign(500, servoPos, 3);
//sweeper2.sAssign(500, servoPos1, 5);
}
void loop() {
if (x == 3) {
previousUpdate = millis();
Serial.println("send to tigger");
while ( millis() - previousUpdate < ActiveTime) {
sweeper1.Update();
Serial.println("triggered");
Active = true;
} if (millis() - previousUpdate > ActiveTime) {
Active = false;
//previousUpdate = millis();
}
}
}*_