Author Topic: Serial interface acting really weird  (Read 5756 times)

0 Members and 1 Guest are viewing this topic.

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Serial interface acting really weird
« on: July 12, 2016, 03:47:14 am »
I have a basic Atmega168p setup with arduino and using a MCP2221 for serial I/O.  I had this working fine, but suddenly it's jibbing right out.

I also have a LCD displaying stuff and it goes completely nuts with junk, and the serial is just outputting lot of garbage.  If I comment out the code block that starts with "if(cmdlevel==0)" then it works fine.  If I change the values that it's looking for it does not matter either.  I'm completely confused as to why it's acting this way, it was fine before.   Maybe there's something I'm completely missing here, thanks in advance for the help.

Code: [Select]

/*
Solar power pack controller code
*/


// include the LCD library code:
#include <LiquidCrystal.h>
#include <EEPROM.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(4, 10, 9, 6, 7, 8);  //RS, ENBL, D4, D5, D6, D7


int incomingByte = 0;
char serialbuffer[51];
int serialpos=0;

int cmdlevel=0; //command level.  0=main command, 1+ see command levels below
int cmdsublevel=0;  //sub levels used within command functions

/* command levels :
1: callibration menu
2: float voltage adjust
*/





int lcddelayint=0; //used for LCD delay feature (to display something for extended time)


//pin assignments:

int PINsoftoff=2;
int PINbuckpwm=3;
int PINloadrelay=5;
int PINampspv=A3;
int PINampsload=A1;
int PINvoltspv=A4;
int PINvoltsload=A2;


//callibration vars:  (nvram)
int nvram_solvolt;
int nvram_solamps;
int nvram_loavolt;
int nvram_loaamps;






void ConvNum(char * out, long num)
{
    if(num<0)num=0;
    if(num>9999999)num=9999999;


    char numstrrev[8] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
    char numstr[8];

    long tempint=num;
    int digits=0;

    /*
    9,999,999 =  999.    7d = 3 (comma pos)
    999,999   =  99.9    6d = 2
    10,000    =  1.00    5d = 1
    1,000     =  0.10    4d = 1
    100       =  0.01    3d = 1
    10        =  .001    2d = 0
    0-9       =  .000    1d = 0
    */

    //convert num to char array (in reverse)
    for(int i=0;i<8;i++)
    {
        int tmp = tempint%10;
        numstrrev[i]=0x30+tmp;

        //debug:
        //cout<<i<<":"<<tmp<<"  tempint:"<<tempint<<"  numstrrev:"<<numstrrev<<endl;


        tempint=tempint-tmp;
        tempint=tempint/10;

        digits++;
        if(tempint==0)break;
    }

    //cout<<" digits:"<<digits<<endl;//debug


    int pos=0;

    if(digits<=1)
    {
        out[0]='.';
        out[1]='0';
        out[2]='0';
        out[3]='0';
    }
    else if(digits==2)
    {
        out[0]='.';
        out[1]='0';
        out[2]='0';
        out[3]=numstrrev[1];
    }
    else if(digits==3)
    {
        out[0]='0';
        out[1]='.';
        out[2]='0';
        out[3]=numstrrev[2];
    }
    else if(digits==4)
    {
        out[0]='0';
        out[1]='.';
        out[2]=numstrrev[3];
        out[3]=numstrrev[2];
    }
    else if(digits==5)
    {
        out[0]=numstrrev[4];
        out[1]='.';
        out[2]=numstrrev[3];
        out[3]=numstrrev[2];
    }
    else if(digits==6)
    {
        out[0]=numstrrev[5];
        out[1]=numstrrev[4];
        out[2]='.';
        out[3]=numstrrev[3];
    }
    else if(digits==7)
    {
        out[0]=numstrrev[6];
        out[1]=numstrrev[5];
        out[2]=numstrrev[4];
        out[3]='.';
    }
    else
    {
        out[0]='9';
        out[1]='9';
        out[2]='9';
        out[3]='.';
    }



    out[4]=0x0;
}




bool lcddelay(int setto)
{
  if(setto>0)
  {
    lcddelayint=setto+(millis() / 1000);
    return false;
  }
 
  if(lcddelayint>(millis() / 1000))return true;
 
  return false;
 
}



