Constructive criticism and help working out a hangup in code needed.

Im fairly new to the arduino community, one of those guys that wouldn't ask for help unless ive come to the end of the road.

This code is for a hydroponics pH automated doser. Through the use of a pH probe, a pH shield, an Arduino UNO r3, and a peristalic pump; this code would sense the pH too high and send signal to the relay to power on the pump until the pH was back down to an acceptable range. Day 4 of this issue though has left me with the option of just timing a relay to come on every other day and run the pump for a few seconds hoping it is in the ballpark of the range that is accepted.

What is happening in the included code is that randomly, it hangs, leaving the pump on and dispensing all the ph buffer causing the whole system to be acidic.

As previously mentioned, I am fairly new to Arduino. There are most likely lots of issues with the code included, all thoughts and comments are very much welcomed.

http://pastebin.com/raw.php?i=30ej0YBd

I would say that you are almost certainly running out of memory. I suggest you do some investigation into PROGMEM. There's a whole thread on here about it. I'm sure someone will be able to give you a link to it.

FOUND IT!
You should really have a read through this. It will likely save your sanity :slight_smile:
http://forum.arduino.cc/index.php?topic=269646.msg1900850#msg1900850

you can put your constant variables into PROGMEM:

for example:

unsigned char PROGMEM myFont  [][8]=... ...

and use the F() macro to put your string constants into program memory:

for example:

  Serial.print(F("avgMeasuredPH-"));
  Serial.print(F("avgMeasuredPH-"));

and

sendStr((unsigned char*)F("pH Down Going"));

Alright, I've added progmem to all of my strings that I could and added

unsigned char PROGMEM myFont  [][8]={

just before the unsigned char

is there something I need to define for progmem or am I good just added the F macro to the strings a unsigned char fonts?

Also, there is a minor annoyance on the startup of this sketch. There is a signal sent to the relay for a couple seconds before the loop is started. Normally I wouldn't care, however, the solution gets a much larger dose of acidic solution. I have gone back through and edited what I could out and try a few things. Is this something that I will just need to get over?

I've included an updated code in the below link. Thanks so much for your prompt help.
http://pastebin.com/raw.php?i=VGAAAgsx

tullyamo:
I've included an updated code in the below link. Thanks so much for your prompt help.
http://pastebin.com/raw.php?i=VGAAAgsx

Please attach your sketches as attachments, rather than sending people off to Pastebin.

How to use this forum

I have a page about PROGMEM: Gammon Forum : Electronics : Microprocessors : Putting constant data into program memory (PROGMEM)

Agh, I can't believe you chose pin 13 for your relay. It's connected to the onboard LED. This is flashed when there's any activity on the serial port and I believe this is why it gets flashed as the serial port is initialised on start up. Naturally this flashing will be setting your relay off. Could you pick another? such as 12,11,10,9,8,7,6,5,4,or 3, (don't use 2 or 1 either, they also get affected by coms)

