I want to have a joystick connected to arduino-controller, and a motor connected to arduino-robot.
I am first trying to test the serial communication between the two arduinos down a serial wire. (I do not plan to run my final project this way, I intend to try a wireless connection,, I've ordered some cheap 4333mhz transmitters)
I have the joystick working for me when everything is in one arduino. I just podged up the codes from how I had them,, but I will paste the codes as they are (not working or exactly what I tried), so that any can see my hoped result with the joystick. up and down are mapped to motor outputs on two pins, which are connected to a little H-bridge I breadboarded out of regular to-92 4401 transistors. the motor is tiny at present, so this works. Using my joystick I can go forward and backward on the single motor while working my code back together on a single arduino.
I've split the code into slave-master and did manage a communication between the two devices,, however no matter what I did on the joystick I recieved full forward power, and in serial monitor it printed 255 only. ,, SO I think that I did not successfully encode motorInstruct down the wire, and the wire.read was merely reading some kind of string input or something, and it was being mapped to 255 incidentally.
#include <Wire.h>
int joypin = A0;
void setup() {
// put your setup code here, to run once:
Wire.begin(); // join i2c bus (address optional for master)
pinMode(joypin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int motorInstruct = analogRead(A0);
Wire.beginTransmission(8); // transmit to device #8
Wire.write(motorInstruct); // sends five bytes
Wire.endTransmission(); // stop transmitting
delay (50);
}
#include <Wire.h>
int motorPinf = 3;
int motorPinb = 5;
void setup() {
// put your setup code here, to run once:
Wire.begin(8); // join i2c bus with address #8
Serial.begin(9600); // start serial for output
pinMode(motorPinf, OUTPUT);
pinMode(motorPinb, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int motorInstruct = Wire.read();
motorInstruct = map (motorInstruct, 0, 1023, -255, 255);
Serial.print(motorInstruct);
if (motorInstruct >= -255 && motorInstruct <= -51)
{
analogWrite(motorPinb, -motorInstruct);
analogWrite(motorPinf, 0);
}
if (motorInstruct >= 51 && motorInstruct <=255)
{
analogWrite(motorPinf, motorInstruct);
analogWrite(motorPinb, 0);
}
if (motorInstruct >= -50 && motorInstruct <= 50)
{
analogWrite(motorPinf, 0);
analogWrite(motorPinb, 0);
}
Serial.print(motorInstruct); // print the character
delay(50);
}