void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
 
   Serial.begin(9600);           // set up Serial library at 9600 bps
 
   pinMode(PINsoftoff, INPUT);
   pinMode(PINbuckpwm, OUTPUT);
   pinMode(PINloadrelay, OUTPUT);
}


void CMD_help()
{
    Serial.print("\r\n\r\n-------------------------------------------------------------------------------\r\n"); 
    Serial.print("Solar Power System [updated July 11 2016]  -  [url=http://www.iceteks.com\]www.iceteks.com\[/url]r\n");
    Serial.print("-------------------------------------------------------------------------------\r\n");
    Serial.print("help             Show this screen\r\n");
    Serial.print("pcloadletter     Make the LCD say \"PC LOAD LETTER\"\r\n");   
    Serial.print("eeprompdump      Dump contents of eeprom (NVRAM)\r\n");   
    Serial.print("cal              volt/amp calibration\r\n");   
}


void CMD_pcloadletter()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" PC LOAD LETTER ");
 
  lcddelay(30);
}



void CMD_eepromdump()
{
  int address=0;
  byte value;
 
  Serial.print("Dumping EEPROM...\r\n\r\n");
 
do
{
    // read a byte from the current address of the EEPROM
  value = EEPROM.read(address);

  Serial.print(address);
  Serial.print("\t");
  Serial.print(value, DEC);
  Serial.println();

  /***
    Advance to the next address, when at the end restart at the beginning.

    Larger AVR processors have larger EEPROM sizes, E.g:
    - Arduno Duemilanove: 512b EEPROM storage.
    - Arduino Uno:        1kb EEPROM storage.
    - Arduino Mega:       4kb EEPROM storage.

    Rather than hard-coding the length, you should use the pre-provided length function.
    This will make your code portable to all AVR processors.
  ***/
 
  address = address + 1;
}while(address < 10);
 
Serial.print("\r\n\r\nDone.\r\n");

}



void CMD_cal()
{
 /* int calvalue=0;
 
  calvalue = Serial.read()+0x30;
 
  if(calvalue<1 || calvalue>4)
  {
    Serial.print("Usage: cal [1-4]    1:sol volts, 2: sol amps, 3: load volts, 4: load amps\r\n");
  }*/
 
  /*
  cmdsublevels:
  0 = main menu
  1 = solvolt
  2 = solamps
  3 = loavolt
  4 = loaamps
  */
 
 
 
 
  if(cmdsublevel==0)
  {
      if(serialpos<=0)
      {
        Serial.print("Callibration menu options:\r\n");
        Serial.print("solvolt        solar voltage\r\n");
        Serial.print("solamps        solar amps\r\n");
        Serial.print("loavolt        load voltage\r\n");
        Serial.print("loaamps        load amps voltage\r\n");
        Serial.print("quit         Quit this menu\r\n");
      }
      else if(strcmp("solvolt",serialbuffer)==0)cmdsublevel=1;
      else if(strcmp("solamps",serialbuffer)==0)cmdsublevel=2;
      else if(strcmp("loavolt",serialbuffer)==0)cmdsublevel=3;
      else if(strcmp("loaamps",serialbuffer)==0)cmdsublevel=4;
      else if(strcmp("quit",serialbuffer)==0)
      {
        cmdsublevel=0;
        cmdlevel=0;
      }

  }
  else if(cmdsublevel==1)
  {
     char tmpnum[10];
     
     if(serialpos<=0)
     {
       Serial.print("solvolt current value: ");
       
       itoa(nvram_solvolt,tmpnum,10);
       
       Serial.print(tmpnum);
       
       Serial.print("Enter new value:");
     }
     else
     {
       Serial.print("New value set to:");       
       Serial.print(serialbuffer);
       
       
       
     }
   
   
   
  }
 
 
 
 
 
 
}





