0

Hey I am working on a small project and I somehow managed to cause that setup is running more than once and on top of that new code won't upload to the board. I am not entirely sure what it is I've done wrong but I can't find answer so I am asking you guys.

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  // I want to make some sound to tell the user to start measuring so I'll put some code below
  Serial.print("Inicialize setup");
  while (millis() < 5000)
  {
    if (millis() % 1000)
    {
      Serial.print(millis());
      Serial.print("\n");
    }
    if (AnalogValue > MaxIntensity)
    {
      MaxIntensity = AnalogValue;
    }
    if (AnalogValue < MinIntensity)
    {
      MinIntensity = AnalogValue;
    }
  }
  Serial.print("setup done");
  float MaxDist = Distance(MaxIntensity);
  float MinDist = Distance(MinIntensity);
  float delta = MaxDist - fabs(MinDist);
  Segmentlength = delta / 7;

  for (int i = 7; i > 0; i--)
  {
    TonesUpperValues[i] = MinDist + (Segmentlength * i);
  }
}

And this is the function used above

float Distance(int Intensity)
//function to calculate distance from intensity
{
  float d = (1 / (exp(Intensity + EOffset))) + Offset;
  d *= -1;
  return d;
}

In serial monitor I see the millis being printed over and over and also some text which I am guessing is "inicialize setup" and "Setup done" When I try to upload the code this error appears:avrdude: ser_open(): can't open device "\\.\COM5": Access is denied. even though : Auto-detected: COM5 Also previous code(the one that can't be overwritten by a new upload and is currently running had this warning in function that was not part of setup:src\main.cpp:38:1: warning: control reaches end of non-void function [-Wreturn-type]

Do you guys see any mistakes that I've made?

  • Try loading a simple blink program - follow the suggestions of @Arkadip below. You can tell the Serial monitor not to scroll; try to capture the start of output. As long as you get compile errors, you aren't uploading new code. This `millis() % 1000` just gives you a number between 0 and 999 - it will be true 999/1000 times so that's why you're getting a *LOT* of numbers printed. You could try this instead: `(millis() % 1000) == 0` but that will rarely be true - you'd likely miss 1000,2000,3000,4000 before the first `if` takes you out of the loop in `setup()`.. – aMike Jan 06 '21 at 23:47

1 Answers1

0

Looking at the code snippet, I am not totally sure what's happening with your code. But I found some problems, which I hope will help you.

  1. For avrdude: ser_open(): can't open device "\\.\COM5": Access is denied. issue,
  • Arduino not always recognize the board automatically. So, make sure you have selected the correct com port using Tools > Port. You must see your Arduino there

  • If the board is not there, make sure you have connected the Arduino correctly i.e the LED on the Arduino is lit.

  • If you have connected the board correctly, but the board is still not shown, then make sure the Arduino USB driver is installed properly. ReInstall the Arduino IDE to install the USB driver.

  1. Your code snippet does not have the void loop(). If you are not using the void loop(), then leave it blank, but never miss it. It will lead to a compiler error.

  2. For warning: control reaches end of non-void function [-Wreturn-type] error, Please go through this What does "control reaches end of non-void function" mean?

Hope this will help you. Thanks