Der Code vom Sensor-Nano:
//Bordcomputer Slave (Sensor)
//with 74HC595 LED-Bargraph
//I2C Master
//library for I2C
#include <Wire.h>
//library for EEPROM access
#include <EEPROM.h>
//I2C slave address
int slaveAddress = 2;
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 9;
////Pin connected to DS of 74HC595
int dataPin = 10;
//
//RPM
//
volatile int ignitionCount;
//ignition pickup input | Interrupt-Pin
int interPin1 = 2;
unsigned long t;
//period between 2 ignitions
float period = 0.0;
//variable to store the RPM
unsigned long rpm = 20;
float fCrank = 0.0;
//
//Speed
//
//EEPROM address for the wheel circumference value.
//Don't change this if you don't EXAKTLY know what you're doing !
int wheelCircAddress = 1;
//wheel-circumference in mm
int wheelCirc;
volatile int wheelRevolutions;
unsigned long t1;
float fWheel = 0.0;
int interPin2 = 3;
const float pi = 3.142;
int vel;
int numberOfWheelsensors=1;
int numberOfWheelsensorsAddress=2;
//Byte-Array for the LED-Bargraph
byte data[9] {B00000000, B00000001, B00000011, B00000111, B00001111, B00011111, B00111111, B01111111, B11111111};
int val;
void setup() {
// put your setup code here, to run once:
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
t = millis();
t1=millis();
attachInterrupt(digitalPinToInterrupt(interPin1),isIgnition,RISING);
ignitionCount;
attachInterrupt(digitalPinToInterrupt(interPin2),isRev,RISING);
wheelRevolutions=0;
Serial.begin(9600);
Wire.begin();
EEPROM.get(wheelCircAddress,wheelCirc);
//EEPROM.get(numberOfWheelsensorsAddress,numberOfWheelsensors);
}
//ISR for the Speed
void isIgnition() {
ignitionCount++;
}
//ISR for the Speed
void isRev() {
wheelRevolutions++;
}
//2 bytes
void sendRPM(int x){
Wire.beginTransmission(slaveAddress); // begin transmission to slave device
Wire.write(x & 0xff); // send first byte
Wire.write(x >> 8); // send second byte shiftet by 8 bits
Wire.endTransmission(); // stop transmitting
}
//1 byte
void sendSpeed(int x){
Wire.beginTransmission(slaveAddress);
Wire.write(x);
Wire.endTransmission();
}
void loop() {
//calculate the RPM
if (ignitionCount > 20){
detachInterrupt(digitalPinToInterrupt(interPin1));//Disable interrupt when calculating
fCrank = (1/(((millis()-t)/ignitionCount)/1000.0));
rpm = fCrank * 60;
ignitionCount = 0;
Serial.print("Crank frequency ");
Serial.println(fCrank);
Serial.print("RPM ");
Serial.println(rpm);
Serial.println("-------------");
sendRPM(rpm);
//drive the LED-Bar
val = map(rpm, 0, 12000, 0, 8);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, data[val]);
digitalWrite(latchPin, HIGH);
attachInterrupt(digitalPinToInterrupt(interPin1),isIgnition,RISING);
t=millis();
}
//calculate the Speed
if (wheelRevolutions > 0){
detachInterrupt(digitalPinToInterrupt(interPin2));//Disable interrupt when calculating
fWheel = (1/(((millis()-t1)/wheelRevolutions)/1000.0));
vel=(((wheelCirc*pi*(fWheel*60))/1000)*60)/1000;
vel=vel/numberOfWheelsensors;
wheelRevolutions=0;
Serial.print("Wheel frequency ");
Serial.println(fWheel);
Serial.print("Speed ");
Serial.println(vel);
Serial.println("-----------------");
//sendSpeed(vel);
attachInterrupt(digitalPinToInterrupt(interPin2),isRev,RISING);
t1=millis();
}
}
Der Code vom Pulsgeber-Nano
int rpm;
int vel;
unsigned long t1;
unsigned long t2;
#define rpmIn A0 //rpm input pin 100k Pot
#define rpmOut 13
#define velIn A1 //speed input pin 10k Pot
#define velOut 11
void setup() {
// put your setup code here, to run once:
pinMode(rpmIn,INPUT);
pinMode(rpmOut,OUTPUT);
pinMode(velIn,INPUT);
pinMode(velOut,OUTPUT);
Serial.begin(9600);
}
void rpmPulse(int rpmIn, int pin, int onTime){
//rpm=map(analogRead(rpmIn),0,1024,150,1);
rpm=map(analogRead(rpmIn),0,1024,6,300);
if (t1 + rpm < millis()){
digitalWrite(pin,HIGH);
t1=millis();
}
if (t1 + onTime < millis()){
digitalWrite(pin,LOW);
}
}
void velPulse(int rpmIn, int pin, int onTime){
rpm=map(analogRead(rpmIn),0,1024,1500,100);
if (t2 + rpm - onTime < millis()){
digitalWrite(pin,HIGH);
t2=millis();
}
if (t2 + onTime < millis()){
digitalWrite(pin,LOW);
}
}
void loop() {
// put your main code here, to run repeatedly:
rpmPulse(rpmIn,rpmOut,2);
velPulse(velIn,velOut,1);
//Serial.println (rpm);
}
Und noch die funktionierenden Master-Slave Sketche
// Wire Master Writer
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Writes data to an I2C/TWI slave device
// Refer to the "Wire Slave Receiver" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
int slaveAddress=2;
void sendRPM(int x){
Wire.beginTransmission(slaveAddress); // transmit to device #8
Wire.write(x & 0xff); // sends five bytes
Wire.write(x >> 8); // sends one byte
Wire.endTransmission(); // stop transmitting
}
void setup() {
Serial.begin(9600);
Wire.begin(); // join i2c bus (address optional for master)
}
void loop() {
unsigned long t = millis();
int x = random(0,12000) ;
sendRPM(x);
//delay(10);
Serial.println(millis() - t);
}
// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
int rpm;
void setup() {
Wire.begin(2); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
Serial.print("Drehzahl: ");
Serial.print(rpm);
Serial.println(" RPM");
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
if (Wire.available()){
rpm = Wire.read();
if(Wire.available()){
rpm = rpm + (Wire.read()<<8);
}
}
}