void loop()
{

 
  //analogWrite(PINbuckpwm, 120);

  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0): 
  //lcd.setCursor(0, 1);
  // print the number of seconds since reset:
 // lcd.print(millis() / 1000);
 




  //LCD display update:
 
  //lcd.clear();
 
  if(!lcddelay(0))
  { 
   
    int num=0;   
    char numchar[5];   
   
    //Solar volts:
    ConvNum(numchar,(long)analogRead(PINvoltspv)*100);
   
    lcd.setCursor(0, 0);
    lcd.print("SOL: ");
   
    lcd.setCursor(5,0);
    lcd.print(numchar);
   
    lcd.setCursor(9,0);
    lcd.print("V ");
   
    //Solar amps:
    ConvNum(numchar,(long)analogRead(PINampspv)*100);
   
    lcd.setCursor(11,0);
    lcd.print(numchar);
   
    lcd.setCursor(15,0);
    lcd.print("A");
   
   
    //load volts:
    ConvNum(numchar,(long)analogRead(PINvoltsload)*100);
   
    lcd.setCursor(0, 1);
    lcd.print("LOA: ");
   
    lcd.setCursor(5,1);
    lcd.print(numchar);
   
    lcd.setCursor(9,1);
    lcd.print("V ");
   
    //load amps:
    ConvNum(numchar,(long)analogRead(PINampsload)*100);
   
    lcd.setCursor(11,1);
    lcd.print(numchar);
   
    lcd.setCursor(15,1);
    lcd.print("A");   
   

 
 
    lcddelay(2);
  }




  // Handle Serial comm:
 
  int stilltyping=true;
 
  if (Serial.available() > 0)
  {
          // read the incoming byte:
          incomingByte = Serial.read(); 
         
         
          //filter unwanted chars:               
          if(incomingByte<13 || (incomingByte>14 && incomingByte<32) || incomingByte>126)return;
         
          //echo back what we typed:               
          Serial.print((char)incomingByte);
         
          //if enter pressed or reached end of buffer, process buffer           
          if(incomingByte==13 || serialpos>=49)
          {
              //Serial.print("\r\n");
             
              stilltyping=false;

              Serial.flush();
          }
          else
          {               
            serialbuffer[serialpos]=incomingByte;
            serialbuffer[serialpos+1]=0;
            serialpos++;
          }
         
  }
 
 
  //handle command:
 
  if(!stilltyping)
  {
      if(cmdlevel==0)    //if I comment out this block, then it acts fine.
      {
          if(strcmp("help",serialbuffer)==0)CMD_help();
          else if(strcmp("pcloadletter",serialbuffer)==0)CMD_pcloadletter();
          else if(strcmp("eepromdump",serialbuffer)==0)CMD_eepromdump();
          else if(strcmp("cal",serialbuffer)==0)
          {
            cmdlevel=1;
          }
          else if(serialpos>0)
          {
            Serial.print("\r\nInvalid command: \"");
            Serial.print(serialbuffer);
            Serial.print("\", type help to see list of valid commands");
          }
      }
      else if(cmdlevel==1)
      {
        CMD_cal();
      }
   
   
   
   
   
      //decide which cursor to show, # for master menu, > for within a command
      if(cmdlevel==0)Serial.print("\r\n# "); 
      else Serial.print("\r\n> "); 
     
      //clear buffer
      serialpos=0;
      for(int i=0;i<51;i++)
      {
        serialbuffer[i]=0x0;
      } 
  }




 
}

« Last Edit: July 12, 2016, 03:52:03 am by Red Squirrel »
 

Online MK14

  • Super Contributor
  • ***
  • Posts: 4539
  • Country: gb
Re: Serial interface acting really weird
« Reply #1 on: July 12, 2016, 03:55:41 am »
I'm just guessing from a quick look.

The strcmp(), is the string ALWAYS null terminated ?

If NOT it could disappear for ages, trying to find the "null" in memory, hence messing up the operation.

Also you seem to NOT be using the switch command.
Rather than having lots of if's from 1 to 7 etc, it is usually neater/easier/better to use switch (case) statements.
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #2 on: July 12, 2016, 04:08:23 am »
Cases don't work with strings, so it's just easier to use ifs across the board as in some cases I need to check against strings or ints or combination.

The thing I don't get is that it almost seems like serial.available is returning a non zero value.  Since stilltyping gets set to true, and if that if statement is false (which it should be when sitting idle) then the next set should not even be getting executed.

As a side note, when I first plug in the USB, I keep getting this error when I try to connect to the port:

FATAL: cannot open /dev/ttyACM0: Device or resource busy

I have to wait like 20 seconds or so before it lets me.

