Home→Forums→MonoBrick EV3 Firmware→interface an Arduino Uno as an i2c slave to EV3?
- This topic has 8 replies, 2 voices, and was last updated 10 years, 4 months ago by Helmut Wunder.
-
AuthorPosts
-
June 19, 2014 at 22:44 #4439
Helmut WunderParticipanthey,
I would urgently need code to interface an Arduino Uno or Duemilanove(==2009 😉 ) as an i2c slave to EV3.
Please code for both Arduino (Sketch) and EV3 (C#) will be appreciated!
For the start, a quite simple example will do (write digital pin for LED (if touch sensor pressed at EV3), read touch sensor state on Arduino (e.g. pin dig11), read Arduino analog resistor value (e.g. pin ana3), display all values on EV3 screen).
Thank you all very much in advance!June 20, 2014 at 12:48 #4443
Helmut WunderParticipantps,
an Arduino Sketch code for i2c communication is already available, but maybe it has to be changed to work reliably with the corresponding C# code:#include <Wire.h> #define SLAVE_ADDRESS 0x04 int number = 0; int state = 0; void setup() { pinMode(13, OUTPUT); Serial.begin(9600); // start serial for output // initialize i2c as slave Wire.begin(SLAVE_ADDRESS); // define callbacks for i2c communication Wire.onReceive(receiveData); Wire.onRequest(sendData); Serial.println("Ready!"); } void loop() { delay(100); } // callback for received data void receiveData(int byteCount){ // ... maybe add a delay(15) to go easy on the i2c bus... ??? while(Wire.available()) { number = Wire.read(); Serial.print("data received: "); Serial.println(number); if (number == 1){ if (state == 0){ digitalWrite(13, HIGH); // set the LED on state = 1; } else{ digitalWrite(13, LOW); // set the LED off state = 0; } } } } // callback for sending data void sendData(){ Wire.write(number); }
so any help for Mono/C# code will be appreciated!
Thanks in advance!June 21, 2014 at 14:00 #4447
Helmut WunderParticipanthey,
by NXC for NXT I got it working – one just would need to transcode the NXC source into Mono/C# –
who would like to do it? 😎http://www.mindstormsforum.de/viewtopic.php?f=25&t=8302&p=65015#p65015
June 21, 2014 at 18:59 #4448
Tcm0ParticipantDoes anyone have a working example for any I2C sensor (maybe an official one)? It would be helpfull when trying to write something for the Arduino.
June 22, 2014 at 10:28 #4449
Tcm0ParticipantNevermind, I got it: Klick me.
Here is my wiring diagram (yes, my breadboard is a mess): Klick me.
And now the code.
My Arduino class:using System; using MonoBrickFirmware; using MonoBrickFirmware.Sensors; namespace EV3Temeplate { class Arduino : I2CSensor { public Arduino(SensorPort Port, byte adress) : base (Port, adress, I2CMode.LowSpeed) { base.Initialise(); } public byte[] ReadReg(byte Register, byte Length) { return base.ReadRegister(Register, Length); } public void WriteReg(byte Register, byte[] Data) { base.WriteRegister(Register, Data); } public byte[] ReadAndWriteReg(byte Register, byte[] Data, int rxLength) { return base.WriteAndRead(Register, Data, rxLength); } public override string ReadAsString() { throw new NotImplementedException(); } } }
My program.cs:
using System; using System.Threading; using MonoBrickFirmware; using MonoBrickFirmware.Display; using MonoBrickFirmware.Sensors; using MonoBrickFirmware.UserInput; namespace EV3Temeplate { class Program { static void Main(string[] args) { EventWaitHandle stopped = new ManualResetEvent(false); LcdConsole.WriteLine("Connecting..."); byte Adresse = 0x04 << 1; ButtonEvents buts = new ButtonEvents(); Arduino Arduino1 = new Arduino(SensorPort.In1, Adresse); byte[] Ergebnis = new byte[1]; LcdConsole.WriteLine("Connected"); LcdConsole.WriteLine("Press Enter to read I2C Port"); LcdConsole.WriteLine("Press Escape to leave program"); buts.EscapePressed += () => { stopped.Set(); }; buts.EnterPressed += () => { Ergebnis = Arduino1.ReadReg(0x04, 1); LcdConsole.WriteLine("Result: " + Convert.ToString(Ergebnis[0])); }; stopped.WaitOne(); } } }
and the program that runs on the Arduino (sorry, I can’t organize it): Klick me.
June 27, 2014 at 08:16 #4459
Helmut WunderParticipanthey,
congratulations, this looks like a very good start!
but will you even try to establich a communication protocol for alternately, rotatory sending and receiving a data array like byte array[14] like I did in my NXC example? That would be great if we finally could access an Arduino by a EV3 similar like as it’s already possible by a NXT !http://www.mindstormsforum.de/viewtopic.php?f=25&t=8302#p65015
June 27, 2014 at 13:03 #4460
Tcm0ParticipantWhy don’t you port your NXT code to EV3? 😛
June 27, 2014 at 16:24 #4461
Helmut WunderParticipantwhy do you ask? you should know it, we already discussed this beyond measure –
For the EV3 there is no complete C-based API available – no sensor- and no i2c library, just a Lego lms2012 header which is bugged like hell.
I also don’t understand anything about C#, Visual Studio, WiFi, classes, objects, implementing, derivating, uses, extending –
just know something about straight procedures and functions like by ANSI C or Sketch or Pascal. OOP is simply a mess to me.But the NXC code and the Sketch code (both actually simplified procedural C) have been established by me, top-down, within a couple of days (as you know).
http://www.mindstormsforum.de/viewtopic.php?f=25&t=8302&p=65015#p65015If I knew how to do it by C# I wouldn’t have started my thread about Brick/Arduino-i2c-communication.
But as you are experienced in C# – : if it was that easy, why didn’t you proceed with your beginnings ?
Where’s the coding problem ?July 5, 2014 at 09:48 #4478
Helmut WunderParticipantany ideas for a transcoding?
-
AuthorPosts
You must be logged in to reply to this topic.
Follow