TWI on two Arduinos

From Code Lab Wiki

Jump to: navigation, search

Based upon what was discovered with TWI on the Arduino Mini and Capsense with Arduino .

/*
 * TWI Test + CapSense
 * ES + BMW
 * 05 April 2008
 *
 * Trying to get TWI to work.  Setup - two arduino minis, one ID6 and one ID7.
 * The same code (with a different hard-coded ID) runs on both minis.
 * ID6 has a pot attached to pin2, and ID7 has a LED attached to pin13.  ID6 sends the pot
 * value over TWI to ID7, which receives it and blinks the LED at a corresponding rate.
 *
 */

#include <Wire.h>

int nodeID = 7;    // set the unique ID of the node
int potPin = 2;    // select the input pin for the potentiometer
int ledPin = 3;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor

int  i;
unsigned int x, y;
float accum, fout;  // these are variables for a simple low-pass (smoothing) filter
float fval = .07;         // fval of 1 = no filter - .0001 = max filter
float lastCount;
int hit = 0;
byte touched = 0;

void setup() {
  pinMode(ledPin, OUTPUT);        // declare the ledPin as an OUTPUT
  Wire.begin(nodeID);             // join i2c bus (address optional for master)
  Wire.onReceive(receiveEvent);   // register event
  Serial.begin(9600);
  DDRB=B101;    
  digitalWrite(10, LOW);  // guard pin low
}

void loop() {
  y = 0;        // clear out variables
  x = 0;
  for (i=0; i < 4 ; i++ ){      
    // LOW-to-HIGH transition
    PORTB = PORTB | 1;                   // Same as line below - but slightly faster
    while ((PINB & B10) != B10 ) {       // while the sense pin is not high               
      x++;
    }
    delay(1);
    //  HIGH-to-LOW transition
    PORTB = PORTB & 0xFE;                 
    while((PINB & B10) != 0 ){    
      y++;  
    }
    delay(1);
  }

  fout =  (fval * (float)x) + ((1-fval) * accum);  // Easy smoothing filter "fval" determines amount of new data in fout
  accum = fout;    

  Serial.print((long)x, DEC);    // raw data - Low to High
  Serial.print( "   ");
  Serial.print((long)y, DEC);    // raw data - High to Low
  Serial.print( "   ");
  Serial.println( (long)fout, DEC); // Smoothed Low to High

  if((long)fout > 4){
    analogWrite(4,255);
    touched = 100;

  } 
  else {
    if((long)fout <= 4){
      hit++;
      if(hit >= 4){
        analogWrite(4,0);
        touched = 0;
        hit = 0;
      }
    }
  }
  if((millis() - lastCount >= 75) && hit >= 1){
    hit = hit - 1;
    lastCount = millis();
  }
  /*
  Serial.println("___________________________________");
   Serial.print(hit, DEC);
   Serial.println("");
   */


  if (nodeID == 6) {
    val = analogRead(potPin);    // read the value from the sensor
    val = val / 4;               // analogRead gives us 0-1023, we only want 1 byte

    Wire.beginTransmission(7); // transmit to device #7
    Wire.send(val);     // sends one byte  
    Wire.endTransmission();    // stop transmitting

    if(touched == 1){
      digitalWrite(ledPin, HIGH); 
    } 
    else {
      digitalWrite(ledPin, LOW);
    } 
  }

  if (nodeID == 7 && val <= 500) {
    digitalWrite(ledPin, HIGH);  // turn the ledPin on
    delay(val);                  // stop the program for some time
    digitalWrite(ledPin, LOW);   // turn the ledPin off
    delay(val);    // stop the program for some time
    Serial.println(val);

    Wire.beginTransmission(6); // transmit to device #6
    Wire.send(touched);     // sends one byte  
    Wire.endTransmission();    // stop transmitting
    //Serial.println("one byte sent of touched value:" + touched);
    //Serial.println(touched);
  }
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  val = Wire.receive();
}

Personal tools