Sudden inability to setup channels

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
Steve_Elves
Posts: 22
Joined: 07 Aug 2022 21:54

Sudden inability to setup channels

Post by Steve_Elves »

I've been messing around trying to get an LCD to work with data from an ultrasonic level detector A02YYUW. I was able to set up the channel to pass data to my Z-Wave controller, but in trying to modify the sketch to display the value I want on an LCD screen, I've managed to break something.

The channel setup code in my sketch looks like this:

ZUNO_SETUP_CHANNELS(
ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TANK_CAPACITY,
SENSOR_MULTILEVEL_SCALE_LITER,
SENSOR_MULTILEVEL_SIZE_TWO_BYTES,
SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL,
getterCapacity));

The end of the error message is this:
"Error 0 Preprocessor failed!
exit status 1001
C:\Program Files (x86)\Arduino\arduino-builder returned 1,001
Error compiling for board Z-Wave>ME Z-Uno2."

The more particular message above this reads:
"preprocessing "ZUNO_LCD_A02YYUW_test_v2.ino"...........................error:C:\Users\steve\AppData\Local\Temp\arduino_build_406454\\ZUNO_LCD_A02YYUW_test_v2_ino.cpp:108 ZUNOPREPROC Problems in ZUNO_SETUP_CHANNELS macro. Can't compile it! Please check the spelling inside the brackets!"

I've tried everything I can think of - does anyone in the hive mind have any ideas what might be wrong here?
User avatar
PoltoS
Posts: 7579
Joined: 26 Jan 2011 19:36

Re: Sudden inability to setup channels

Post by PoltoS »

Please show he full code, may be the error is somewhere around.
Steve_Elves
Posts: 22
Joined: 07 Aug 2022 21:54

Re: Sudden inability to setup channels

Post by Steve_Elves »

Here it is. Note there is other stuff from an example in there. As an aside, the LCD is not displaying, either.


// demo sketch for connecting I2C LCD display and Distance sensor A02YYUW to Z-Uno

// add library Wire.h
#include "Wire.h"
#include "Print.h"

// i2c address
#define LC_I2CADDR 0x27

// commands
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80

// flags for display entry mode
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00

// flags for display on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00

// flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00

// flags for function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00

// flags for backlight control
#define LCD_BACKLIGHT 0x08
#define LCD_NOBACKLIGHT 0x00

#define En 0x4 // Enable bit
#define Rw 0x2 // Read/Write bit
#define Rs 0x1 // Register select bit

int cols;
int rows;
int numlines;
int backlightval;
int displayfunction;
int displaycontrol;
int displaymode;

// Serial-1 connection info

static uint8_t buf[4]; // custome receiving buffer 4 bytes
unsigned char CS; // Variable to hold checksum

uint16_t Distance = 0;
float Level = 0;
float Levelmm = 0;
float Volume = 0;
int Temp = 0;
word Capacity = 0;

// set up channel
ZUNO_SETUP_CHANNELS(
ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TANK_CAPACITY,
SENSOR_MULTILEVEL_SCALE_LITER,
SENSOR_MULTILEVEL_SIZE_TWO_BYTES,
SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL,
getterCapacity)
);

void setup() {
Serial.begin();
Serial.println("start");
lcdbegin(16,2);
Serial1.begin(9600);
}

void loop() {

// Get readings from serial buffer
if (Serial1.available() == 4) {

delay(50);

// Check for packet header character 0xff
if (Serial1.read() == 0xff) {
buf[0] = 0xff;
// Read remaining 3 characters of data
for (int i = 1; i < 4; i++) {
buf = Serial1.read();
}

// Compute checksum
CS = buf[0] + buf[1] + buf[2];
// If checksum is valid compose distance from data
if (buf[3] == CS) {
Distance = (buf[1] * 256) + buf[2];
Levelmm = (1700 - Distance);
Capacity = (3844/1700)*Levelmm; // two 500-liter tanks
}
}
else {
Distance = 9999;
}

}

delay(3000);
// word getCapacity() {
// Print readings to serial monitor
Serial.print("Distance = ");
Serial.print(Distance);
Serial.print(" mm ");
Serial.print(" Level = ");
Serial.print(Levelmm);
Serial.print(" mm ");
Serial.print(" Volume = ");
Serial.print(Capacity);
Serial.println(" Liters");
// return Capacity;
}
// }


