Let's Make Robots!

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?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

# 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.

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 dont see the point of this post" -> Fair enough

The rest -> I know

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.

"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...

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 :)

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 :)

But since you do not normally use picaxe I'll explain. (the manuals are a free download)
SYMBOL is used for both variables and constants. 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.

The first line specifies which chip the code is designed for.
The second and third lines speed up the programming cycle by not downloading any data or tables.
The fourth line specifies the clock speed. Many picaxe chips have an internal oscillator but I have specified the external 16MHz resonator.

the standard time unit for the picaxe basic is usually milliseconds although there is a pause command for uS. This is affected by clockspeed as the language assumes the default internal 4MHz clock.

Yes, readadc is 8 bit so a byte variable can be used. Readadc10 is 10bit so a wordvariable is used.

 

Remember, Picaxe basic is not ment to be a sophisticated or powerful language. It is a simple basic language. Based around the microcontrollers abilities and designed to get people who have never programmed before to have some fun with microcontrollers.

Hehe..people can tell me RTFM as much they want but I don't intend to, since I have NO plans of EVER buying a Picaxe board/chip nor writing a single line of Picaxe code.

Like I said I created this thread with the purpose of enabling Picaxe users and Arduino users to share their code, so whenever someone makes something cool (like your compound eye or fritsl's low tech RC protocol) it can be shared across the two platforms. I thought this would be of interest to many others as well :)

I did NOT create this topic with the selfish purpose of learning Picaxe and because I'm too lazy to read a manual. The idea was to create a simple Picaxe/Arduino-"dictionary" of the most basic commands for ALL to use. I hope that is clear and we can avoid too many RTFM's!!

 

To your last comment...I know...But Arduino is also aimed towards designers and non-technical users, which is why this "dictionary" may be of use to many people in both camps. However Arduino is based on rudimentary C, is expandable and therefore more attractive to people with a programming background like me. And perhaps some Picaxe users wanting to make something a bit more complicated could also use this to get started with Arduino...

 

Thanks for your input :)

Aniss