Double run setup

Hi I have situation when I start arduino it run setup message twice, this message shows in serial monitor:
"Set coeficents "
"Set coeficents "
After that I go to main loop and wait for serial input of 24 float numbers(see functon in code: void modArr). Here when I enter 24 numbers it take them but after that he puts 24 zeros again. When I send 48 numbers the he store them in array by name: static float coef[24];

Full code:

#include <TinyGPS++.h>
#include <AltSoftSerial.h>
#include <EEPROM.h>

static const uint32_t GPSBaud = 38400;      
static float coef[24]; 
// = {5.5, 3.75, 4.0, 3.95, 4.0, 4.1, 4.17, 4.22, 4.21, 4.13, 4.2, 4.23, 4.22, 4.19, 4.17, 4.17, 4.21, 4.21, 4.18, 4.16, 4.13, 4.16, 4.2, 4.23 };
static float kmh[24] = {10, 20, 25, 38, 50, 61, 72, 83, 95, 109, 119, 130, 142, 155,168, 180, 190, 202, 215, 220, 230, 241, 250, 260};
int spd=0;
float spdCoef;
int imp;


TinyGPSPlus gps;

AltSoftSerial ss;
int phrase[3];         
String stack="";
bool start = false;

void setup()
{
  Serial.begin(38400);
  ss.begin(GPSBaud); 
  Serial.println("Set coeficents");  

}

void loop()
{  
    if(start==false){  
    if(Serial.available()>0){
      modArr(coef, 24);
      start = true;
      }
    }
    
  if(start==true){ 
  
  Serial.print(F("Brzina = "));
  printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  Serial.println(F("Km/h"));
  Serial.println();
  smartDelay(100);

   spd = gps.speed.kmph();
   spdCoef = spdGps(spd);
   Serial.println(spdCoef);
   imp = spd * spdCoef;
   Serial.println(imp);
   Serial.println(coef[5]);
   Serial.println(kmh[5]);

  if (millis() > 5000 && gps.charsProcessed() < 10)
    Serial.println(F("No GPS data received: check wiring"));   
  
   }
}

static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
  smartDelay(0);
}
int spdGps(int spdVal){
  
    for(int i=0;i<sizeof(kmh);i++){
       if (spd>kmh[i] && kmh[i+1]>spd) {
        int y = map(spdVal, kmh[i], kmh[i+1], coef[i], coef[i+1]); 
        return y;                     
       } 
    }
}

void modArr(float arr[], int sizeArr){  
 
  while (Serial.available() > 0){
    for (int i=0; i<sizeArr; i++)
    {
     arr[i] = Serial.parseFloat();
     Serial.println(arr[i]);
     Serial.println(i);
    }
  }
  
} 

Hi, @milosk
Welcome to the forum.

Please read the post at the start of any forum , entitled “How to use this Forum”.

To add code please click this link;

What model Arduino are you using?
Can I suggest

Serial.begin(38400);

to

Serial.begin(115200);

And change your IDE monitor to that baud rate.

Thanks… Tom… :grinning: :+1: :coffee: :australia:

Hi Tom, thanks. I tried that :slight_smile: i used 9600 to 115200 but is same. I think also this gps antena can work on maximum 38400 so I left same baud at the end of antena and arduino uno. Chip is atmeg328.

When you upload a sketch it will run for about 150 milliseconds before the Serial Monitor connects. Serial Monitor has to wait for the uploader to close the serial connection before it can open the serial connection. When the serial connection opens, it resets the board.

I’ve started adding a 200 millisecond delay after Serial.begin(); to make sure none of my sketch gets run between the time of upload and time Serial Monitor connects.

This mostly causes problems when a sketch reads and writes EEPROM immediately on startup. For example, if you read a location in EEPROM, increment the value, and write it back, the behavior will show it incremented twice on each upload.

2 Likes

This solved the problem… Thanks

Yes. I work with LEDs so my solution is to have a very small sketch open that turns off all the LEDs, I upload that while thinking about why I’ve gone wrong and near blinded myself.

So that first 200 mS is not running the sketch I am working on.

Edit: shower realization: that won’t fix the double eprom bump, as my sketch will run whilst the turn-off sketch it being uploaded, doh!

But it is handy to turn off the LEDs, like a strip you’ve accidently turned full on OMG those things be bright.

a7

1 Like