User Tools

Site Tools


lego_nxt_optical_sensor

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
lego_nxt_optical_sensor [2018/10/02 14:09] – [5, Send data from an Arduino to LEGO NXT via I2C] phamquyenanhlego_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:** [email protected] +
-\\ +
-**Date:** Last modified on 10/02/2018 +
-\\ +
-**Keywords:** tutorial, Optical Sensor Mouse, SPCP168A, PS/2, LEGO NXT +
-\\+
  
 ===== 1, Optical Sensor Mouse SPCP168A ===== ===== 1, Optical Sensor Mouse SPCP168A =====
Line 24: Line 19:
 {{ :wiki:spcp168a-circuit.gif?nolink |}} {{ :wiki:spcp168a-circuit.gif?nolink |}}
 The SPCP168A datasheet : http://datasheetcafe.databank.netdna-cdn.com/wp-content/uploads/2015/11/SPCP168A.pdf The SPCP168A datasheet : http://datasheetcafe.databank.netdna-cdn.com/wp-content/uploads/2015/11/SPCP168A.pdf
-===== 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/keyboard) sends and receives commands. 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/keyboard) sends and receives commands.
Line 61: Line 56:
 </code>  </code> 
          
-===== 3, Materials and Software =====+===== 3, Materials  =====
 ==== Materials and Sources ==== ==== Materials and Sources ====
  
Line 75: Line 70:
 | Lego NXT breadboard adapter   | https://www.generationrobots.com/en/401251-nxt-breadboard-adapter-dexter-industries.html | | Lego NXT breadboard adapter   | https://www.generationrobots.com/en/401251-nxt-breadboard-adapter-dexter-industries.html |
 | LEGO Mindstorms NXT / EV3 Connector Cables  | https://www.amazon.com/LEGO-Mindstorms-NXT-Connector-Cables/dp/B00G1L8J5A | | LEGO Mindstorms NXT / EV3 Connector Cables  | https://www.amazon.com/LEGO-Mindstorms-NXT-Connector-Cables/dp/B00G1L8J5A |
-==== Software and Sources ==== +
-  * Arduino ide +
-  * ROBOTC for LEGO Mindstorms+
 ===== 4, Interface A Sensor Mouse with Arduino  via PS/2 protocol ===== ===== 4, Interface A Sensor Mouse with Arduino  via PS/2 protocol =====
 ==== Connection ==== ==== Connection ====
Line 156: Line 149:
     // put a hold on the incoming data     // put a hold on the incoming data
     low(_clockPin);     low(_clockPin);
 +}
 +</code> 
 +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;count>=0;count--)
 +    { 
 +      Sum+=(int)DataWheel[count];
 +    }
 +    if(Sum>0) W++;
 +    else if(Sum<0) W--;
 +    Sum=0;count=0;
 +  }
 } }
 </code>  </code> 
Line 194: Line 214:
 {{:wiki:fullccc.png?nolink|}} {{:wiki:fullccc.png?nolink|}}
 {{:wiki:42982714_486068395227202_1657342972317925376_n.jpg?nolink|}} {{:wiki:42982714_486068395227202_1657342972317925376_n.jpg?nolink|}}
 +
 **Code** **Code**
  
 +Read and dislay data 
 +<code c I2C>
 +#pragma config(Sensor, S1,ard,sensorI2CCustom)
 +#include "common.h" // I2C lib
 +#include "settings.h"
 +ubyte I2CReply[17]  ;  //technically we should use tByteArray
 +ubyte   I2CRequest[17];
 +int xdata;
 +int ydata;
 +int wdata;
 +int datas;
 +// take two bytes and make an integer
 +int makeInt(ubyte highbyte,ubyte lowbyte){
 + return (highbyte<<8) | lowbyte;
 +}
 +int makeInt(ubyte highbyte,ubyte lowbyte){
 + return (highbyte<<8) | lowbyte;
 +}
 +//start at memoryAddress and return back replysize in bytes
 +int I2CArduino(tSensors link, ubyte memoryAddress,ubyte replysize) {
 + memset(I2CRequest, 0, 17);
 + memset(I2CReply, 0, 17);
 +
 + I2CRequest[0] = 2;    // Message size
 + I2CRequest[1] = ARDUINO_I2C_ADDR; // I2C Address
 + I2CRequest[2] = memoryAddress; // memory address
 + if (!writeI2C(link,I2CRequest,I2CReply,replysize)){
 + return -1;
 + }
 + return 0;
 +}
 +
 +//===========================================================
 +task main () {
 +
 + nI2CRetries = 3;
 + clearDebugStream();
 + writeDebugStreamLine("Starting...");
 + wait1Msec(500);
 + while (true){
 + if (I2CArduino(ard,ARD_DATA,8)){
 + writeDebugStreamLine("ERROROR!!");
 + }
 + datas=makeInt(I2CReply[0],I2CReply[1]); // take two bytes for Determine the negative or positive value of X,Y,W. 
 +         xdata=makeInt(I2CReply[2],I2CReply[3]); // take two bytes for X axis
 + ydata=makeInt(I2CReply[4],I2CReply[5]); // take two bytes for Y axis
 + wdata=makeInt(I2CReply[6],I2CReply[7]); // take two bytes for W axis
 + if(datas&0x01) xdata=-xdata;// if (datas&0x01==1) X<0
 + if(datas&0x02) ydata=-ydata;// if (datas&0x02==1) Y<0
 + if(datas&0x04) wdata=-wdata;// if (datas&0x04==1) W<0
 + nxtDisplayTextLine(1,"(%4d,%4d)",xdata,ydata);
 + nxtDisplayTextLine(3,"%4d",wdata);
 + }
 +}
 +</code> 
  
 +**Video**
 +{{youtube>NjTEpLt_3GU?medium}}
 +===== Reference =====
 +  - https://en.wikipedia.org/wiki/Optical_mouse
 +  - http://pcbheaven.com/wikipages/The_PS2_protocol/
 +  - https://playground.arduino.cc/Main/InterfacingWithHardware#mouse
 +  - http://www.networktechinc.com/ps2-prots.html
 +  - http://datasheetcafe.databank.netdna-cdn.com/wp-content/uploads/2015/11/SPCP168A.pdf
 +If you have any questions please contact for me via email [email protected].
 +===== Download =====
 +{{:wiki:i2cnxtandarduino.rar|}}
  
  
  
  
lego_nxt_optical_sensor.1538514546.txt.gz · Last modified: 2018/10/02 14:09 by phamquyenanh