What am I doing wrong here?

regpye

New Member
Need some help again please.
I checked the manual and maybe I am misunderstanding something, but a syntax error keeps popping up.

Code:
Start:
;Analogue sensor find thresholds by trial and error, depends on soil type, etc.
symbol sensor1 = C.1
readadc sensor1, b10
         
if b10 <100    then goto wet
if b10 =>100 and =<170 then goto JustRight ; ERROR ON THIS LINE
if b10 >170 then goto dry
   
;******************************************
goto start
Wet:
SerTxd("Too Wet, pump is off, ADC value ",#b10, CR, LF)
low pump1
goto start
JustRight:
SerTxd("Just Right, ADC value ",#b10, CR, LF)
low pump1
goto start
Dry:
SerTxd("Too Dry, pump is on, ADC value ",#b10, CR, LF)
high pump1
goto start
 

regpye

New Member
Need some help again please.
I checked the manual and maybe I am misunderstanding something, but a syntax error keeps popping up.

Code:
Start:
;Analogue sensor find thresholds by trial and error, depends on soil type, etc.
symbol sensor1 = C.1
readadc sensor1, b10
        
if b10 <100    then goto wet
if b10 =>100 and =<170 then goto JustRight ; ERROR ON THIS LINE
if b10 >170 then goto dry
  
;******************************************
goto start
Wet:
SerTxd("Too Wet, pump is off, ADC value ",#b10, CR, LF)
low pump1
goto start
JustRight:
SerTxd("Just Right, ADC value ",#b10, CR, LF)
low pump1
goto start
Dry:
SerTxd("Too Dry, pump is on, ADC value ",#b10, CR, LF)
high pump1
goto start
Sorted it out, silly me.
 

1968neil

Senior Member
Code:
Symbol sensor1       = C.1          ; Sensor 1 Input Pin
Symbol Pump1         = C.2          ; Output for pump
Symbol Sensor1_Value = b10          ;  Sensor variable



Start:
;Analogue sensor find thresholds by trial and error, depends on soil type, etc.

readadc sensor1, Sensor1_Value     ;  Read sensor 1 value into b10 (Sensor1_Value
        
if Sensor1_Value <100   then goto wet
if Sensor1_Value =>100 and Sensor1_Value =<170 then goto JustRight ; ERROR ON THIS LINE
if Sensor1_Value >170 then goto dry
  
;******************************************
goto start
Wet:
SerTxd("Too Wet, pump is off, ADC value ",#b10, CR, LF)
low pump1
goto start
JustRight:
SerTxd("Just Right, ADC value ",#b10, CR, LF)
low pump1
goto start
Dry:
SerTxd("Too Dry, pump is on, ADC value ",#b10, CR, LF)
high pump1
goto start
 

regpye

New Member
if Sensor1_Value =>100 and Sensor1_Value =<170 then goto JustRight
Yes I found it Neil, I left off the second Sensor1. Couldn't see it for looking, then all of a sudden I realised my omission.
Working out the actual values maybe a challenge, I guess it will be trial and error to get it right, or maybe there is a way of testing?
 

AllyCat

Senior Member
Hi,
I guess it will be trial and error to get it right, or maybe there is a way of testing?
Certainly a trial is necessary, but not necessarily any need to error. Perhaps just add an output from the program loop to the PE's Terminal Emulator such as: SERTXD("Present Sensor Value is ",#Sensor1_Value,CR,LF) , make the soil over-damp and take measurements as it dries out (or vice versa).

"Magic numbers" such as the 100 and 170 really should be Symbolised at the top of the program, particularly if they are used more than once in the program (otherwise you might forget to update all of the multiple instances and "break" the program at a later date). But actually in this example, they don't need to be used more than once anyway. A section of your code could be simplified to :
Code:
symbol WET_THRESHOLD = 100
symbol DRY_THRESHOLD = 170
......
if Sensor1_Value < WET_THRESHOLD then wet
if Sensor1_Value > DRY_THRESHOLD then dry       ; Else it must be....
goto JustRight                  ; Or simply "fall into" the JustRight routine
......
Cheers, Alan.
 

regpye

New Member
"Magic numbers"
Thanks Alan, It is not a waste of time to do it as you said, because it is a learning exercise for me, I love learning something new or a better way to do something.
So I will implement what you have suggested and keep it on file for future reference as I do for most things like this.
 

regpye

New Member
Certainly a trial is necessary
Works just fine thanks Allan, here is an updated program for anyone wanting to do the same.
For testing purposes in the "Resting" section, the sleep should be remmed out or reduced so that the program doesn't go to sleep for about 4 hours.
Code:
; *******************************
;    Written by: Reg Pye
;    Function:   single bucket water system moisture sensor (can be used for more buckets all connected together with water outlets and using just one sensor in one bucket.)   
;    Target PICAXE:    08M2
;    C.4 connects to  +power rail of sensor circuit, used to power off in sleep mode.
;    C.2 connects to sensor AO pin of the sensor using analogue method.
;    C.1 is mosfet output to pump
;     
;    program put to sleep for several hours with power to sensor switched off to save on corrosion of the sensor fingers.
; *******************************

#Picaxe 08M2
#No_Data

start:
symbol sensor1 = C.2 ;C.2  ADC analogue connection and Digital connection, selected on sensor.
symbol pump1 = C.1
symbol sensorpower = C.4
Symbol Sensor1_Value = b10          ;  Sensor variable
symbol WET_THRESHOLD = 100    ; Adjust for when just wet enough
symbol DRY_THRESHOLD = 170    ; Adjust for just too dry


high sensorpower    ;switch on power to sensor
    ;**********************************************
;Analogue sensor find thresholds by trial and error, depends on soil type, etc.
readadc sensor1, b10
if Sensor1_Value < WET_THRESHOLD then wet
if Sensor1_Value > DRY_THRESHOLD then dry       ; Else it must be....
goto JustRight                  ; Or simply "fall into" the JustRight routine          

;******************************************

Wet:
SerTxd("Too Wet, pump is off, ADC value ",#b10, CR, LF)
low pump1 goto resting
goto start
JustRight:
SerTxd("Just Right, ADC value ",#b10, CR, LF)
low pump1 
goto start
;******************************************

Dry:
SerTxd("Too Dry, pump is on, ADC value ",#b10, CR, LF)
high pump1
wait 15        ; give some time to wet the soil, then test again
goto start
;******************************************

Resting:    ;sensor power has turned off and program will now sleep for about 4 hours
SerTxd("Will now go into rest mode, ADC value  ",#b10, CR, LF)
low sensorpower    ;switch off power to sensor
sleep 6200    ;program to sleep for about 4 hours
goto start
 

The bear

Senior Member
Reg,
Nice work.
If you add this line at the beginning of your program:
Sertxd( "Watering System ", 17.10.23, Cr, Lf ) ;Name that Picaxe, It will show up in the Terminal.
It will help you identify the picaxe at a later date, you can add the version number too, e.g. Mark 56
Good luck.............
 

regpye

New Member
Sertxd( "Watering System ", 17.10.23, Cr, Lf ) ;Name that Picaxe, It will show up in the Terminal.
Sertxd( "Watering System ", 17.10.23, Cr, Lf ) ;Name that Picaxe, It will show up in the Terminal.
^
Syntax error on line 14 at/before position 30

Error: Illegal character: ('.' (0x2E))
 

hippy

Technical Support
Staff member
Sertxd( "Watering System , 17.10.23", Cr, Lf )
If wanting to include a timestamp date in the code downloaded to a PICAXE, there are a few pre-processor expansions which can help with that. Those will save you from having to frequently update the source code, the risk of forgetting to -
Code:
SerTxd("Today's ISO date is ", PPP_DATE,    CR, LF)
SerTxd("Today's UK  date is ", PPP_DATE_UK, CR, LF)
SerTxd("Today's US  date is ", PPP_DATE_US, CR, LF)
SerTxd("The current time is ", PPP_TIME,    CR, LF)
Code:
Today's ISO date is 2023-10-18
Today's UK  date is 18-10-2023
Today's US  date is 10-18-2023
The current time is 10:54:52
If one prefers to have '/' in the dates rather than '-' that can be done using a macro but isn't always worth the cost of the extra memory used -
Code:
#Macro SerTxdDate(date)
  For b0 = 0 To 9
    LookUp b0, (date), b1
    If b1 = "-" Then
      b1 = "/"
    End If
    SerTxd(b1)
  Next
#EndMacro

SerTxd("Today's UK date is ")
SerTxdDate(PPP_DATE_UK)
SerTxd(CR, LF)
It's also possible to get a two-digit year by not outputting anything when b0 is the index into the century digits, 6 or 7 for UK and US dates.

I personally prefer ISO dates rather than UK or US as that avoids the potential confusion as to whether "01/02/23" means 1st of February or January 2nd.
 
Top