Also if I replace if (Serial.available() > 0) with if(1==2) then I don't get the weird stuff going on.  So it's like if for whatever reason there is data on the serial when there should not be.   This is with the USB unplugged.
« Last Edit: July 12, 2016, 04:15:50 am by Red Squirrel »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11260
  • Country: us
    • Personal site
Re: Serial interface acting really weird
« Reply #3 on: July 12, 2016, 04:14:08 am »
I have to wait like 20 seconds or so before it lets me.
That's modem manager tries to probe the port to check if you have attached a modem.

Just goolge "modem manager Arduino" and you will find plenty of topics discussing the solution. The solution is to blacklist your device from modem manager probes.
Alex
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #4 on: July 12, 2016, 04:16:40 am »
I have to wait like 20 seconds or so before it lets me.
That's modem manager tries to probe the port to check if you have attached a modem.

Just goolge "modem manager Arduino" and you will find plenty of topics discussing the solution. The solution is to blacklist your device from modem manager probes.

Ok it's good to know it's not my code then... though the other issue is still baffling me.   It seems like Serial.available() is returning a non zero value when it should. (ex: USB not plugged in/not getting characters)
 

Online MK14

  • Super Contributor
  • ***
  • Posts: 4539
  • Country: gb
Re: Serial interface acting really weird
« Reply #5 on: July 12, 2016, 04:30:17 am »
A question, towards the top of your source code, you have:

        tempint=tempint-tmp;
        tempint=tempint/10;

Surely the bold line above, is redundant and NOT doing anything useful for you. Since the divide by 10, loses the last digit anyway ?
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #6 on: July 12, 2016, 04:31:08 am »
It definitely seems like Serial.available() is non zero, I just commented out the serial handling code and have it outputting to LCD.  It's random, sometimes it's 0 but still acts up, other times it's as high as 61, then goes down then back up. Completely messed up. 
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #7 on: July 12, 2016, 04:32:35 am »
A question, towards the top of your source code, you have:

        tempint=tempint-tmp;
        tempint=tempint/10;

Surely the bold line above, is redundant and NOT doing anything useful for you. Since the divide by 10, loses the last digit anyway ?

Can't see why this would be doing anything.  This is still a work in progress so lot of misc stuff to fix/test.  I was in the middle of it when it suddenly started to act this way.   Though I can't recall what that code is doing, will check that when I can get this to actually work properly.
 

Online MK14

  • Super Contributor
  • ***
  • Posts: 4539
  • Country: gb
Re: Serial interface acting really weird
« Reply #8 on: July 12, 2016, 04:38:57 am »
A question, towards the top of your source code, you have:

        tempint=tempint-tmp;
        tempint=tempint/10;

Surely the bold line above, is redundant and NOT doing anything useful for you. Since the divide by 10, loses the last digit anyway ?

Can't see why this would be doing anything.  This is still a work in progress so lot of misc stuff to fix/test.  I was in the middle of it when it suddenly started to act this way.   Though I can't recall what that code is doing, will check that when I can get this to actually work properly.

I agree, it should NOT be causing you any issues. It was just something I noticed, when looking through some of your code.
Possibly removing it later, is fine. Best to get your code working first.
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #9 on: July 12, 2016, 04:47:35 am »
It seems stilltyping seems to randomly get set to false too, even when serial.available is zero.  Somehow it seems random variables keep changing on me.  When it does that, I also noticed that all the analog inputs get set to 1024, well at least the ones that are on the LCD.  I'm starting to wonder if I somehow damaged the chip...  Is there a proper way to shut these down?  I've been turning it on and off a lot as I move the USB from the programmer to it's actual port.  I have a separate 5v psu to power it.  Wondering if I need to do more than just pull the plug.
« Last Edit: July 12, 2016, 04:51:33 am by Red Squirrel »
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Serial interface acting really weird
« Reply #10 on: July 12, 2016, 04:48:25 am »
To summarise three salient points from your post...

  > was working, not now
  > strange behaviour
  > if I remove some section of code it works

the first thing that would come to my mind is, am I running out of SRAM and trampling over data.

