Skip to content

Commit

Permalink
New Arduino example
Browse files Browse the repository at this point in the history
  • Loading branch information
Fattoresaimon committed Oct 20, 2017
1 parent 12ae753 commit d96703a
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 8 deletions.
106 changes: 106 additions & 0 deletions Arduino Library/Examples/EncoderI2C_4encoder.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*This example show how to read 4 I2C Encoder
All the 4 encoders will count between -20 and 20, the color of the LED will blink according to the direction of rotation.
The INT pin is readed in polling mode
Connections with Arduino UNO:
- -> GND
+ -> 5V
SDA -> A4
SCL -> A5
INT -> 12
*/

#include <Wire.h>
#include "i2cEncoderLib.h"


#define ENCODER_N 4
const int IntPin = 12;
i2cEncoderLib encoder[ENCODER_N] = { i2cEncoderLib(0x39), i2cEncoderLib(0x30), i2cEncoderLib(0x32), i2cEncoderLib(0x34)}; //Class initialization with the I2C addresses


int32_t counter[ENCODER_N] = {0, 0, 0, 0};
int32_t maxvalue[ENCODER_N] = {20, 20, 20, 20};
int32_t minvalue[ENCODER_N] = { -20, -20, -20, -20};
int32_t econfig[ENCODER_N] = {(INTE_ENABLE | LEDE_ENABLE | WRAP_DISABLE | DIRE_RIGHT | IPUP_ENABLE | RMOD_X1 ),
(INTE_ENABLE | LEDE_ENABLE | WRAP_DISABLE | DIRE_RIGHT | IPUP_ENABLE | RMOD_X1 ),
(INTE_ENABLE | LEDE_ENABLE | WRAP_DISABLE | DIRE_RIGHT | IPUP_ENABLE | RMOD_X1 ),
(INTE_ENABLE | LEDE_ENABLE | WRAP_DISABLE | DIRE_RIGHT | IPUP_ENABLE | RMOD_X1 ),
};
uint8_t encoder_status;

volatile uint8_t i;
void setup(void)
{
pinMode(IntPin, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);

Serial.print("Encoder Test!!\n");
//Encoder Initialization
for (i = 0; i < ENCODER_N; i++) {
Serial.println(econfig[i], HEX);
encoder[i].begin(econfig[i]);
encoder[i].writeLEDA(0x00);
encoder[i].writeLEDB(0x00);
encoder[i].writeCounter(counter[i]);
encoder[i].writeMax(maxvalue[i]);
encoder[i].writeMin(minvalue[i]);
encoder[i].updateStatus();
}
}

void loop() {


if (digitalRead(IntPin) == LOW) {
digitalWrite(LED_BUILTIN, HIGH);

for (i = 0; i < ENCODER_N; i++) { //Use a for loop for read all the 4 encoders
if (digitalRead(IntPin) == HIGH)
break;

if (encoder[i].updateStatus()) {

if (encoder[i].readStatus(E_INCREMENT)) {
encoder[i].writeLEDA(0xff);
}

if (encoder[i].readStatus(E_DECREMENT)) {
encoder[i].writeLEDB(0xff);
}

if (encoder[i].readStatus(E_PUSH)) {
Serial.print("E");
Serial.print(i);
Serial.print(" Push\n");
}

if (encoder[i].readStatus(E_MAXVALUE)) {
Serial.print("E");
Serial.print(i);
Serial.print(" Max\n");
}

if (encoder[i].readStatus(E_MINVALUE)) {
Serial.print("E");
Serial.print(i);
Serial.print(" Min\n");
}

counter[i] = encoder[i].readCounterByte();
encoder[i].writeLEDA(0x00);
encoder[i].writeLEDB(0x00);
}
}

for (i = 0; i < ENCODER_N; i++) { //print the final value
Serial.print(counter[i], DEC);
Serial.print("\t");
}
Serial.println();

digitalWrite(LED_BUILTIN, LOW);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
#include "i2cEncoderLib.h"

/*In this example, i want my variable counter between -10 and 10.
When it reaches the limit the LED will blink red in case of minimum and will blink green when it reaches the maximum.*/
When it reaches the limit the LED will blink red in case of minimum and will blink green when it reaches the maximum.
The INT pin is readed in polling mode
Connections with Arduino UNO:
- -> GND
+ -> 5V
SDA -> A4
SCL -> A5
INT -> 12
*/


const int IntPin = 12;
i2cEncoderLib encoder(0x30); //Initialize the I2C encoder class with the I2C address 0x30 is the address with all the jumper open
Expand Down
8 changes: 4 additions & 4 deletions Arduino Library/i2cEncoderLib.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//
// FILE: i2cEncoderLib.h
// VERSION: 0.1..
// PURPOSE: Libreary for the i2c encoder board with arduinp
// FILE: i2cEncoderLib.cpp
// VERSION: 0.2.0
// PURPOSE: Library for the I2C Encoder with arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET:
// DATASHEET: https://github.com/Fattoresaimon/i2cencoder/blob/master/EncoderI2C%20v1.2.pdf
//
// URL:
//
Expand Down
7 changes: 4 additions & 3 deletions Arduino Library/i2cEncoderLib.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
//
// FILE: i2cEncoderLib.h
// VERSION: 0.1..
// PURPOSE: Libreary for the i2c encoder board with arduinp
// VERSION: 0.2.0
// PURPOSE: Library for the I2C Encoder with arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET:
// DATASHEET: https://github.com/Fattoresaimon/i2cencoder/blob/master/EncoderI2C%20v1.2.pdf
//
// URL:
//
// AUTHOR:
// Simone Caron
//


#ifndef I2CENCODERLIB_H
#define I2CENCODERLIB_H

Expand Down

0 comments on commit d96703a

Please sign in to comment.