QUESTION: Why does this code "break" my arduino / Does anything about this code raise a red flag?
I've created a code that simply lights a green LED when a cable is continuous. It checks to see if certain PWM pins are set to "HIGH" simultaneously. There's three different cables I'm checking: A 2 wire cable, a 3 wire cable, and a 4 wire cable.
For example: I want to check to see if any of the wires in the 2-wire cable are broken. I have a 2 pin connector set up for each end of the cable. Using a PCB, one connector receives 5V into each pin and sends it through the cable, the other connector sends trace to a respective digital PWM pin on the arduino (in this case pins 2 and 3 respectively.) As you can see in my code, the arduino will check to see if pins 2 and 3 are receiving a "HIGH" input and, if so, will set pin 13 to "HIGH" in order to light the green LED and set pin 12 to "LOW" in order to dim the red LED (which indicates no continuity.)
This same process is repeated for the 3-wire cable and 4-wire cable.
My question isn't about the code itself (I recognize that only 1 cable can be plugged in at a time or else it will give false readings.) My problem is that when I uploaded this code to my arduino, eventually it stopped working properly and I could no longer upload ANY could to it, even a blank one.
I tried all sorts of trouble-shooting found online but I can no longer communicate with my arduino. The problem seems to be my code, I got a new arduino and the problem happened again after uploading it. I now have a third arduino that I didn't upload this code to, I've been tinkering with it and it works just fine. I'm new to arduino, so I'm wondering if there's something I'm just not getting that's causing my code to "break" my arduino. Help would be greatly appreciated.
The Code:
void setup() {
int x=2;
while (x<11) {
pinMode(x, INPUT);
x++;
}
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop() {
//
digitalWrite(12, HIGH); // 12 is red LED, no continuity
digitalWrite(13, LOW); // 13 is green LED, continuity
int pin=0;
int twopin = 2;
int threepin = 4;
int fourpin = 7; // declare variables and set green LED to off and red LED to on by default
while (digitalRead(2) == HIGH) { //check 1st connectors (2pin)
while (twopin<4) {
if (digitalRead(twopin) != HIGH) {
pin = 12;
break;
}
else {
pin = 13;
}
twopin++;
}
if (pin == 13) {
digitalWrite(12, LOW);
}
digitalWrite(pin, HIGH);
}
while (digitalRead(4) == HIGH) { //check second connectors (3pin)
while (threepin<7) {
if (digitalRead(threepin) != HIGH) {
pin = 12;
break;
}
else {
pin = 13;
}
threepin++;
}
if (pin == 13) {
digitalWrite(12, LOW);
}
digitalWrite(pin, HIGH);
}
while (digitalRead(7) == HIGH) { //check third connectors (4pin)
while (fourpin<11) {
if (digitalRead(fourpin) != HIGH) {
pin = 12;
break;
}
else {
pin = 13;
}
fourpin++;
}
if (pin == 13) {
digitalWrite(12, LOW);
}
digitalWrite(pin, HIGH);
}
}