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?