Arduino vs. Picaxe syntax

The idea of this thread is to provide a place for asking question about the SYNTAX of the Arduino and Picaxe languages. I invite everybody to do the same and I'll be happy to help with Arduino syntax. Perhaps we can create a basic "dictionary" between the two and hopefully this will help the users of the respective platforms to SHARE code.

Perhaps we're not so different after all :)

 

Here is something to get us started:

 

----------------------------------------------------
Comments
----------------------------------------------------

ARDUINO:

// This is how you make a comment in ARDUINO

/*
This is how you make a comment of
several lines in ARDUINO
*/


PICAXE:

' This is how you make a comment in PICAXE


----------------------------------------------------
Declaring a constant
----------------------------------------------------

ARDUINO:

// This assigns the value 3 to the constant LEDPIN
// It's a convention in C/C++ though not necesary to use capital letters for constants
#define LEDPIN 3


PICAXE:

' This assigns the value 3 to the constant ledpin
symbol ledpin = 3


----------------------------------------------------
Declaring a variable
----------------------------------------------------

ARDUINO:

// Single Byte variable for values between -128 and 127
char sensorValue;

// Single Byte variable for values between 0 and 255
byte sensorValue;

// Two Byte variable for values between -32768 and 32767
int sensorValue;

// Two Byte variable for values between 0 and 65535
unsigned int sensorValue;


PICAXE:

' Single Byte variable for values between 0 and 255
symbol sensorValue = b0 (or b1, b2...)

' Two Byte variable for values between 0 and 65535
symbol sensorValue = w0 (or w1, w2...)

' The picaxe chips have a limited space reserved for variables.
' In a picaxe 28X1 they are b0-b27 (bytes 0-27).
' Two bytes can be used in conjunction to make a word so for example, w0 (word 0)=b0+b1.

 

----------------------------------------------------
Assigning value to a variable
----------------------------------------------------

ARDUINO:

sensorValue = 100; // Assign the value 100 to the variable
sensorValue++; // Increase the value by 1
sensorValue--; // Decrease the value by 1
sensorValue+= 5; // Increase the value by 5
sensorValue-= 5; // Increase the value by 5


PICAXE:

sensorValue = 100 ' Assign the value 100 to the variable

' help me fill out the rest

 

----------------------------------------------------
Setup / initialize
----------------------------------------------------

ARDUINO:

void setup() {
// This code is executed once when the program starts
}


PICAXE:

' No equivalent exists


----------------------------------------------------
The main loop to be repeated
----------------------------------------------------

ARDUINO:

void loop() {
// Code to repeat goes here
}


PICAXE:

Main:
' Code to repeat goes here
goto Main


----------------------------------------------------
Turn on/off digital output pin
----------------------------------------------------

ARDUINO:

digitalWrite(1, HIGH); // Turns ON output of digital pin 1
digitalWrite(1, LOW); // Turns OFF output of digital pin 1

// Note: You must first set the pin to mode OUTPUT
// This is done in the setup() function like this:
pinMode(1, OUTPUT);

 

PICAXE:

high 1 ' Turns ON output of digital pin 1
low 1 ' Turns OFF output of digital pin 1


----------------------------------------------------
Reading a value from an analog in
----------------------------------------------------

ARDUINO:

// This reads a value from analog pin 1 and stores it in the variable sensorValue
// The value received is a 10 bit value thus a number between 0 and 1023
sensorValue = analogRead(1);


PICAXE:

// This reads a value from analog pin 1 and stores it in the variable sensorValue
// The value received is an 8 bit value thus a number between 0 and 255
readadc 1, sensorValue

// This reads a value from analog pin 1 and stores it in the variable sensorValue
// The value received is a 10 bit value thus a number between 0 and 1023
readadc10 1, sensorValue

 

----------------------------------------------------
If - then - else
----------------------------------------------------

ARDUINO:

if(sensorValue == 100) {
// Do something
}
else if(sensorValue > 200) {
// Do something else
}
else {
// If neither of the above conditions are met then do this
}


PICAXE:

if sensorValue = 100 then
' Do something
else if sensorValue > 200 then
' Do something else
else
' If neither of the above conditions are met then do this
end if


----------------------------------------------------
Wait / delay / pause
----------------------------------------------------

ARDUINO:

delay(100); // wait for 100 milliseconds


PICAXE:

pause 100 'wait for 100 milliseconds

wait 10 'wait for 10 seconds


----------------------------------------------------
Controlling a servo
----------------------------------------------------

ARDUINO:

// This requires a few steps so I'll present a small program that
// moves a servo back and forth between 20 and 120 degrees

// Informs the compiler I'll be using the servo library
#include <Servo.h>

// Create a servo instance
Servo myservo;

void setup() {
// Initialize a servo on digital pin 9
myservo.attach(9);
}

void loop() {
// Move servo to 120 degress and wait 1000 milliseconds
myservo.write(120);
delay(1000);

// Move servo to 20 degress and wait 1000 milliseconds
myservo.write(20);
delay(1000);
}


PICAXE:

' Not sure how this works. But I saw this:
servo 0, 150

servopos 0, 150

' I suppose it moves a servo on pin 0 to 150 degress?
' I don't quite understand the difference?

A couple of questions to picaxe users

I tried to read OddBot’s compound eye code and here are some questions:

- You use the keyword “symbol” both for declaring constants AND variables?

- What does it mean when you assign a value like “b2” or “w6” to a variable?

- Also don’t know what the first 4 lines do "#picaxe 28X1 #no_data #no_table setfreq em16"

Other question:

- What is the time unit of: wait 10 in Picaxe?

- What values can you assign to a variable in Picaxe (declared with symbol x = 1 syntax)

- Is it correct to assume that readadc returns values in 8 bit while readadc10 in 10 bit?

 

More questions will follow later :slight_smile:

I dont see the point of this

I dont see the point of this post

full documentation of Arduino can be found here or go read the AVR LibC documentation

Picaxe basic can be found on their webpage.

I dunno. I think it is a

I dunno. I think it is a nice bridge for those considering expanding from one platform to the other.

Yes, full documentation is available on either language, but here you will find a comparison.

well

"I dont see the point of this post" -> Fair enough

The rest -> I know

Exactly

"here you will find a comparison." -> Exactly

But like I said it is mainly meant for people who DON’T intend to (ever) learn the other language (in my case Picaxe), who just wanna be able to READ basic code in the other language…

OK

I think the basics are getting there. But before I update the “dictionary” I’ll need a bit more help…

 

In Arduino you can declare a 1-Byte variable like this:

char oneByteVariable; -> holds a value between -128 and 127.

or

byte oneByteVariable; -> Holds a value between 0 and 255

The Picaxe equivalent would be:

symbol oneByteVariable = b1 (or b2, b3 etc…) -> Holds a value between 0 and 255

I suppose it’s not possible to hold a value between -128 and 127?

As for 2-Byte variables Arduino goes:

int twoByteVariable; -> Holds a value between -32,768 and 32,767

or

unsigned int twoByteVariable; -> Holds a value between 0 and 65535

In Picaxe:

symbol twoByteVariable = w1 (or w2, w3 etc…)

But I’m not sure whether it operates with negative values or not?

EDIT: Figured this out…

 

Besides that I plan to add examples of serial read/write, PWM output and loops (while…for…repeat…whatever they’re called in Picaxe?). So if you have picaxe examples on these it would be cool.

 

Actually I think with this one should be able to translate MOST Arduino/Picaxe applications both ways :slight_smile:

some remarks

# this could be a picaxe remark ’ just as this one

Several commands can go on a single line in picaxe basic: the newline has no special meaning.

servopos is for updating the servoposition AFTER you set it using the servo command.

Finally: the use of w0 as an equal for the b0, b1 combo i NOT optional. w0 is just a different way of referring to those two bytes. In other words: when you code
b0 = 4 b1 = 5
you will find that w0 is not empty anymore. It will be a word composed of those two bytes. Which converts to a decimal value of
4 * 256 + 5 = 1029.