And now I have blindly (because I can't be bothered reading it) slapped your code into the Arduino IDE and compiled it..
  Global variables use 1,127 bytes of dynamic memory.

How much SRAM does the 168p have.... I think it's 1k?  So.... yeah, there's your problem, your ram is over-committed before you have even started doing anything on the stack.

Skimming over your code... lots of ram based strings.  Pull them out into PROGMEM (just use the F() macro).

And you might want to (use and) read the "Further Learning" I wrote here: http://sparks.gogo.co.nz/avr-ram-use.html

« Last Edit: July 12, 2016, 04:51:26 am by sleemanj »
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 
The following users thanked this post: wilfred, MK14

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #11 on: July 12, 2016, 05:02:19 am »
Oh wow I never would have even thought of going over.  That sorta crossed my mind but figured there was no way.   How did you find out the number that is being used?  When compiling it shows storage but I'm within the limit.

I will try to see if I can streamline my code better.   I brought the buffer down to 12 bytes but still doing it.    Though I'm wondering how much the LCD class uses maybe that's bringing me close to the end on it's own.
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #12 on: July 12, 2016, 05:16:06 am »
Ok so I took an Atmega 328 from an actual arduino and plugged it in (thank goodness I used a socket for this board) and now it works!

So maybe I really was going over the memory.  I guess I have to be very dilligent about usage.  Good thing I did not start using STL.... lol

Just wondering though, what happens if I allocate from the heap, is that good or bad?  Like using the new and delete operator?

Also what I found interesting is if I commented out code from a function that was not being called at all, it also fixed it.  Does all the memory get allocated ahead of time for each function/class?
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Serial interface acting really weird
« Reply #13 on: July 12, 2016, 05:22:03 am »
Oh wow I never would have even thought of going over.  That sorta crossed my mind but figured there was no way.   How did you find out the number that is being used?  When compiling it shows storage but I'm within the limit.

I will try to see if I can streamline my code better.   I brought the buffer down to 12 bytes but still doing it.    Though I'm wondering how much the LCD class uses maybe that's bringing me close to the end on it's own.

The numbers are those shown at in the Arduino console.

As mentioned, you have a lot of non-progmem strings.  When you use bare strings like that they occupy BOTH program memory AND ram.  That is why you should use the F() macro in Arduino where possible (all those Serial.print("....") become Serial.print(F("...."))) because the F macro forces the string to remain in program memory - but note that the thing you are using that F macro'd string with must understand what to do with the F macro'd string.  That measn you can't use strcmp with an F macro string, but you can use Serial.print with one.  For strcmp and the like, there are PROGMEM aware versions which, while they can't use F macro strings can use PROGMEM stored strings.

Note that the "Global Variables" value reported in the IDE includes both what you would think of as a global variable, and other things that use RAM all the time, like strings, and static local variables.

For example, all of this data is in your RAM all the time, this is the biggest offender...
Code: [Select]
  80010e: 01 0d 0a 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     .....-----------
  80011e: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     ----------------
  80012e: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     ----------------
  80013e: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     ----------------
  80014e: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     ----------------
  80015e: 2d 2d 2d 2d 0d 0a 00 53 6f 6c 61 72 20 50 6f 77     ----...Solar Pow
  80016e: 65 72 20 53 79 73 74 65 6d 20 5b 75 70 64 61 74     er System [updat
  80017e: 65 64 20 4a 75 6c 79 20 31 31 20 32 30 31 36 5d     ed July 11 2016]
  80018e: 20 20 2d 20 20 5b 75 72 6c 3d 68 74 74 70 3a 2f       -  [url=http:/
  80019e: 2f 77 77 77 2e 69 63 65 74 65 6b 73 2e 63 6f 6d     /www.iceteks.com
  8001ae: 5d 77 77 77 2e 69 63 65 74 65 6b 73 2e 63 6f 6d     ]www.iceteks.com
  8001be: 5b 2f 75 72 6c 5d 72 0a 00 68 65 6c 70 20 20 20     [/url]r..help   
  8001ce: 20 20 20 20 20 20 20 20 20 20 53 68 6f 77 20 74               Show t
  8001de: 68 69 73 20 73 63 72 65 65 6e 0d 0a 00 70 63 6c     his screen...pcl
  8001ee: 6f 61 64 6c 65 74 74 65 72 20 20 20 20 20 4d 61     oadletter     Ma
  8001fe: 6b 65 20 74 68 65 20 4c 43 44 20 73 61 79 20 22     ke the LCD say "
  80020e: 50 43 20 4c 4f 41 44 20 4c 45 54 54 45 52 22 0d     PC LOAD LETTER".
  80021e: 0a 00 65 65 70 72 6f 6d 70 64 75 6d 70 20 20 20     ..eeprompdump   
  80022e: 20 20 20 44 75 6d 70 20 63 6f 6e 74 65 6e 74 73        Dump contents
  80023e: 20 6f 66 20 65 65 70 72 6f 6d 20 28 4e 56 52 41      of eeprom (NVRA
  80024e: 4d 29 0d 0a 00 63 61 6c 20 20 20 20 20 20 20 20     M)...cal       
  80025e: 20 20 20 20 20 20 76 6f 6c 74 2f 61 6d 70 20 63           volt/amp c
  80026e: 61 6c 69 62 72 61 74 69 6f 6e 0d 0a 00 20 50 43     alibration... PC
  80027e: 20 4c 4f 41 44 20 4c 45 54 54 45 52 20 00 44 75      LOAD LETTER .Du
  80028e: 6d 70 69 6e 67 20 45 45 50 52 4f 4d 2e 2e 2e 0d     mping EEPROM....
  80029e: 0a 0d 0a 00 09 00 0d 0a 0d 0a 44 6f 6e 65 2e 0d     ..........Done..
  8002ae: 0a 00 43 61 6c 6c 69 62 72 61 74 69 6f 6e 20 6d     ..Callibration m
  8002be: 65 6e 75 20 6f 70 74 69 6f 6e 73 3a 0d 0a 00 73     enu options:...s
  8002ce: 6f 6c 76 6f 6c 74 20 20 20 20 20 20 20 20 73 6f     olvolt        so
  8002de: 6c 61 72 20 76 6f 6c 74 61 67 65 0d 0a 00 73 6f     lar voltage...so
  8002ee: 6c 61 6d 70 73 20 20 20 20 20 20 20 20 73 6f 6c     lamps        sol
  8002fe: 61 72 20 61 6d 70 73 0d 0a 00 6c 6f 61 76 6f 6c     ar amps...loavol
  80030e: 74 20 20 20 20 20 20 20 20 6c 6f 61 64 20 76 6f     t        load vo
  80031e: 6c 74 61 67 65 0d 0a 00 6c 6f 61 61 6d 70 73 20     ltage...loaamps
  80032e: 20 20 20 20 20 20 20 6c 6f 61 64 20 61 6d 70 73            load amps
  80033e: 20 76 6f 6c 74 61 67 65 0d 0a 00 71 75 69 74 20      voltage...quit
  80034e: 20 20 20 20 20 20 20 20 51 75 69 74 20 74 68 69             Quit thi
  80035e: 73 20 6d 65 6e 75 0d 0a 00 73 6f 6c 76 6f 6c 74     s menu...solvolt
  80036e: 00 73 6f 6c 61 6d 70 73 00 6c 6f 61 76 6f 6c 74     .solamps.loavolt
  80037e: 00 6c 6f 61 61 6d 70 73 00 71 75 69 74 00 73 6f     .loaamps.quit.so
  80038e: 6c 76 6f 6c 74 20 63 75 72 72 65 6e 74 20 76 61     lvolt current va
  80039e: 6c 75 65 3a 20 00 45 6e 74 65 72 20 6e 65 77 20     lue: .Enter new
  8003ae: 76 61 6c 75 65 3a 00 4e 65 77 20 76 61 6c 75 65     value:.New value
  8003be: 20 73 65 74 20 74 6f 3a 00 68 65 6c 70 00 70 63      set to:.help.pc
  8003ce: 6c 6f 61 64 6c 65 74 74 65 72 00 65 65 70 72 6f     loadletter.eepro
  8003de: 6d 64 75 6d 70 00 63 61 6c 00 0d 0a 49 6e 76 61     mdump.cal...Inva
  8003ee: 6c 69 64 20 63 6f 6d 6d 61 6e 64 3a 20 22 00 22     lid command: "."
  8003fe: 2c 20 74 79 70 65 20 68 65 6c 70 20 74 6f 20 73     , type help to s
  80040e: 65 65 20 6c 69 73 74 20 6f 66 20 76 61 6c 69 64     ee list of valid
  80041e: 20 63 6f 6d 6d 61 6e 64 73 00 0d 0a 23 20 00 0d      commands...# ..
  80042e: 0a 3e 20 00 53 4f 4c 3a 20 00 56 20 00 41 00 4c     .> .SOL: .V .A.L
  80043e: 4f 41 3a 20 00                                      OA: .
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 
The following users thanked this post: MK14

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Serial interface acting really weird
« Reply #14 on: July 12, 2016, 05:28:16 am »
Just wondering though, what happens if I allocate from the heap, is that good or bad?  Like using the new and delete operator?

Also what I found interesting is if I commented out code from a function that was not being called at all, it also fixed it.  Does all the memory get allocated ahead of time for each function/class?

Stack and Heap occupy the same memory space, one grows up from the bottom the other grows down from the top.  There is no memory protection of course, so your stack and heap can easily grow too large and trash one-another.

Generally speaking, stack is better used since it's cleared up for you as functions exit and their stack frame is dumped.  Also the compiler is smart and will use free registers if possible instead pushing onto the stack, but if you dynamically allocate on the heap of course  it can't do that.

As for the second question.  As I say, it's the strings are the biggest offender.  Initialisation data (fixed strings being the big consumer of this) has to be copied from program memory into RAM at boot, therefore it occupies ram all the time.  Use the PROGMEM keyword, or the F macro with strings and functions that are F macro aware to avoid this (the compiler leaves the data in program memory, it's up then to the code to manually copy it out of program memory into ram as it needs it).
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #15 on: July 12, 2016, 05:36:41 am »
Ohhh I see had no idea the static strings actually used memory.  Will look into that F macro then.    i might be able to switch back to my smaller chip and keep this one for the arduino. I actually originally wanted to buy that version of the chip but must have messed my order, so I may as well learn to conserve memory so I can make use of them.

