Simple blinking sketch with analogread and if statements hanging

Dear Arduino-fellows,

I have a very simple project: I ordered a preassambled board with an ATMEGA328P and LEDs and soldered a 10k potentiometer to it to select different values by reading the signal. Flashing bootloader and programming the ATMEGA328P is not a problem but the code just randomly hangs at different positions.

I use the digital pin 7 to drive an NMOSFET, source is connected to ground, drain is connected to ground of the LEDs.

Please have a look at my code below. Thank you a lot!

int potentiometer = A0;
int led = 7;
int averagevalue = 0;

void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  // put your setup code here, to run once:
}

void loop() {
int averagevalue = analogRead(A0);
  
if(averagevalue >= 0 && averagevalue <= 100)
  Serial.println("Mode off");
  Serial.println("Driving led LOW");
  digitalWrite(led, LOW);
  delay(1000);
}

else if(averagevalue >= 101 && averagevalue <= 300) {
  Serial.println("Mode on");
  Serial.println("Driving led HIGH");
  digitalWrite(led, HIGH);
  delay(5000);
}
else if(averagevalue >= 301 && averagevalue <= 500) {
  Serial.println("Blink Slow");
  Serial.println("Driving led HIGH");
  digitalWrite(led, LOW);
  delay(1000);
  digitalWrite(led, HIGH);
  delay(1000);
}
else if(averagevalue >= 501 && averagevalue <= 700) {
  Serial.println("Blink MEDIUM");
  digitalWrite(led, LOW);
  delay(500);
  Serial.println("Driving led HIGH");
  digitalWrite(led, HIGH);
  delay(500);
}
else if(averagevalue >= 701 && averagevalue <= 900) {
  Serial.println("Blink Fast");
  Serial.println("Driving led HIGH");
  digitalWrite(led, HIGH);
  delay(100);
  digitalWrite(led, LOW);
  delay(100);
}
else if(averagevalue >= 901 && averagevalue <= 1000) {
  Serial.println("Blink Fast");
  Serial.println("Driving led HIGH");
  digitalWrite(led, HIGH);
  delay(50);
  digitalWrite(led, LOW);
  delay(50);
}
else{
  Serial.println("No mode detected");
  digitalWrite(led, LOW);
  delay(100);
}
}

That doesn't compile, in the first place.

But when I fixed it, it runs for me with just a normal led (no mosfet in the mix).

So I'm wondering how long I should sit here until

the code just randomly hangs at different positions

It's been running on my Uno for over 10 minutes now, with me giving the pot a nudge every now and then.

It's run for an hour, no problems other than the blinkenlite giving me a headache.

I use the digital pin 7 to drive an NMOSFET, source is connected to ground, drain is connected to ground of the LEDs.

You need to pos a schematic of that so we can see exactly how it is wired as your description is inadequate.
It doesn’t mention how this bunch is wired, what is the resistors you used and where the other end of the LEDs go.

Hand drawn is fine, Fritzing layout is not.

Hi Guys,

Thank you all! Very kind of you - the code works also for my Arduino UNO. I placed a reset button on my PCB since it hangs quite often (only if the pot is changed). It has to be related to the PCB - I’ve attached the eagle files. As NMOSFET, I am using this low threshold voltage one that works fine:
https://www.infineon.com/cms/de/product/power/mosfet/12v-300v-n-channel-power-mosfet/irlz44n/

Thank you again!

eagle_schematic_nmosfet.PNG

Well thank you for those schematics but sadly they are not very useful. This is because they don’t show the most important thing I asked about, which is how the LEDs are wired up, what resistors do you use and how they are powered.
A full schematic showing these things might help us spot what you are doing wrong.