Hi everybody. I am working on making an automated testing tool for something at work. But I think I have killed my mega. It is still "functional" but I got odd behavior on one of the pins activating one of the relays. It behaves like it is inverted. When I boot up the code, I have a for loop to write all of them low, and it turns on. When I go farther down the code to turn it on, it turns off. And then I noticed the chip on the mega get hot to the touch. And even gets hot when nothing than the USB is connected. I have already killed a nano, but that one ended with no serial connection, and a hot chip. Code is still work in progress. Lots up debugging serial prints. And some unused integers. The function "serialReturnChar I could not get working, so it is not used. Not my main focus now, but I dont mind some tips on how to return chars from serial. What I had in mind was mostly using it as y/n (yes/no) scenarios when I need user input on something I cant do electrical. ![]()
Automatic_liftboks_test.ino
#include "myDeclarations.h"
int testPwrOn[1];
int results[15];
const unsigned int MAX_MESSAGE_LENGTH = 12;
static char message[MAX_MESSAGE_LENGTH];
int pwrOnPin[2]{ 32, 33 };
int pwrOn[2];
int testSelect = 0;
void setup() {
Serial.begin(9600);
while (!Serial)
;
for (int i = 22; i <= 27; i++) {
pinMode(i, OUTPUT);
delay(100);
digitalWrite(i, LOW);
delay(100);
Serial.print(i);
Serial.println("pinMode OUTPUT");
}
for (int i = 32; i <= 45; i++) {
pinMode(i, INPUT);
Serial.print(i);
Serial.println("pinMode INPUT");
}
delay(1000);
Serial.println("Setup done");
}
void loop() {
int runOnce = 0;
Serial.println("\n \n \n \n What test to run? Type the number to your desired test and press enter to start");
Serial.println("1 Full test");
Serial.println("2 Idle test");
Serial.println("3 Relay test");
//Serial.println("Geting input from user");
testSelect = serialReturn();
Serial.print("Number from user retrived");
Serial.println(testSelect);
if (testSelect == 1) {
Serial.println("Full test");
idleTest();
} else if (testSelect == 2) {
Serial.println("Idle test");
idleTest();
} else if (testSelect == 3) {
Serial.println("Relay test");
relayTest();
}
}
int serialReturn() {
int number = 0;
while (number < 1) {
if (Serial.available()) {
static char message[MAX_MESSAGE_LENGTH];
static unsigned int message_pos = 0;
char inByte = Serial.read();
if (inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1)) {
message[message_pos] = inByte;
message_pos++;
} else {
message[message_pos] = '\0';
number = atoi(message);
message_pos = 0;
}
}
}
//Serial.println("Number recived: ");
//Serial.println(number);
return (number);
}
/*
void serialReturnChar() {
char returnChar = 0;
static unsigned int message_pos;
//Check to see if anything is available in the serial receive buffer
while (returnChar < 1) {
if (Serial.available()) {
//Create a place to hold the incoming message
static unsigned int message_pos = 0;
//Read the next available byte in the serial receive buffer
char inByte = Serial.read();
//Message coming in (check not terminating character) and guard for over message size
if (inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1)) {
//Add the incoming byte to our message
message[message_pos] = inByte;
message_pos++;
}
//Full message received...
else {
//Add null character to string
message[message_pos] = '\0';
//Print the message (or do other things)
Serial.println(message);
returnChar = 'message';
//Reset for the next message
}
}
}
}
*/
void idleTest() {
int input = 0;
Serial.println("Idle test starting");
Serial.println("Sett switch in OFF position. When ready type 1 and press enter");
while (input != 1) {
input = serialReturn();
Serial.print("Input: ");
Serial.println(input);
}
for (int i = 0; i <= 1; i++) {
pwrOn[i] = digitalRead(pwrOnPin[i]);
Serial.print("digitalRead pwrOnPin");
Serial.println(i);
Serial.print("pwrOn ");
Serial.println(pwrOn[i]);
if (pwrOn[i] == 1) {
Serial.print("pwrOn");
Serial.print(i);
Serial.println(" Pass");
} else {
Serial.print("pwrOn");
Serial.print(i);
Serial.println(" Fail");
}
}
}
void relayTest() {
Serial.println("To exit test type 9 and press enter");
int input = 0;
while (input != 7) {
input = serialReturn();
Serial.println(input);
int relay = input + 21;
digitalWrite(relay, !digitalRead(relay));
}
for (int i = 22; i <= 27; i++) {
Serial.println(i);
digitalWrite(i, LOW);
delay(100);
}
}
myDecleration.h
void idleTest();
int serialReturn();
void serialReturnChar();
void relayTest();
