So I'm having a very weird problem with my Nano Every. It's providing 5v to a KY-040 rotary encoder and an Adafruit 2.8" tft display and powers on only when connected to my desktop PC. When connected to any other power source I have available, the display backlight will power up, however the arduino will not run its sketch (display blank, no flashing leds on the arduino). This is true when connected to my laptop, a regulated benchtop PSU at any voltage and any current supply through the Vin pin, and a 5v-1.5A phone charger through the USB port.
If I disconnect the rotary encoder, the arduino now powers on correctly from my external power supply. If I then reconnect the encoder, the arduino works, but the encoder does not. The encoder only works when connected to my PC via USB. I have tested this with two different Nano Every boards and two different KY-040 encoders. Does anyone have an idea of what could be wrong here?
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Encoder.h>
#include <avr/wdt.h>
Encoder myEnc(15, 16);
const int encoder_button_pin = 14;
const int TFT_DC = 9;
const int TFT_CS = 10;
const int stallsense1 = 7;
const int stallsense2 = 4;
const int motordirection1 = 5;
const int motordirection2 = 2;
const int motorspeed1 = 6;
const int motorspeed2 = 3;
long cycleset1 = 100000;
long cycleset2 = 750000;
int speedarray[2] = {motorspeed1, motorspeed2};
int directionarray[2] = {motordirection1, motordirection2};
long cyclearray[2] = {0, 0};
int motorpwm = 5;
int textsize = 2;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void changedirection(int motornum) {
analogWrite(speedarray[motornum], 0);
delay(100);
digitalWrite(directionarray[motornum], !digitalRead(directionarray[motornum]));
delay(50);
analogWrite(speedarray[motornum], motorpwm * 255 / 100);
cyclearray[motornum] = cyclearray[motornum] + 1;
display_current_cycle(motornum);
}
void display_current_cycle(int motornum) {
if (motornum == 1) {
tft.setTextSize(textsize);
tft.setCursor(.5 * 320 / 8, 2 * 240 / 8);
tft.print(cyclearray[1]);
tft.setTextSize(textsize / 2);
tft.print(" / ");
tft.print(cycleset1);
}
if (motornum == 2) {
tft.setTextSize(textsize);
tft.setCursor(0.5 * 320 / 8, 6.5 * 240 / 8);
tft.print(cyclearray[2]);
tft.setTextSize(textsize / 2);
tft.print(" / ");
tft.print(cycleset2);
}
}
void updatepwm() {
tft.setCursor(18 * 320 / 32, 2 * 240 / 12);
tft.print("Motor Speed: ");
tft.print(motorpwm);
tft.print("% ");
}
void fillmenu() {
tft.setTextSize(1);
tft.setCursor(18 * 320 / 32, 240 / 12);
tft.print("Setpoint: ");
tft.print(cycleset1);
updatepwm();
tft.setCursor(18 * 320 / 32, 3 * 240 / 12);
tft.print("Pause");
tft.setCursor(18 * 320 / 32, 4 * 240 / 12);
tft.print("Reset Counter");
tft.setCursor(18 * 320 / 32, 8 * 240 / 12);
tft.print("Setpoint: ");
tft.println(cycleset2);
tft.setCursor(18 * 320 / 32, 9 * 240 / 12);
tft.println("Pause");
tft.setCursor(18 * 320 / 32, 10 * 240 / 12);
tft.print("Reset Counter");
}
void setup() {
// put your setup code here, to run once:
//wdt_enable(WDT_PERIOD_2KCLK_gc); //4 second watchdog timer to recover from code freezes
pinMode(stallsense1, INPUT);
pinMode(stallsense2, INPUT);
pinMode(motordirection1, OUTPUT);
pinMode(motordirection2, OUTPUT);
pinMode(motorspeed1, OUTPUT);
pinMode(motorspeed2, OUTPUT);
attachInterrupt(digitalPinToInterrupt(stallsense1), [] () {
changedirection(1);
}, RISING);
attachInterrupt(digitalPinToInterrupt(stallsense2), [] () {
changedirection(2);
}, RISING);
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setRotation(3);
tft.setTextSize(textsize);
tft.setTextWrap(false);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
//tft.drawRect(320/12, 240/2, 11*320/12, 11*240/12, ILI9341_WHITE);
tft.setCursor(320 / 8, 240 / 8);
tft.print("Helm 1: ");
display_current_cycle(1);
tft.setTextSize(textsize);
tft.setCursor(320 / 8, 5.5 * 240 / 8);
tft.print("Helm 2: ");
display_current_cycle(2);
fillmenu();
}
void loop() {
// get encoder position
long Encoder_position = myEnc.read();
// get button state
bool Encoder_button = digitalRead(encoder_button_pin);
// check if encoder has been rotated clockwise
if (Encoder_position <= -3)
{
// GO UP
/*
if(motorpwm<100){
motorpwm = motorpwm + 5;
updatepwm();
Serial.print("motorpwm: ");
Serial.println(motorpwm);
}
*/
digitalWrite(motordirection1, HIGH);
analogWrite(motorspeed1, motorpwm);
delay(100);
analogWrite(motorspeed1, 0);
// init encoder for the next step
myEnc.write(Encoder_position + 4);
}
// check if encoder has been rotated counter clockwise
else if (Encoder_position >= 3)
{
// GO DOWN
/*
if(motorpwm>0){
motorpwm = motorpwm - 5;
updatepwm();
Serial.print("motorpwm: ");
Serial.println(motorpwm);
}
*/
digitalWrite(motordirection1, LOW);
analogWrite(motorspeed1, motorpwm);
delay(100);
analogWrite(motorspeed1, 0);
// init encoder for the next step
myEnc.write(Encoder_position - 4);
}
// check if button has been pressed
if (!Encoder_button) {
// PRESS ENTER
if (motorpwm < 100) {
motorpwm = motorpwm + 5;
}
else {
motorpwm = 5;
}
updatepwm();
delay(200);
/*
motorpwm = 0;
updatepwm();
Serial.print("motorpwm: ");
Serial.println(motorpwm);
delay(100);
*/
}
//wdt_reset();
}