void printCapacity(word Capacity) {
clear();
setCursor(0,0);
write('L');
write('e');
write('v');
write('e');
write('l');
write(32);
write('=');
write(32);
write(Capacity/1000);
write(46);
write(Capacity%1000);
write(32);
write('L');
return;
}

void lcdbegin(int c,int r) {
//
cols=c;
rows=r;
backlightval = LCD_BACKLIGHT;
//
Wire.begin();
displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
//
if (rows > 1) {
displayfunction |= LCD_2LINE;
}
numlines = rows;

// SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
// according to datasheet, we need at least 40ms after power rises above 2.7V
// before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
delay(50);

// Now we pull both RS and R/W low to begin commands
expanderWrite(backlightval); // reset expanderand turn backlight off (Bit 8 =1)
delay(1000);

//put the LCD into 4 bit mode
// this is according to the hitachi HD44780 datasheet
// figure 24, pg 46

// we start in 8bit mode, try to set 4 bit mode
write4bits(0x03 << 4);
delayMicroseconds(500); // wait min 4.1ms
delay(4);

// second try
write4bits(0x03 << 4);
delayMicroseconds(500); // wait min 4.1ms
delay(4);

// third go!
write4bits(0x03 << 4);
delayMicroseconds(150);

// finally, set to 4-bit interface
write4bits(0x02 << 4);


// set # lines, font size, etc.
command(LCD_FUNCTIONSET | displayfunction);

// turn the display on with no cursor or blinking default
displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
display();

// clear it off
clear();

// Initialize to default text direction (for roman languages)
displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;

// set the entry mode
command(LCD_ENTRYMODESET | displaymode);

home();

}

/********** high level commands, for the user! */
void clear(){
command(LCD_CLEARDISPLAY);// clear display, set cursor position to zero
delay(2); // this command takes a long time!
}

void home(){
command(LCD_RETURNHOME); // set cursor position to zero
delay(2); // this command takes a long time!
}

void setCursor(uint8_t col, uint8_t row){
int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
if ( row > numlines ) {
row = numlines-1; // we count rows starting w/0
}
command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
}

// Turn the display on/off (quickly)
void noDisplay() {
displaycontrol &= ~LCD_DISPLAYON;
command(LCD_DISPLAYCONTROL | displaycontrol);
}
void display() {
displaycontrol |= LCD_DISPLAYON;
command(LCD_DISPLAYCONTROL | displaycontrol);
}




inline void command(uint8_t value) {
send(value, 0);
}

inline size_t write(uint8_t value) {
send(value, Rs);
return 0;
}
/************ low level data pushing commands **********/

// write either command or data
void send(uint8_t value, uint8_t mode) {
uint8_t highnib=value&0xf0;
uint8_t lownib=(value<<4)&0xf0;
write4bits((highnib)|mode);
write4bits((lownib)|mode);
}

void write4bits(uint8_t value) {
expanderWrite(value);
pulseEnable(value);
}

void expanderWrite(uint8_t _data){
Wire.beginTransmission(LC_I2CADDR);
Wire.write((int)(_data) | backlightval);
Wire.endTransmission();
}

void pulseEnable(uint8_t _data){
expanderWrite(_data | En); // En high
delayMicroseconds(1); // enable pulse must be >450ns

expanderWrite(_data & ~En); // En low
delayMicroseconds(50); // commands need > 37us to settle
}
amatilda
Posts: 61
Joined: 26 Sep 2021 22:09

Re: Sudden inability to setup channels

Post by amatilda »

Hello. You didn't declare a function:

Code: Select all

byte getterCapacity() {
    return 0;
}
As well as:

Code: Select all

buf = Serial1.read();
to replace

Code: Select all

buf[i] = Serial1.read();
Steve_Elves
Posts: 22
Joined: 07 Aug 2022 21:54

Re: Sudden inability to setup channels

Post by Steve_Elves »

Thanks for the reply! The code preceding "buf = ..." is intended to read 4 bytes from the serial buffer in order. This part of the code actually works. As noted in my first post, the issue appeared to me to be a problem with channel definition, although I realize that sometimes the compiler kicks out confusing messages. I think you're probably right about the function declaration - I'll give that a try and let you know. Thanks again for your help.
Steve_Elves
Posts: 22
Joined: 07 Aug 2022 21:54

Re: Sudden inability to setup channels

Post by Steve_Elves »

Declaring a function as you suggested worked. Thanks a lot!
Post Reply