0

I'm trying to make a game where i can use one Arduino as a controller (the controller is a potentiometer) and the other Arduino sending the signals to the computer. Im trying to figure out communication with two Arduino's so i would like to use two instead of one although using one would be logical and easier. Ive tried looking into I2C but cant seem to figure it out.

asked Dec 6, 2016 at 19:33
2
  • 1
    Why are you using 2 Arduinos? You could do all of this in just one Arduino. Commented Dec 7, 2016 at 1:55
  • Please also improve the formatting of your question. Commented Dec 7, 2016 at 1:55

1 Answer 1

1

Start by reading the potentiometer. Add a Wire request service function and that makes the Slave:

#include <Wire.h>
const uint8_t controllerAddress = 8;
const int controllerPin = A0;
volatile int controllerValue = 0;
void setup() {
 Wire.begin(controllerAddress);
 Wire.onRequest(requestEvent); 
}
void loop() {
 int value = analogRead(controllerPin);
 noInterrupts(); 
 controllerValue = value; 
 interrupts();
}
void requestEvent() {
 Wire.write((const uint8_t*) &controllerValue, sizeof(controllerValue)); 
}

This is minor update of the Arduino Wire Master Reader tutorial. The sketch for the Master is left as an exercise.

Cheers!

answered Dec 6, 2016 at 21:20
2
  • I would assign 'A0' to controllerPin instead of '0', to avoid any confusing with digital pins: const int controllerPin = A0; Commented Apr 6, 2017 at 6:57
  • Nice catch. I will fix that. Commented Apr 6, 2017 at 18:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.