I'm thinking to a remote platform that will use a IR remote, a servo and a joystick, among other stuff.
This is my first experience with these devices and I made a test platform based on a Nano Every + a serial LCD.
Each of the 3 components were positively tested by their own but when I combine them I felt into a serious problem: when I sent an IR command from the remote I get a continuos flow of erratic readings from the joistick.
In the GitHub page of IRremote library I saw some notes on using the analogWrite togheter with IRremote, but nothing on analogRead.
Later on there is a table that suggest (?) which pin to use with differente MCUs; for the ATmega 4809 it says:
ATmega4809 TCB0 A4
Does it mean that I absolutely have to use A4 to receive IR ?
This is a shortened code of my sketch:
/*
=================================================
TestBed: Test Servo + Joistick + IR receiver with remote
HW: Nano Every + serial I2C LCD
==================================================
*/
/* NOTE:
Servo position is set by some IR commands from the remote
and displayed on the LCD.
The Joystick is intended to move the receiving unit (or other devices).
These 2 inputs (servo and joy) are used in different not overlapping moments
*/
#define DECODE_NEC // Includes Apple and Onkyo
#include <Arduino.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <Servo.h>
#include <IRremote.hpp>
#define IR_PIN SS
#define SERVOPIN 10
#define JOYX A0
#define JOYY A1
#define JOYSW A2
uint8_t Xvalue, Yvalue, lastX, lastY;
uint8_t JoyDelta = 7;
uint8_t switchStat, oldStat;
uint8_t SWDelta = 128;
Servo myservo;
void setup() {
pinMode(SERVOPIN, OUTPUT);
pinMode(IR_PIN, INPUT);
pinMode(JOYSW, INPUT);
Serial.begin(9600);
Wire.begin();
IrReceiver.begin(IR_PIN);
myservo.attach(SERVOPIN);
myservo.write(90); // set servo to mid-point
...
}
void loop() {
if (IrReceiver.decode()) {
...do something
IrReceiver.resume(); // Enable receiving of the next value
}
/* ========= here comes the problem ==========*/
Xvalue = analogRead(JOYX);
delay(2);
Yvalue = analogRead(JOYY);
// impose a dead zone to prevent unwanted changes due to noise
if (abs(Xvalue - lastX) > JoyDelta || abs(Yvalue - lastY) > JoyDelta) {
sprintf(buf, "X=%u , Y=%u", Xvalue, Yvalue);
Serial.println(buf);
lastX = Xvalue;
lastY = Yvalue;
}
switchStat=analogRead(JOYSW);
if (abs(switchStat - oldStat) > SWDelta) {
Serial.print("Switch=");
Serial.println(switchStat);
oldStat=switchStat;
}
}
Thanks