The netduino seems to be producing random numbers that I am viewing in the debug window. Also, the trasmit led on the arduino is not activating like it does when viewing the data with a serial monitor. I have made sure that the com ports are the same for both devices and the transmit pin of the arduino is connected to the receive pin of the netduino.
Does anyone see errors in my code or have a suggestion for me to try? Thanks for any help.
Arduino code
enum PinAssignments { encoderPinA = 2, encoderPinB = 3, clearButton = 8 }; volatile unsigned int encoderPos = 0; unsigned int lastReportedPos = 1; boolean A_set = false; boolean B_set = false; void setup(){ pinMode(encoderPinA,INPUT); pinMode(encoderPinB,INPUT); attachInterrupt(0,doEncoderA,CHANGE); attachInterrupt(1,doEncoderB, CHANGE); Serial.begin(9600); } void loop(){ if(lastReportedPos != encoderPos) { Serial.print("Index:"); Serial.print(encoderPos, DEC); Serial.println(); lastReportedPos = encoderPos; } } //interrupt A void doEncoderA(){ A_set = digitalRead(encoderPinA) == HIGH; encoderPos += (A_set != B_set) ? +1 : -1; } void doEncoderB(){ B_set = digitalRead(encoderPinB) == HIGH; encoderPos += (A_set == B_set) ? +1 : -1; }
netduino code
namespace read_encoder { public class Program { const int MaxValue = 40; static SerialPort com; static byte[] ByteBuffer = new byte[MaxValue]; static int index = 0; public static void Main() { com = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One); com.DataReceived += new SerialDataReceivedEventHandler(com_DataReceived); com.Handshake = Handshake.None; com.Open(); Thread.Sleep(Timeout.Infinite); } static void com_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] bytes = new byte[1]; while(true) { com.Read(bytes, 0, bytes.Length); ByteBuffer[index++] = bytes[0]; if (index >= MaxValue) { string line = ""; for (int i = 0; i < MaxValue; i++) { line = ByteBuffer[i].ToString(); } Debug.Print(line); index = 0; } } } } }