Code that "breaks" arduino

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);
}


}
int twopin = 2;
 int threepin = 4;
 int fourpin = 7;

really?

sbrown:
I'm trying to create a code that checks the continuity in different cables.

Please rewrite your description (or whatever it is) using paragraphs. It is impossible to make sense of it when it is all one.

A good test of readability is to read your text out loud and only take a breath when you come to a paragraph break.

...R

Where exactly are you getting stuck?

Does the code compile?
Can you send it to the Arduino?
Can you send any other (example) sketch to the Arduino?
Do you see any response from the LED on pin 13 (using another sketch, or when you first connect the Arduino to the PC)?
Can you charge your phone from the USB port in question?

If the code doesn't compile then there is a program (likely syntax) problem.

If the sketch will compile but won't transfer it is not program problem.

If the sketch transfers but doesn't do what you want, then it is a logic problem within the program.

sbrown:
what?? Twopin, threepin and fourpin as in the number of pins on the connector. Do you have any input relevant to the question?

yes, use variable names that make sense of the code, not confuse a reader.

your code should look something like this:

const uint8_t testPins[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10
};

const uint8_t passLed = 13;
const uint8_t failLed = 12;

void setup() {
  for (auto& p : testPins) {
    pinMode(p, INPUT);
  }
  pinMode(passLed, OUTPUT);
  pinMode(failLed, OUTPUT);

}

void loop() {
  bool pass = true;
  for (auto& p : testPins) {
    if (digitalRead(p) == LOW) {
      pass = false;
    }
  }
  digitalWrite(passLed, pass? HIGH : LOW);
  digitalWrite(failLed, pass? LOW : HIGH);
}

With all fairness to Mr(s) sbrown, (s)he probably hasn't had time to read the posting guides at the top of page 1 of the forum.

edit

DKWatson:
With all fairness to Mr(s) sbrown, (s)he probably hasn’t taken the time to read the posting guides at the top of page 1 of the forum.

The code compiles, perhaps I spent to much time elaborating on what the code does and not enough on the actual problem.

PRIOR TO UPLOADING MY CODE:

Arduino operates as it should, any example code compiles and uploads and runs appropriately.

ONCE IVE UPLOADED MY CODE:

Initially it works as intended, but after some time behaves unpredictably. On top of this, it appears my reset button no longer works and I can no longer upload ANY code to the arduino.

This has happened on 2 different arduinos, I've got a third running on the exact same setup that works fine, only difference being I haven't uploaded this "code in question" to it.

Appreciate the help.

It is not the code that breaks the Arduino.

It is the other devices and the wiring only when you upload this sketch that breaks the Arduino.

.

ieee488:
It is not the code that breaks the Arduino.

It is the other devices and the wiring only when you upload this sketch that breaks the Arduino.

.

Can the code in conjunction with the wiring have a destructive effect on the arduino? Or is it just a specific wiring mishap that may have caused some damage? It just seemed unlikely to me considering I'm only using 5V, and the problem occurred on each of the arduinos I uploaded this code to but not on the one I didn't.

sbrown:
Can the code in conjunction with the wiring have a destructive effect on the arduino? Or is it just a specific wiring mishap that may have caused some damage? It just seemed unlikely to me considering I'm only using 5V, and the problem occurred on each of the arduinos I uploaded this code to but not on the one I didn't.

It is your circuit.

5V in conjunction with sufficient current can kill an Arduino.

Let the Arduino in question sit for a while, then load the sketch and run it with a voltmeter attached to the 5V pin. The program doesn't have a lot of spurs or options to trace through, so that leads me to think heat and regulator overload and shutdown.

