0

I'm new with Arduino (and I'm not very good at English). Recently with Arduino Nano I have tested:

  • Control water pump based on soil moisture (using relay) (1)
  • Display soil moiture (in percent) on a Nokia 5110 LCD (2)

Now I'm combining 2 codes to control water pump while always displaying moisture value (in percent) on LCD. However, after I have uploaded the code an Arduino Nano, the LCD showed nothing.

When I touched the sensor (changed moisture value) the first time, the LCD showed up, but when I touched the sensor the second time (changed moisture value again), the LCD went blank. The relay and pump still work fine, so I think the problem comes from LCD's code.

Here's my code (1):

const int VAL_PROBE = 0; 
int RELAY1 = A1;
const int MOISTURE_LEVEL = 250; 

void setup() 
{
  Serial.begin(9600);
  pinMode (2, INPUT);
  pinMode(A0, INPUT);
  pinMode(RELAY1, OUTPUT);
}

void loop() 
{
  int moisture = analogRead(VAL_PROBE);
  Serial.print("Moisture = ");
  Serial.println(moisture);

  if(moisture < MOISTURE_LEVEL)
  {
    digitalWrite(RELAY1,0);           // turn on relay
    Serial.println("Light ON");
  }
  else 
  {
    digitalWrite(RELAY1,1);          // turn off relay
    Serial.println("Light OFF");
  }
}

The code (2)(got it from the Internet):

    //////////////////////////////////////////////
   //        Arduino Soil Moisture Sensor      //
  //            with NOKIA 5110 LCD           //
 //           http://www.educ8s.tv           //
//////////////////////////////////////////////


#include <LCD5110_Graph.h> // THE LIBRARY I AM USING IS THIS:  http://www.rinkydinkelectronics.com/library.php?id=47

LCD5110 lcd(8,9,10,12,11);

extern unsigned char BigNumbers[];
extern uint8_t ui[];

int sensorPin = A0;  
int sensorValue = 0;  
int percent = 0;
String percentString ="0";
int stringLength = 0;

void setup() {
  lcd.InitLCD();
  lcd.setFont(BigNumbers);
  delay(1000);
}

void loop() {
  lcd.clrScr();
  lcd.drawBitmap(0, 0, ui, 84, 48);
  sensorValue = analogRead(sensorPin);
  percent = convertToPercent(sensorValue);
  percentString = String(percent);
  stringLength = percentString.length();
  displayPercent(stringLength);

  lcd.update();
  delay(1000);
}

int convertToPercent(int value)
{
  int percentValue = 0;
  percentValue = map(value, 1023, 350, 0, 100);
  if(percentValue>100)
    percentValue = 100;
  return percentValue;
}

void displayPercent(int length)
{
  switch(length)
  {
    case 1:  lcd.print(percentString,38,19); break;
    case 2:  lcd.print(percentString,24,19); break;
    case 3:  lcd.print(percentString,10,19); break;
    default:  lcd.print(percentString,0,19); break;
  }
}

And my combined code:

const int VAL_PROBE = 0;
int RELAY1 = A1;
const int MOISTURE_LEVEL = 250; // Mức độ ẩm cố định là 250
#include <LCD5110_Graph.h>

LCD5110 lcd(8,9,10,12,11);

extern unsigned char BigNumbers[];
extern uint8_t ui[];

int sensorPin = A0;  
int MoistureValue = 0;  
int percent = 0;
String percentString ="0";
int stringLength = 0;


void setup() 
{
  Serial.begin(9600);//Mở cổng Serial ở mức 9600
  pinMode (2, INPUT);
  pinMode(A0, INPUT);
  pinMode (12, OUTPUT);
  pinMode(RELAY1, OUTPUT);
  lcd.InitLCD();
  lcd.setFont(BigNumbers);
  delay(1000);
}

void loop() 
{
  lcd.clrScr();
  lcd.drawBitmap(0, 0, ui, 84, 48); // hiển thị giao diện màn hình
  MoistureValue = analogRead(sensorPin); // đọc giá trị từ cảm biến

  percent = convertToPercent(MoistureValue);
  percentString = String(percent);
  stringLength = percentString.length();
  displayPercent(stringLength);

  lcd.update(); // cập nhật màn hình
  delay(1000); // mỗi 1 giây

  int moisture = analogRead(VAL_PROBE);
  Serial.print("Moisture = ");
  Serial.println(moisture);

  if(moisture < MOISTURE_LEVEL)
  {
    digitalWrite(12,HIGH);
    digitalWrite(RELAY1,0);           // bật relay
    Serial.println("Light ON");
  }
  else 
  {
    digitalWrite(12,LOW);
    digitalWrite(RELAY1,1);          // tắt relay
    Serial.println("Light OFF");
  }
}

int convertToPercent(int value) // đổi giá trị cảm biến sang phần trăm
{
  int percentValue = 0;
  percentValue = map(value, 1023, 350, 0, 100);
  if(percentValue>100)
    percentValue = 100;
  return percentValue;
}

void displayPercent(int length) // hiển thị phần trăm ra màn hình
{
  switch(length)
  {
    case 1:  lcd.print(percentString,38,19); break;
    case 2:  lcd.print(percentString,24,19); break;
    case 3:  lcd.print(percentString,10,19); break;
    default:  lcd.print(percentString,0,19); break;
  }
}

Any advice?

  • include a circuit schematic, preferably fritzing – Dat Ha Oct 23 '16 at 13:56
1

I think the problem is with

digitalWrite(12,HIGH);

and

digitalWrite(12,LOW);

Pin 12 is used by the LCD. Changing it's output will reset the LCD. I'm not sure why you put it there to begin with, as it wasn't part of any of the two sketches.

  • 1
    Thank you Gerben. Pin 12 I used in code (1) as a led to indicate if the pump was activated. I though I have deleted it in the last code but turn out I deleted it in code (1) instead. That's why you though it wasn't part of any sketches. What a simple mistake haha. I don't have time to check it right now but it must be the answer. I'm going to check it and close this question in few hours later. Btw this website don't have section to add comment for answers? I can only add comment for questions – Lê Thành Vinh Oct 23 '16 at 17:29
  • @LêThànhVinh you just commented on an answer. For your own question, you can edit it – dhimaspw Nov 23 '16 at 1:32
0

in cases like this where you are combining multiple pieces of code together, a simpler approach would be to rename that:

//set up 1
void setup1(void) {
...
}

//loop 1
void loop1(void) {
...
}

//set up 2
void setup2(void) {
...
}

//loop 2
void loop2(void) {
...
}

...

//combined setup
void setup(void) {
  setup1(); 
  setup2();
  ...
}

//combined loop
void loop(void) {
  loop1();
  loop2();
  ...
}

that's much easier to track than to manually copy-and-paste them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.