I am a 14 year old trying to do robotics for a school project, and I understand how to wire my device but I am having trouble with coding it properly. I am trying to make a device that detects the NPK levels of soil and I plan to show the values on an LCD screen.
give more details on the devices being used? soil sensor, LCD, etc
upload any code indicating what problems you are having
We are using an NPK sensor (12-24v), LCD screen (16x2)
and this is the code that we're trying to use:
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal.h>
#define SCREEN_WIDTH 52 // LCD display width, in pixels
#define SCREEN_HEIGHT 12 // LCD display height, in pixels
#define LCD_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define RE 8
#define DE 7
//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
const byte nitro[] = {0x01,0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phos[] = {0x01,0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte pota[] = {0x01,0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
byte values[11];
SoftwareSerial mod(2,3);
void setup() {
Serial.begin(9600);
mod.begin(9600);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
delay(500);
display.clearDisplay();
display.setCursor(25, 15);
display.setTextSize(1);
display.setTextColor("BLACK");
display.println(" NPK Sensor");
display.setCursor(25, 35);
display.setTextSize(1);
display.print("Initializing");
display.display();
delay(3000);
}
-=trogen(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(mod.write(nitro,sizeof(nitro))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<7;i++){
//Serial.print(mod.read(),HEX);
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}
byte phosphorous(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(mod.write(phos,sizeof(phos))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<7;i++){
//Serial.print(mod.read(),HEX);
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}
byte potassium(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(mod.write(pota,sizeof(pota))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<7;i++){
//Serial.print(mod.read(),HEX);
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}
Search these forms for NPK sensor and you will find a few long discussions. In one discussion I did post why the code you are using is flawed. (I'm on mg phone now so can't easily locate it - sorry)
I guess this may be a cut/paste error?
what happens when you run the program?
- any information on LCD?
- any serial monitor output? if so upload it as text
- any sensor data at all?
looks like the NPK sensor is using RS485? did you test it in a seperate program before adding LCD code?
upload a schematic of the wiring? how do you power the system?
Actually, I haven't tested the code out yet and I'm also not done with the wiring since it needs soldering. I just plan to make the code in advance.
I suggest you connect the NPK sensor and implement a program to test it - forget the LCD for now
when NKP sensor is working connect the LCD and implement a program to test it
when both are working implement the combined program
what program would you suggest for testing the NPK? and also how do we know that the NPK sensor is working without the LCD? does the values show up on the Arduino IDE?
as a start you appear to have code in post #3 for reading the NPK sensor data
if you run your code of post #3 and garbage appears on the LCD how do you know if the NPK sensor readings are faulty or the LCD is displaying incorrectly
forget the LCD for now and display NPK sensor results on the Serial Monitor using Serial.println() statements