If you upload the program and let it sit (ie., don't run any tests) does it continue to work?

If it isn't the program, then it is the hardware. Please post a schematic of the connections for the Arduino and test cables.

adwsystems:
Let the Arduino in question sit for a while, then load the sketch and run it with a voltmeter attached to the 5V pin. The program doesn’t have a lot of spurs or options to trace through, so that leads me to think heat and regulator overload and shutdown.

If you upload the program and let it sit (ie., don’t run any tests) does it continue to work?

If it isn’t the program, then it is the hardware. Please post a schematic of the connections for the Arduino and test cables.

I can’t say exactly because the arduino in questino wont allow me to upload code to it anymore, but I think you’re on to something. I remember the running from the arduino pins becoming hot when I first uploaded this code. And the part you said about “heat and regulator overload and shutdown” (specifically the “shutdown” part) sounds closely accurate to how my arduino is behaving (like a brick.)

Is there some sort of delays I could add to my code in order to avoid this issue?

I can upload the pcb schematic showing how the connections are to be made.

Avoid what issue? What is the issue? Can't fix what we don't know is broke.

We will wait on the schematic. Pictures of you Arduino setup might help too.

IGNORE the fact that on that schematic I have the connectors improperly wired to pins 0 and 1 on the PWM (should start at 2 and continue to pin 10)

The issue is that I can no longer upload code to my arduino (can't communicate with it at all) and the code that is on it already (code in question) no longer behaves correctly. This has happened on 2 different arduino's after I've uploaded this code to them. I borrowed a third from a friend and didn't upload this code to it, I had no issues.

Essentially it's broken, I'm not sure exactly where. I was wondering if there was an obvious "oh don't do that" in my code or with the wiring that is required because of the code. I can do more research if more information is needed. I'm new to this kind of thing so I don't know exactly how to test for broken components or what components to look at first but a voltmeter sounds like a good start.

What you where saying about over-heating sounds accurate though, because it was getting really hot when I had first uploaded this code and it was working correctly.

Code by itself can never break the Arduino. While there are commands you can send to it which will permanently 'brick' it, the Arduino editor is carefully designed to never send those commands.

The power regulator is easily destroyed with as little as 12V input, depending on output current.

Plugging in USB while also powered from another source can burn out almost anything.

Are you absolutely sure there is nothing connected to pin 0 and 1 when programming?

adwsystems:
Avoid what issue? What is the issue? Can't fix what we don't know is broke.

The "issue" I was talking about here was the heat and regulator overload you suggested may be happening.

If reset button no longer does anything (that is, it doesn't even make the onboard LED blink, while the onboard power LED is on (if it's not, the problem is damage to power supply circuitry preventing the board from getting power)), that implies that either something is connected to reset button preventing it from going low (this would explain upload problems) or the chip is seriously damaged.

Damage like that cannot come from code. I suspect the problem is with your attached circuit. The easiest way to damage the microcontroller chip is to apply voltage higher than Vcc or lower than Ground to any pin (bear in mind that when the device is powered off, Vcc is ~0)

Overheating the regulator usually just takes out the regulator - it doesn't keep you from reprogramming it when powered from USB.

MorganS:
The power regulator is easily destroyed with as little as 12V input, depending on output current.

Plugging in USB while also powered from another source can burn out almost anything.

Are you absolutely sure there is nothing connected to pin 0 and 1 when programming?

I do remember having the USB and the power brick plugged in at the same time, and it is possible I had accidentally or momentarily plugged into the “0 and 1” pins. I’m going to purchase a couple more and try again on one of them, making sure to be careful of these things. I didn’t realize that I could potentially “brick” it so easily

DrAzzy:
If reset button no longer does anything, that implies that either something is connected to reset button preventing it from going low (this would explain upload problems) or the chip is seriously damaged.

Damage like that cannot come from code. I suspect the problem is with your attached circuit. The easiest way to damage the microcontroller chip is to apply voltage higher than Vcc or lower than Ground to any pin (bear in mind that when the device is powered off, Vcc is ~0)

I don’t have anything connected to the reset pin, it’s possible I did one of the two things you mentioned in that last paragraph (I was messing with wiring while this code was running.)

Is there a common way that either of those things can be done while messing with wiring on an arduino? I was trying to set up a test run just using a wire running from 5v and then connecting multiple wires to that one wire, then running those wires to the pwm pins.

BulldogLowell:
yes, use variable names that make sense of the code, not confuse a reader.

your code should look something like this:

No idea how to read that code, but I’ll test it out. Thank you!

sbrown:
No idea how to read that code, but I'll test it out. Thank you!

well, now we are even!

now that I see your schematic, I would think you want three sets of indicators, one for each cable, no?