The situation is:
Arduino Uno recieves data from accelerometer (pulseX, pulseY), normalizes it (accelerationX, accelerationY), and forms a control signal (which is a byte value). Then it sends this byte via I2C on request from the Arduino Pro.
Arduino Pro recieves this byte, and selects a movement function. The problem is, even though it enters the function (right(), left(t), etc), the motor never actually works. More accurately, the problem seems to be with the case: if i move the movement function from it, everything works.
God, that was a terrible explanation, please ask any questions remaining.
Slave code:
#include <Wire.h>
// these constants won't change:
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer
int pulseX, pulseY; // variables to read the pulse widths
int accelerationX, accelerationY; // variables to contain the resulting accelerations
byte control;
void setup()
{
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
Wire.begin(2);
Wire.onRequest(requestEvent);
}
void loop()
{
pulseX = pulseIn(xPin, HIGH);
pulseY = pulseIn(yPin, HIGH);
accelerationX = pulseX - 5000;
accelerationY = pulseY - 5000;
//forming control byte to send to the master
if (accelerationX > 500) control = B00000001; //tilt back
if (accelerationX < -500) control = B00000010; //tilt forward
if (accelerationY > 500) control = B00000011; //tilt right
if (accelerationY < -500) control = B00000100; //tilt lfet
if (accelerationX < 500) {
if (accelerationY < 500) {
if (accelerationX > -500) {
if (accelerationY > -500) control = B00000000;}}} //back to blank state
delay(1000);
}
void requestEvent()
{
Wire.write(control);
}
Master code:
#include <Wire.h>
int pwm_a = 3;
int pwm_b = 11;
int dir_a = 12;
int dir_b = 13;
int movement_delay = 500;
byte control, control_new;
void setup()
{
Wire.begin();
Serial.begin(9600);
pinMode(pwm_a, OUTPUT);
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
pinMode(13, OUTPUT);
pinMode(8, INPUT);
}
void loop()
{
Wire.requestFrom(2, 1);
while (Wire.available())
{
control = Wire.read();
control_new = control;
}
switch (control_new)
{
case B00000001: backward();
delay(movement_delay);
break;
case B00000010: stop();
forward();
delay(movement_delay);
break;
case B00000011: right();
delay(movement_delay);
break;
case B00000100: left();
delay(movement_delay);
break;
case B00000000: stop();
delay(100);
break;
}
}
void forward()
{
stop();
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, HIGH);
analogWrite(pwm_a, 255);
analogWrite(pwm_b, 255);
}
void backward()
{
stop();
digitalWrite(dir_a, LOW);
digitalWrite(dir_b, LOW);
analogWrite(pwm_a, 255);
analogWrite(pwm_b, 255);
}
void left()
{
stop();
digitalWrite(dir_a, LOW);
digitalWrite(dir_b, HIGH);
analogWrite(pwm_a, 255);
analogWrite(pwm_b, 255);
}
void right()
{
stop();
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, LOW);
analogWrite(pwm_a, 255);
analogWrite(pwm_b, 255);
}
void stop()
{
digitalWrite(dir_a, LOW);
digitalWrite(dir_b, LOW);
analogWrite(pwm_a, 0);
analogWrite(pwm_b, 0);
}