But wow glad I figured out the issue, it was driving me nuts as it just made no sense what was going on.  Now at least I know where to concentrate.

I'm still not seeing that memory output though,  is that on compile or upload?  I'm using "upload using programmer" feature if that matters. 
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Serial interface acting really weird
« Reply #16 on: July 12, 2016, 05:41:25 am »
Ensure you have verbose output switched on in File>Preferences. 

The memory usage is printed at the end of the compile (verify in Arduino speak) cycle, hit the verify button.
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #17 on: July 12, 2016, 05:49:08 am »
yeah both are on, this is the output I get:


Code: [Select]
/usr/share/arduino/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -D__PROG_TYPES_COMPAT__ -I/usr/share/arduino/hardware/arduino/cores/arduino -I/usr/share/arduino/hardware/arduino/variants/eightanaloginputs -I/usr/share/arduino/libraries/LiquidCrystal -I/usr/share/arduino/libraries/EEPROM /tmp/build3260894923757074322.tmp/solarpowerpack.cpp -o /tmp/build3260894923757074322.tmp/solarpowerpack.cpp.o
solarpowerpack.ino: In function ‘void ConvNum(char*, long int)’:
solarpowerpack.ino:63:10: warning: unused variable ‘numstr’ [-Wunused-variable]
solarpowerpack.ino:98:9: warning: unused variable ‘pos’ [-Wunused-variable]
solarpowerpack.ino: In function ‘bool lcddelay(int)’:
solarpowerpack.ino:173:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
solarpowerpack.ino: In function ‘void loop()’:
solarpowerpack.ino:348:9: warning: unused variable ‘num’ [-Wunused-variable]
  Using previously compiled: /tmp/build3260894923757074322.tmp/LiquidCrystal/LiquidCrystal.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/EEPROM/EEPROM.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/WInterrupts.c.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/wiring_shift.c.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/realloc.c.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/malloc.c.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/wiring_pulse.c.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/wiring_analog.c.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/wiring_digital.c.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/wiring.c.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/Tone.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/Stream.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/USBCore.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/CDC.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/IPAddress.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/new.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/HardwareSerial.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/HID.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/WMath.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/main.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/WString.cpp.o
  Using previously compiled: /tmp/build3260894923757074322.tmp/Print.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/WInterrupts.c.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/wiring_shift.c.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/realloc.c.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/malloc.c.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/wiring_pulse.c.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/wiring_analog.c.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/wiring_digital.c.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/wiring.c.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/Tone.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/Stream.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/USBCore.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/CDC.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/IPAddress.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/new.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/HardwareSerial.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/HID.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/WMath.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/main.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/WString.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-ar rcs /tmp/build3260894923757074322.tmp/core.a /tmp/build3260894923757074322.tmp/Print.cpp.o
