lego_nxt_optical_sensor
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
lego_nxt_optical_sensor [2018/10/02 14:09] – [5, Send data from an Arduino to LEGO NXT via I2C] phamquyenanh | lego_nxt_optical_sensor [2018/10/02 15:10] (current) – [Code] phamquyenanh | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Get data from Optical Sensor SPCP168A and display in LEGO NXT ====== | ====== Get data from Optical Sensor SPCP168A and display in LEGO NXT ====== | ||
- | **Author:** Pham Quyen Anh **Email:** phamquyenanh.qb@gmail.com | + | |
- | \\ | + | |
- | **Date:** Last modified on 10/ | + | |
- | \\ | + | |
- | **Keywords: | + | |
- | \\ | + | |
===== 1, Optical Sensor Mouse SPCP168A ===== | ===== 1, Optical Sensor Mouse SPCP168A ===== | ||
Line 24: | Line 19: | ||
{{ : | {{ : | ||
The SPCP168A datasheet : http:// | The SPCP168A datasheet : http:// | ||
- | ===== 2, PS/2 protocal | + | ===== 2, PS/2 protocol |
==== PS/2 connection ==== | ==== PS/2 connection ==== | ||
The PS/2 connection is the IBM standard protocol to communicate keyboard, PC mouse with your computer. This protocol has the responsibility to send the key scan codes that you press to the pc and get some response commands from it. This means that we are dealing with a bi-directional type of protocol as each device (pc/ | The PS/2 connection is the IBM standard protocol to communicate keyboard, PC mouse with your computer. This protocol has the responsibility to send the key scan codes that you press to the pc and get some response commands from it. This means that we are dealing with a bi-directional type of protocol as each device (pc/ | ||
Line 61: | Line 56: | ||
</ | </ | ||
| | ||
- | ===== 3, Materials | + | ===== 3, Materials |
==== Materials and Sources ==== | ==== Materials and Sources ==== | ||
Line 75: | Line 70: | ||
| Lego NXT breadboard adapter | | Lego NXT breadboard adapter | ||
| LEGO Mindstorms NXT / EV3 Connector Cables | | LEGO Mindstorms NXT / EV3 Connector Cables | ||
- | ==== Software and Sources ==== | + | |
- | * Arduino ide | + | |
- | * ROBOTC for LEGO Mindstorms | + | |
===== 4, Interface A Sensor Mouse with Arduino | ===== 4, Interface A Sensor Mouse with Arduino | ||
==== Connection ==== | ==== Connection ==== | ||
Line 156: | Line 149: | ||
// put a hold on the incoming data | // put a hold on the incoming data | ||
low(_clockPin); | low(_clockPin); | ||
+ | } | ||
+ | </ | ||
+ | Reading data from optical mouse sensor | ||
+ | <code c Read> | ||
+ | void readdd() | ||
+ | { | ||
+ | requestData(); | ||
+ | Dstatu = readByte(); // this byte for buttons | ||
+ | Dx = readByte(); | ||
+ | Dy = readByte(); | ||
+ | if (_supportsIntelliMouseExtensions) { | ||
+ | DataWheel[count] = readByte(); | ||
+ | count++; | ||
+ | | ||
+ | } | ||
+ | X += (int)Dx; | ||
+ | Y += (int)Dy; | ||
+ | if(count==3) | ||
+ | { | ||
+ | for(count; | ||
+ | { | ||
+ | Sum+=(int)DataWheel[count]; | ||
+ | } | ||
+ | if(Sum> | ||
+ | else if(Sum< | ||
+ | Sum=0; | ||
+ | } | ||
} | } | ||
</ | </ | ||
Line 194: | Line 214: | ||
{{: | {{: | ||
{{: | {{: | ||
+ | |||
**Code** | **Code** | ||
+ | Read and dislay data | ||
+ | <code c I2C> | ||
+ | #pragma config(Sensor, | ||
+ | #include " | ||
+ | #include " | ||
+ | ubyte I2CReply[17] | ||
+ | ubyte | ||
+ | int xdata; | ||
+ | int ydata; | ||
+ | int wdata; | ||
+ | int datas; | ||
+ | // take two bytes and make an integer | ||
+ | int makeInt(ubyte highbyte, | ||
+ | return (highbyte<< | ||
+ | } | ||
+ | int makeInt(ubyte highbyte, | ||
+ | return (highbyte<< | ||
+ | } | ||
+ | //start at memoryAddress and return back replysize in bytes | ||
+ | int I2CArduino(tSensors link, ubyte memoryAddress, | ||
+ | memset(I2CRequest, | ||
+ | memset(I2CReply, | ||
+ | |||
+ | I2CRequest[0] = 2; // Message size | ||
+ | I2CRequest[1] = ARDUINO_I2C_ADDR; | ||
+ | I2CRequest[2] = memoryAddress; | ||
+ | if (!writeI2C(link, | ||
+ | return -1; | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | // | ||
+ | task main () { | ||
+ | |||
+ | nI2CRetries = 3; | ||
+ | clearDebugStream(); | ||
+ | writeDebugStreamLine(" | ||
+ | wait1Msec(500); | ||
+ | while (true){ | ||
+ | if (I2CArduino(ard, | ||
+ | writeDebugStreamLine(" | ||
+ | } | ||
+ | datas=makeInt(I2CReply[0], | ||
+ | xdata=makeInt(I2CReply[2], | ||
+ | ydata=makeInt(I2CReply[4], | ||
+ | wdata=makeInt(I2CReply[6], | ||
+ | if(datas& | ||
+ | if(datas& | ||
+ | if(datas& | ||
+ | nxtDisplayTextLine(1," | ||
+ | nxtDisplayTextLine(3," | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | **Video** | ||
+ | {{youtube> | ||
+ | ===== Reference ===== | ||
+ | - https:// | ||
+ | - http:// | ||
+ | - https:// | ||
+ | - http:// | ||
+ | - http:// | ||
+ | If you have any questions please contact for me via email phamquyenanh.qb@gmail.com. | ||
+ | ===== Download ===== | ||
+ | {{: | ||
lego_nxt_optical_sensor.1538514546.txt.gz · Last modified: 2018/10/02 14:09 by phamquyenanh