Arduino to Picaxe comms

Steve2381

Senior Member
I am sending a single serial data packet of one number from an Arduino 2560p using Serial.print(var). Set to 1200 baud.

Received on a 14m2 on pin B.1 using serin b.1,N1200_4,ArduinoIn.
I just get random number. What am I missing? If I add a qualifier, it doesn't seem to help (or not work at all).

The wiring is simple. TX on the Arduino, through a 22k (I have tried other values) and in on Picaxe 14m2 pin B.1. Tied it low with a 10k to see if that helps... nope.
Disconnect the comms wire and the random values stop, so the link is there.

Hmm
 

srnet

Senior Member
When you say random, do you mean you are always sending the same value from the Arduino and the PICAXE sees and then prints out different values each time ?

And what is the value of var ?
 

Steve2381

Senior Member
Yes. I am sending 127 - Serial.print(127); from the Arduino. (Picked that as a demo figure)

I am getting random between 0-255 on the Picaxe.
 

Jeremy Harris

Senior Member
Are the arduino and the Picaxe running on the same power supply? If the arduino is on 3.3V and the Picaxe on 5V then there is a very slim chance that the series resistor (which isn't needed on the serial in port under these conditions, anyway) might cause a tiny voltage drop and get too close to the high threshold detection point, perhaps (TBH I'm not 100% convinced of this, but it's worth a try).

Are both 0V rails connected together OK?
 

srnet

Senior Member
Serial.print(127) from an Arduino will actually send three numbers to the PICAXE; 49,50,57, those being the ASCII values for 1 2 7

What numbers is the PICAXE seeing each time ?
 

Steve2381

Senior Member
Its not a PSU issue, It my breadboard and everything shares a common supply. I checked the 0v connection link and its fine.

I understood that Serial.print(127); sends that as a decimal

https://www.arduino.cc/en/Serial/Print

But... quite possible that I am wrong (in fact, highly likely)

Its not 49, 50 and 57 anyway..... its all over the place. Rarely the same number twice
 

Steve2381

Senior Member
srnet is correct (thank you)... I think I should be sending the data with Serial.write(127); However, if I do this, I don't get anything at the Picaxe end.
Hmm
 

Steve2381

Senior Member
Well I thought this would be a simple affair.... nope.
I think it probably is Serial.write(127); to send the byte 127 (as an example).

But, I cannot get it to read that value on the Picaxe.

ArduinoIn=0
serin [200],b.1,T1200_4,ArduinoIn
pause 200

This settles the result down (not flashing random rubbish). But... its not the correct result (still random, just slower!)

Any other ideas on sending a single byte from the Arduino to the Picaxe? Basically I need to send a value 0-255 between the two.
 

srnet

Senior Member
127 is not a good number for testing, its non-printable as ASCII.

When you do say a Serial.write(49), does "1" appear in the Arduino Serial Monitor when its set to 1200 baud?
 

Steve2381

Senior Member
Yes - 1 shows in the Serial monitor when you send Serial.write(49);

That is the ASCII code for 1, so is it sending 49? Why do these simple tasks take so long!

I have tried various values, not just 127
 

Steve2381

Senior Member
Got it...

The Arduino needs:

Serial.print("ready");
Serial.write(val);

The Picaxe needs:

serin [200],b.1,T1200_4,("ready"),ArduinoIn

Thank you for everyones advice.
 

hippy

Technical Support
Staff member
serin [200],b.1,T1200_4,ArduinoIn
Take the timeout out, that will simply confuse things. Also remove any pull-ups or pull-downs you have added to the serial line.

Perhaps post your sending code because that might have some error in it, such as not actually sending at 1200 baud.

Start with something simpler, I can only guess what the code should be on the sending end but probably something like this ...

Code:
while (1)
{
  Serial.write(123);
  Delay.milliseconds(1000); 
};
Then at the PICAXE end ...

Code:
#Picaxe 14M2
#Terminal 4800
Do
  SerIn B.1, T1200, b0
  SerTxd( #b0, " " )
Loop
What results does that give ?
 

Steve2381

Senior Member
The timeout actually doesn't really matter (I don't think). The Picaxe basically loops around and simply sets RGB leds to a colour depending on the incoming variable from the Arduino.

I have the statement ArduinoIn=0 before the Serin command to ensure the input is zero, if nothing is received (which is why I added the timeout).
The Arduino doesn't always send data, so the default needs to be zero.
I thought the pulldown resistor would accomplish that, but it doesn't.

I will remove the pull down resistor then.

The biggest issue I am left with is the Serial.Write command from the Arduino slows the Arduino code horrifically. I didn't realise it would impact the speed of the routine so badly.
So I may have to re-think the method of communication anyway.
 

hippy

Technical Support
Staff member
The biggest issue I am left with is the Serial.Write command from the Arduino slows the Arduino code horrifically. I didn't realise it would impact the speed of the routine so badly.
1200 baud is slow and you are sending multiple characters. A single byte takes over 8ms, and what you appear to be currently sending will take 50ms or more.

You can increase the baud rate and reduce the number of bytes sent. You might have to send bytes individually and add delays to pace the data to allow the PICAXE to be able to keep up.

Depending on what else you are doing in the PICAXE it could be that you are missing some transmissions from the sender. If you miss one it could take up to 100ms to get the next.

It's not possible to assess things in more detail without seeing the code.
 

Steve2381

Senior Member
Well its messy and could probably be done a heck of a lot more efficiently... but it works
A lot of things have changed in the code, so the variables don't really need to be W0,W1 etc... it was originally using values up to 1023. Now its within 255, I could change those I suppose.

I will try bumping up the Baud, now that its working.

Code:
'RGB LED Control

'Picaxe 14M2

#picaxe 14m2
#No_Data

symbol Redcalc       = W0
symbol Greencalc     = W1
symbol Bluecalc      = W2
symbol Green         = W3
symbol Blue          = W4
symbol Red	         = W5
symbol loopcounter   = w6
symbol speed	   = w7
symbol ArduinoIn	   = w8


symbol Redout	= c.2
symbol Blueout	= b.4
symbol Greenout	= b.2

dirsb=%010101                                         '1=out
dirsc=%000100

pwmout Redout,100,0						'Start the outputs
pwmout Blueout,100,0
pwmout Greenout,100,0

speed=1



main:


ArduinoIn=0

serin [200],b.1,T1200_4,("ready"),ArduinoIn				;Receive serial value from Arduino


random b18 							


if pinc.0=0 and pinc.1=0 and pinb.5=0 and pinb.3=0 then 
       gosub Redoff
       gosub Greenoff
       gosub Blueoff
       endif

if pinc.0=1 and pinc.1=0 and pinb.5=0 and pinb.3=0 then 
       gosub Allwhite
       endif
       
if pinc.0=0 and pinc.1=1 and pinb.5=0 and pinb.3=0 then 
       gosub Redwhite
       endif       

if pinc.0=0 and pinc.1=0 and pinb.5=1 and pinb.3=0 then 
       gosub Bluered
       endif
       
if pinc.0=0 and pinc.1=0 and pinb.5=0 and pinb.3=1 then 
       gosub Bluewhite
       endif       
       
if pinc.0=1 and pinc.1=1 and pinb.5=0 and pinb.3=0 then 
       gosub Redgreen
       endif       

if pinc.0=1 and pinc.1=0 and pinb.5=1 and pinb.3=0 then 
       gosub Greenred
       endif
       
       
if pinc.0=1 and pinc.1=0 and pinb.5=0 and pinb.3=1 then 
       speed=400
       gosub Fade
       endif

if pinc.0=0 and pinc.1=1 and pinb.5=1 and pinb.3=0 then 
	 speed=50
       gosub Fade
       endif

if pinc.0=0 and pinc.1=1 and pinb.5=0 and pinb.3=1 then 
       Speed=2000
       Gosub multi
       endif
       
if pinc.0=0 and pinc.1=0 and pinb.5=1 and pinb.3=1 then 
       speed=400
       gosub multi      
       endif       
       
if pinc.0=1 and pinc.1=1 and pinb.5=1 and pinb.3=0 then 
	 Speed=5
       gosub Mono
       endif
       
if pinc.0=1 and pinc.1=0 and pinb.5=1 and pinb.3=1 then 
       Speed=20
       gosub Mono
       endif       
       
if pinc.0=1 and pinc.1=1 and pinb.5=0 and pinb.3=1 then 
       gosub randomchoice
       endif        
       
if pinc.0=1 and pinc.1=0 and pinb.5=1 and pinb.3=1 then 
       gosub Strobe
       endif     
       
if pinc.0=1 and pinc.1=1 and pinb.5=1 and pinb.3=1 then 
       gosub Twinkle
       endif           

goto main



'------------------------------------------------------------------------------

Allwhite:

gosub Redon
gosub Greenon
gosub Blueon
return

'-----------------------------------------

Redwhite:

Gosub Redon

gosub arduinoGreen
gosub arduinoblue

Return
	

'-----------------------------------------

Bluered:

Bluecalc=255-ArduinoIn
pwmduty Blueout,Bluecalc

Gosub arduinoRed
Gosub Greenoff

return

'-----------------------------------------

Bluewhite:

Gosub Blueon

Gosub arduinoGreen
Gosub arduinoRed

Return

'-----------------------------------------

Redgreen:

Redcalc=255-ArduinoIn
pwmduty Redout,Redcalc

Gosub ArduinoGreen
Gosub Blueoff

return
'-----------------------------------------

Greenred:

Greencalc=255-ArduinoIn
pwmduty Greenout,Greencalc

Gosub ArduinoRed
Gosub Blueoff

return
'-----------------------------------------

Multi:

Gosub Redon
Gosub Blueoff
Gosub Greenoff

Pause speed

Gosub Greenon

pause speed

Gosub Redoff
Gosub Blueon

pause speed

Gosub Greenoff
Gosub Redon

pause speed

Gosub Greenon

Pause speed

return

;------------------------------------------

Twinkle:

RANDOM w7
w8 = w7 // 40

if W8=4 then 
 pwmduty Redout,20
 pwmduty Greenout,20
 pwmduty Blueout,20
 Gosub Redoff
 Gosub Greenoff
 Gosub Blueoff
 endif
 
 
return

'-----------------------------------------

Strobe:

Gosub Redon
Gosub Greenon
Gosub Blueon

Redcalc=255-ArduinoIn

If ArduinoIn<250 then
   pause Redcalc
   
   Gosub Redoff
   Gosub Greenoff
   Gosub Blueoff

   endif

return

'-----------------------------------------



Randomchoice:


RANDOM w7
w8 = w7 // 255 + 1
pwmduty Redout,w8
pause 400


RANDOM w7
w8 = w7 // 255 + 1
pwmduty Greenout,w8
pause 400

	
RANDOM w7
w8 = w7 // 255 + 1
pwmduty Blueout,w8
pause 400

return



'-----------------------------------------

mono:

for loopcounter = 0 to 1023 step speed
pwmduty Redout,loopcounter
next loopcounter

for loopcounter = 0 to 1023 step speed
Redcalc=1023-loopcounter
pwmduty Redout,Redcalc
next loopcounter

for loopcounter = 0 to 1023 step speed
pwmduty Greenout,loopcounter
next loopcounter

for loopcounter = 0 to 1023 step speed
Greencalc=1023-loopcounter
pwmduty Greenout,Greencalc
next loopcounter

for loopcounter = 0 to 1023 step speed
pwmduty Blueout,loopcounter
next loopcounter

for loopcounter = 0 to 1023 step speed
Bluecalc=1023-loopcounter
pwmduty Blueout,Bluecalc
next loopcounter

return


'-----------------------------------------


fade:	

pwmduty Redout,0
Gosub Blueon
pwmduty Greenout,0

	 for loopcounter = 0 to 1023 step 5
		  pwmduty Redout,loopcounter
		  Redcalc = 255-loopcounter
		  
		  pause 10
	 next loopcounter
	 pause speed
	 for loopcounter = 0 to 1023 step 5
		  
		  Redcalc = 1023-loopcounter
		  pwmduty Blueout,Redcalc
		  pause 10
	 next loopcounter
	 pause speed
	 for loopcounter = 0 to 1023 step 5
		  pwmduty Greenout,loopcounter
		  
		  Redcalc = 1023-loopcounter
		  pwmduty Redout,Redcalc
		  pause 10
	 next loopcounter
	 Pause speed
	 pwmduty Redout,0
	 pause speed
	 for loopcounter = 0 to 1023 step 5
		  pwmduty Blueout,loopcounter
		   Redcalc = 1023-loopcounter
		  pwmduty Greenout,Redcalc
		  pause 10
	 next loopcounter
	 pause speed
	 
	 for loopcounter = 0 to 1023 step 5
	 Redcalc = 1023-loopcounter
		  pwmduty Blueout,Redcalc
		  pwmduty Redout,loopcounter
		  pwmduty greenout,loopcounter
		   
		  pause 10
	 next loopcounter

	 	 pause speed
	 for loopcounter = 0 to 1023 step 5
	 Redcalc = 1023-loopcounter
		  pwmduty Blueout,loopcounter
		  pwmduty Redout,Redcalc
		  pwmduty greenout,Redcalc
		   
		  pause 10
	 next loopcounter
	
	 pause speed

	return
	
;------------------------------------------------------

Redon:
pwmduty Redout,255
return


Greenon:
pwmduty Greenout,255
return


Blueon:
pwmduty Blueout,255
return

Redoff:
pwmduty Redout,0
return

Greenoff:
pwmduty Greenout,0
return

Blueoff:
pwmduty Blueout,0
return


ArduinoRed:
pwmduty Redout,ArduinoIn	
return
	
ArduinoGreen:
pwmduty Greenout,ArduinoIn	
return

ArduinoBlue:
pwmduty Blueout,ArduinoIn	
return
 

srnet

Senior Member
The biggest issue I am left with is the Serial.Write command from the Arduino slows the Arduino code horrifically. I didn't realise it would impact the speed of the routine so badly
Serial.print and Serial.write are background commands.
 

Steve2381

Senior Member
Solved that issue srnet. Set a flag so that is only sends the data if it changes by more than 5. Its not constantly updating the variable now.

Increased the Baud to 4800..... it didn't like that at all.
 
Top