/usr/share/arduino/hardware/tools/avr/bin/avr-gcc -Os -Wl,--gc-sections -mmcu=atmega328p -o /tmp/build3260894923757074322.tmp/solarpowerpack.cpp.elf /tmp/build3260894923757074322.tmp/solarpowerpack.cpp.o /tmp/build3260894923757074322.tmp/LiquidCrystal/LiquidCrystal.cpp.o /tmp/build3260894923757074322.tmp/EEPROM/EEPROM.cpp.o /tmp/build3260894923757074322.tmp/core.a -L/tmp/build3260894923757074322.tmp -lm
/usr/share/arduino/hardware/tools/avr/bin/avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 /tmp/build3260894923757074322.tmp/solarpowerpack.cpp.elf /tmp/build3260894923757074322.tmp/solarpowerpack.cpp.eep
/usr/share/arduino/hardware/tools/avr/bin/avr-objcopy -O ihex -R .eeprom /tmp/build3260894923757074322.tmp/solarpowerpack.cpp.elf /tmp/build3260894923757074322.tmp/solarpowerpack.cpp.hex
Binary sketch size: 6,788 bytes (of a 30,720 byte maximum)


As a side note when I use F() it does not mean it's writing to flash memory each time the string is displayed right?  I know flash has limited writes, so just something I want to make sure of.
« Last Edit: July 12, 2016, 05:55:58 am by Red Squirrel »
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Serial interface acting really weird
« Reply #18 on: July 12, 2016, 06:36:16 am »
What version Arduino IDE are you using?  Make sure it's up to date.