(don't use 2 or 1 either, they also get affected by coms)

That's pins zero and one.
Pin two is fine.

Agh, I can't believe you chose pin 13 for your relay. It's connected to the onboard LED. This is flashed when there's any activity on the serial port

Is this really the case, because it does not happen to me using a Uno.

No, but it would be flashed when you power on the Uno.

UKHeliBob:
Is this really the case, because it does not happen to me using a Uno.

I have a mega2560 and every time I reset it I get two clear pulses of the onboard LED. Each about half a second in duration. This happens before the setup() function has even started. Furthermore, if I put an LED in pin13 this also strobes with it.

I've just tried it again with the bare sketch

void setup()
{
}
void loop()
{
}

exactly the same result.

I have a mega2560 and every time I reset it I get two clear pulses of the onboard LED. Each about half a second in duration

What I was querying was the statement that the LED on pin 13 is flashed when there's any activity on the serial port. There may be reasons not to choose pin 13 but that is not one of them.

Alright,

This morning I've changed the output pins from 12 and 13 to 6 and 7, with 6 being the one that is used currently. I've made the changes I thought were correct with the progmem and unsigned char. After uploading the modified sketch, a signal is sent to pin 6 and the OLED is blank. I don't think I did the progmem correctly, were as before I had something on the OLED and the relay was running only before the loop started.

Doser_with_Progmem.ino (12.3 KB)

I think the first order of business is to send the output to the Serial object and use the Serial.print() method to see if your functions are producing the results you want. Then I would check to make sure the OLED is properly working. Try placing the following fragment into setup():

  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  delay(DISPLAYDELAY);

and supply a draw() function that looks something like:

void draw() {
    u8g.setFont(u8g_font_courR14);
    u8g.drawStr(30, 15, "Test");
    u8g.drawStr(10, 45, "Using the OLED");
 }

and see what happens.

Leaving the delay(DISPLAYDELAY); returned errors

void setup()
{  
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  //delay(DISPLAYDELAY);

adding this near the end of the sketch I was able to see the test when it started up briefly.

void draw() {
    u8g.setFont(u8g_font_courR14);
    u8g.drawStr(30, 15, "Test");
    u8g.drawStr(10, 45, "Using the OLED");
 }

I had to take out all the progmem code as I was in a very different mindset this morning.. One should probably not spend all day in heart surgery and come home to figure out code. My apologies to Nick Gammon for the omitting progmem out of the sketch. I am not sure where I was going with things this morning, but I am certainly unable to return. I've been given a couple more days to figure this out or dump the project and go commercially. Much thanks for those who are helping and kudos to your patience.

I have submitted the sketch without progmem, with the changes made to the pin assignments. I am still having the problems with the code freezing randomly which we have learned is associated with the lack of memory. Also, I've inlcuded yet another failure on my end. In the code below, the first "if" statement works. The second "if" will print screen but doesn't send the signal to pin 7.

       if(avgMeasuredPH > 6.3)  //When pH is above 6.3, pH Down is given
    {
      digitalWrite(relayPin,LOW);
      setXY(4,0);
      sendStr((unsigned char*)"pH Down Going");
    }
    else
    {
      digitalWrite(relayPin,HIGH);
      setXY(4,0);
      sendStr((unsigned char*)"PH Down Hold");
         } 
       
  if(avgMeasuredPH < 5.8)  //When pH is below 5.8, pH Up is given
    {
      digitalWrite(relayPin1,LOW);
      setXY(5,0);
      sendStr((unsigned char*)"PH Up Going");
    }
    else
    {
      digitalWrite(relayPin1,HIGH);
      setXY(5,0);
      sendStr((unsigned char*)"PH Up Hold");
         }

doser_submitted.ino (12.4 KB)

I'm getting a whole lot of errors trying to compile your code:

/tmp/ccrxbuSN.s: Assembler messages:
/tmp/ccrxbuSN.s:1318777: Warning: .stabs: description field '14625' too big, try a different debug format
/tmp/ccrxbuSN.s:1318778: Warning: .stabs: description field '1469a' too big, try a different debug format
/tmp/ccrxbuSN.s:1318786: Warning: .stabs: description field '14f75' too big, try a different debug format
/tmp/ccrxbuSN.s:1318955: Warning: .stabs: description field '1018b' too big, try a different debug format
/tmp/ccrxbuSN.s:1318956: Warning: .stabs: description field '107d0' too big, try a different debug format
/tmp/ccrxbuSN.s:1318959: Warning: .stabs: description field '10003' too big, try a different debug format
/tmp/ccrxbuSN.s:1318960: Warning: .stabs: description field '105e9' too big, try a different debug format
/tmp/ccrxbuSN.s:1318961: Warning: .stabs: description field '10dc5' too big, try a different debug format
/tmp/ccrxbuSN.s:1318965: Warning: .stabs: description field '1058f' too big, try a different debug format
/tmp/ccrxbuSN.s:1318966: Warning: .stabs: description field '10d54' too big, try a different debug format
  • What board (Arduino) are you compiling for?
  • Where did you get the library: U8glib?

The board is a Arduino UNO r3

The library for the U8glib is Service End for Bintray, JCenter, GoCenter, and ChartCenter | JFrog or

My apologies for not including the library before.

u8glib_arduino_v1.16.zip (1.06 MB)

I curious too as to whether the relay is causing some of the issues with random hanging. There is a 2 channel 5v relay connecting the 12vdc pump for signal and a few times during startup, my OLED has froze or the pump just continued pumping as if the code froze.

Do you get this compile message?

Sketch uses 17,970 bytes (55%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,573 bytes (76%) of dynamic memory, leaving 475 bytes for local variables. Maximum is 2,048 bytes.
Low memory available, stability problems may occur.

How about making the font in PROGMEM?

const unsigned char myFont[][8] PROGMEM ={

Then change the way you use it:

void print_a_char(unsigned char ascii=0)
{
  unsigned char i=0;
  for(i=0;i<8;i++)
  {
    SendChar(pgm_read_byte (&myFont[ascii-0x20][i]));
  }
}


...

void sendStr(unsigned char *string)
{
  unsigned char i=0;
  //setXY(0,0);    
  while(*string)
  {
    for(i=0;i<8;i++)
    {
      SendChar(pgm_read_byte (&myFont[*string-0x20][i]));

      // SendChar(*string);
      delay(10);
    }
    *string++;
  }
}