Greetings,
I am somewhat of a beginner. I am currently using two MKR 1310s as a transmitter and receiver. I am attempting to send distance data by using a Taiss E38S6-100-24G Rotary Encoder to the receiver.
When I observe the serial monitor of the transmitter, the count of the rotations works flawlessly.
My Problem:
I am able to see seemingly appropriate data on the receiver serial monitor but the rotation/distance number stays at whatever number the transmitter was on when I switched ports. When I switch ports back to the Tx'er, it continued to track accurately.
Serial Monitor Example:
//Tx serial monitor //
LoRa Sender
Rotations: 0
Rotations: 4
Rotations: 26
Rotations: 48
Rotations: 54
//Switch from Tx serial monitor and connect to rx //
LoRa Reciever
Rotations: 54
Rotations: 54
Rotations: 54
Rotations: 54
// Reconnect to Tx serial monitor //
LoRa Sender
Rotations: 59
Rotations: 72
Rotations: 84
Rotations: 109
// Reconnect to Rx serial monitor //
LoRa Reciever
Rotations: 54
Rotations: 54
Rotations: 54
Rotations: 54
My code:
[code]
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
//Constants
#define encoder0PinA 0
#define encoder0PinB 1
//Variables
int encoder0Pos = 0;
int valRotary, lastValRotary;
void setup() {
Serial.begin(9600);
// Initialize encoder pins
pinMode(encoder0PinA, INPUT_PULLUP);
pinMode(encoder0PinB, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
while (!Serial);
Serial.println("LoRa Sender");
if (!LoRa.begin(915E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop() {
attachInterrupt(0, doEncoder, CHANGE); //Interupt from pin 0
if (valRotary > lastValRotary)
if (valRotary);
lastValRotary = valRotary;
Serial.print("Rotations: ");
Serial.print(lastValRotary);
Serial.println(", ");
// send packet
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
LoRa.beginPacket();
LoRa.print("Rotations: ");
LoRa.print(valRotary);
LoRa.println(", ");
LoRa.endPacket();
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(3000);
}
void doEncoder()
{
if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB))
{
encoder0Pos++;
}
else
{
encoder0Pos--;
}
valRotary = encoder0Pos / 1.99; // Calibration for 100 positions per rotation
}
[/code]
What I think is wrong but cant figure out how to fix:
I believe my issue lies in either the interrupt or my inability to get the doEncoder() function to work inside of the loop(). Ive attempted to assign the doEncoder() function to a variable inside of loop() in a few different ways to no avail. I have also moved the interrupt around and altered it's parameters.
I think there could also be an issue with my valRotary if statements and their relation to the doEncoder() function.
I may be in over my head here. This is my first post and I hope I have included everything needed to get some hints.
Thank you!