As for F(), no, it does not write to flash, the string is already in flash.  F signals to, for example, Serial.print() that the string has been left in flash, it's not in ram, and so it will have to use the special memory access instructions to read it from the flash before it can use it.
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #19 on: July 12, 2016, 07:12:18 am »
It's version 1:1.0.5.  Probably super old, repos tend to be ridiculously out of date.  Can't be bothered to compile from source unless I really have to.  You loose integration in the GUI (having it in start menu or "open with" option etc) when you do it that way.

But anyway it seems I do have things working now so that's great.

Going to try the other chip again tomorrow, now it's time for bed. :P
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Serial interface acting really weird
« Reply #20 on: July 12, 2016, 09:05:53 am »
It's version 1:1.0.5.  Probably super old, repos tend to be ridiculously out of date.  Can't be bothered to compile from source

Current version is 1.6.9.

Binaries are available on the Arduino site, just download, unpack, cd into the directory, and ./arduino

https://www.arduino.cc/en/Main/Software

~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #21 on: July 12, 2016, 10:08:04 pm »
After using the F() function for all static text, I put the Atmega168 back and reprogrammed and now it works with that chip too.  So I guess that's what it was, I was going over the memory limit.  In my mind I was thinking 1k is a lot and there's no way I'm going to use all that, but also did not realize static text was actually using memory.  I'll have to remember that one from now on.  All static text should have be called with F().  I imagine I could probably use #DEFINE variables too if I wanted right? But F() is easier anyway.
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Serial interface acting really weird
« Reply #22 on: July 12, 2016, 10:48:27 pm »
  I imagine I could probably use #DEFINE variables

No.
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #23 on: July 13, 2016, 12:49:24 am »
  I imagine I could probably use #DEFINE variables

No.

Is there a reason why?  I thought those were compiled in code, or would that use memory too?
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Serial interface acting really weird
« Reply #24 on: July 13, 2016, 01:09:03 am »
Is there a reason why?  I thought those were compiled in code, or would that use memory too?

#define is a preprocessor directive.  The preprocessor runs before the compiler, it effectively takes the keywords you define, and does a "search and replace" to put whatever you defined wherever you used it.

Code: [Select]
  #define FOO "Bar"

  Serial.print(FOO);

is exactly the same (as in, literally, exactly the same code gets compiled) as

Code: [Select]
  Serial.print("Bar");

   
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2750
  • Country: ca
Re: Serial interface acting really weird
« Reply #25 on: July 13, 2016, 02:10:58 am »
Ohhh right, that would make sense. 
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf