Author Topic: arduino i2c slave  (Read 2297 times)

0 Members and 1 Guest are viewing this topic.

Offline carbon dude oxideTopic starter

  • Frequent Contributor
  • **
  • Posts: 429
  • Country: gb
arduino i2c slave
« on: September 01, 2013, 09:36:31 pm »
hello, i am having trouble finding some form of code example which turns my arduino into an i2c slave which just receives 2 bytes of data at a time.

i currently have this:
Code: [Select]
#include <Wire.h>

#define A 7
#define B 6
#define C 9
#define D 12
#define E 13
#define f 10
#define G 11
#define P 8
#define D1 5
#define D2 4
#define D3 3
#define D4 2

String displays[] = {"x", "x", "ABCDEF", "ABCDEF", "ABCDEFP", "X"};

void setup() {
  // put your setup code here, to run once:
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
 
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(P, OUTPUT);
  Serial.begin(9600);
  Wire.begin(2);                // join i2c bus with address #2
  Wire.onReceive(requestEvent) // register event
 
}

void loop() {
  // put your main code here, to run repeatedly:
  //Serial.println(',', DEC);
  for (int i=5; i>1; i--) {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
   
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(f, LOW);
    digitalWrite(G, LOW);
    digitalWrite(P, LOW);
   
    if (displays[i].indexOf('A') > -1) {digitalWrite(A, HIGH);}
    if (displays[i].indexOf('B') > -1) {digitalWrite(B, HIGH);}
    if (displays[i].indexOf('C') > -1) {digitalWrite(C, HIGH);}
    if (displays[i].indexOf('D') > -1) {digitalWrite(D, HIGH);}
    if (displays[i].indexOf('E') > -1) {digitalWrite(E, HIGH);}
    if (displays[i].indexOf('F') > -1) {digitalWrite(f, HIGH);}
    if (displays[i].indexOf('G') > -1) {digitalWrite(G, HIGH);}
    if (displays[i].indexOf('P') > -1) {digitalWrite(P, HIGH);}
    digitalWrite(i, HIGH);
    delay(1);
  }
}
void requestEvent()
{
  Wire.receive("hello "); // respond with message of 6 bytes
                       // as expected by master
}
void test()
{
  byte icstart = B00000000;
  byte icend = B00000000;
  while (Wire.available() > 1){
    icstart = Wire.receive();
    icend = Wire.receive();
  }
  }
}

can someone help me with this please :)
-----
Everything Should Be Made as Simple as Possible, But Not Simpler
-----
 

Offline redben

  • Contributor
  • Posts: 21
  • Country: ch
Re: arduino i2c slave
« Reply #1 on: September 04, 2013, 09:09:17 am »
If you want a bit more help, you'll probably have to describe a bit more your project and your problem with it.
Apparently in this piece of code you don't even share data via I2C bus.
Are you trying to display things on LCD ? "if (displays.indexOf('A') > -1) {digitalWrite(A, HIGH);}"
What are the two devices you try to connect together ?
If it is a pc it is probably much simpler to establish a serial connection via usb.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: arduino i2c slave
« Reply #2 on: September 06, 2013, 10:01:44 pm »
"an i2c slave which just receives 2 bytes of data at a time."

The easiest would be to use an interrupt.
================================
https://dannyelectronics.wordpress.com/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf