i have connect a touch screen resistive on my mega, and i want to send position value to uvga but its not work i don't know anymore the protocol I2C can you explain me why the code doesn't work?
#include "Wire.h"
int xVal;
int yVal;
void setup()
{
Wire.begin();
}
int readX() // returns the value of the touch screen's X-axis
{
int xr=0;
pinMode(A1, OUTPUT);
pinMode(A2, INPUT);
pinMode(A3, OUTPUT);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
delay(5);
xr=analogRead(2);
}
int readY()
{
int yr=0;
pinMode(A2, OUTPUT);
pinMode(A3, INPUT);
pinMode(A4, OUTPUT);
digitalWrite(A2, LOW);
digitalWrite(A4, HIGH);
delay(5);
yr=analogRead(3);
return yr;
}
void loop()
{
Wire.send(xVal);
Wire.send(yVal);
}
Not unless you define "doesn't work". That code does something. You expect it to do something. What either of those things is, though, is a mystery.
What does the code do? What do you expect it to do?
A picture of your Arduino with stuff attached, and a schematic, would be helpful, too.
You never read anything from the Wire instance. Why not?
An I2C device would be connected to digital pins 20 and 21. Is yours? Why are you diddling with the analog pins (as digital pins) in readX() and readY()? What is connected to analog pins 2 and 3? Why?
on the arduino pins 20 (SDA) connect to the uvga pin j1-10, pin 21 (SCL) connect to j1-9.
Are we supposed to guess that that is OK? A link to the device you are trying to talk to would be useful?
with the rx/tx communication will work but very slow for my future project.
I doubt that it is serial communication that is the bottleneck. Baud rate, maybe, but not that fact that communication is serial vs. I2C. Hard to say, though, without knowing what you are trying to communicate with.
The only mention of I2C in the data sheet that I see ways that the uvga is an I2C master. You are trying to treat it as a slave. That is not going to work.
you know how to work with the arduino in slave and uvga in master?
No clue. The Arduino would need to register as a slave with some address, and register a callback for use when the master sent a request/command/data.
The uvga would then send messages to the slave at that address. How it would do that, though, I do not know, nor do I know what those messages would look like or what the reply would need to be, if any.
Every time the new and old readings are different, you have a delay. Why?
Having a non-static local variable in the loop() function means that it gets reset to 3 on every pass through loop, so every time you do not read 3 from the analog pin (no idea what is connected to it), you write to the uvga and have a 5 millisecond delay. I'm guessing that 99% of the time, the Arduino is doing nothing but waiting.