Hello,
I am working on a project to interface an AS5311 linear position sensor Adapter board along with an OLED Display 0.91 Inch 128x32 using I2C connection (a connection made as described in this link) on an Arduino Uno board. The goal is to make something like this, expect only linearly. Individually the screen and the AS5311 sensor are working as expected. However, I am having issues with the integration of both of them.
I have provided as much necessary information as possible to better understand my predicament.
Here are the connections I have used for the screen and sensor respectively
PWM is connected to Digital pin 2.
The goal is to measure the time of the PWM which can vary from 0 to 4096, which corresponds to between 0mm to 2mm linearly and uniformly.
Here is the PMW code (without screen integration) that works perfectly. It uses the logic of interrupts to find the timestamp during the rising and falling edge of the PWM and find the difference between them which will be multiplied by a factor to display a mm distance
volatile float pwm_value = 0;
volatile float prev_time = 0;
volatile float result;
void setup() {
Serial.begin(115200);
// when pin D2 goes high, call the rising function
attachInterrupt(0, rising, RISING);
}
void loop() { }
void rising() {
attachInterrupt(0, falling, FALLING);
prev_time = micros();
}
void falling() {
attachInterrupt(0, rising, RISING);
pwm_value = micros()-prev_time;
result = pwm_value*2/4095;
Serial.println(result,5);
}
This is how the result of the PWM only looks when I move the magnet linearly. It is very stable and promising. So I am certain this logic is correct.
Now I am trying to display the result value on my OLED screen every time it is reassigned a new value using this code and all hell breaks loose.
// Include Wire Librarby for I2C connection
#include <Wire.h>
// Include Adafruit Graphics & OLED libraries
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Reset pin not used but needed for Library
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
// Declaring variables
volatile float pwm_value = 0;
volatile float prev_time = 0;
volatile float result;
void setup() {
// Testing purposes only
Serial.begin(115200);
// Start Wire library for I2C
Wire.begin();
// Initialize OLED with I2C add 0x3C
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
delay(2000);
// Attach intutrrpt for PMW
attachInterrupt(0, rising, RISING);
}
void loop() { }
void rising() {
attachInterrupt(0, falling, FALLING);
prev_time = micros();
}
void falling() {
detachInterrupt(0);
pwm_value = micros()-prev_time;
result = pwm_value*2/4095;
Serial.println(result,5);
// Show result on OLED screen below. Not working
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print(result);
display.display();
attachInterrupt(0, rising, RISING);
}
I am having two errors.
- The sensor kind off stops working as I can see no values popping on the serial monitor as when the PWM code was on its own. When I do the same action of moving the magnet around the sensor, I get an incomplete reading like this.
- The screen simply displays a random placement of dots which makes no sense to me. Have a look.
Any help and knowledge is appreciated. Thanks in advance.