Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
55 views

Arduino-PicBasicPro Code Translation

This document provides examples of translating code from Arduino to PicBasic Pro by summarizing the key differences in code structure, comments, digital input/output, functions, analog input, and keypad interfaces. The examples show how Arduino code using C/C++ syntax can be rewritten using a similar logic in the PicBasic Pro BASIC syntax by replacing constructs like loops, conditional statements, and function definitions.

Uploaded by

largosoft
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Arduino-PicBasicPro Code Translation

This document provides examples of translating code from Arduino to PicBasic Pro by summarizing the key differences in code structure, comments, digital input/output, functions, analog input, and keypad interfaces. The examples show how Arduino code using C/C++ syntax can be rewritten using a similar logic in the PicBasic Pro BASIC syntax by replacing constructs like loops, conditional statements, and function definitions.

Uploaded by

largosoft
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Arduino-to-PicBasicPro Code Translation Examples

Arduino code PicBasic Pro equivalent


Comments/Remarks: Code Structure, Comments/Remarks:

/* A multiple-line comment is ‘ A multiple-line comment is


bracketed like this */ ‘ bracketed like this

// Here’ s a single-line comment ‘ Here’ s a single-line comment

… code … // end-of-line remark … code … ‘ end-of-line remark

Code Structure: Code Structure:

// Declare global variables ‘ Declare global variables


… …

// Define and initialize pins and special features ‘ Define and initialize pins and special features
void setup() { …

} ‘ Infinite loop run continuously
Do While (1)
// Infinite loop run continuously ….
void loop() { ‘ Call subroutine/function
…. Gosub my_function
// Call subroutine/function …
my_function(); Loop

} End

// Define function ‘ Define function


void my_function(void) { my_function:
… …
} Return

Digital Input/Output and Logic: Digital Input/Output and Logic:

const int buttonPin = 2; buttonPin Var PORTA.3


const int ledPin = 13; ledPin Var PORTB.7

void setup() { Output ledPin ‘ not required (done by High/Low)


pinMode(ledPin, OUTPUT); Input buttonPin ‘ not required (default)
pinMode(buttonPin, INPUT);
} ‘ Turn on the LED if the button is down
If (buttonPin == 1) Then
// Turn on the LED if the button is down High ledPin
if (digitalRead(buttonPin) == HIGH) Else
digitalWrite(ledPin, HIGH); Low ledPin
else Endif
digitalWrite(ledPin, LOW);

Using a Function or Subroutine: Using a Function or Subroutine:

const int ledPin = 13; ledPin Var PORTB.7


short i; I Var BYTE

void setup() { Gosub blink_led ‘ blink the LED once


pinMode(ledPin, OUTPUT);
} ‘ Blink the LED 5 times
For I = 1 to 5
blink_led(); // blink the LED once Gosub blink_led
Next I
// Blink the LED 5 times
for (i=1; i<=5; i++) ‘ Subroutine to blink an LED
blink_led(); blink_led:
High ledPin
// Function to blink an LED Pause 500
void blink_led() { Low ledPin
digitalWrite(ledPin, HIGH); Pause 500
delay(500); Return
digitalWrite(ledPin, LOW);
delay(500);
}

Analog Input and Scaling: Analog Input and Scaling:

int x_raw; x_raw Var WORD


int x_scaled; x_scaled Var BYTE

x_raw = analogRead(A0); Adcin 0, x_raw

// Scale raw value from 0-1023 to 0-255 range ‘ Scale raw value from 0-1023 to 0-255 range
x_scaled = map(x_raw, 0, 1023, 0, 255); x_scaled = x_raw / 4 ‘ scale from 10 bits to 8

// Limit the scaled value to within a range


x_scaled = constrain(x_scaled, 0, 255);

LCD Output (count-down timer): LCD Output (count-down timer):

#include <LiquidCrystal.h> i Var BYTE

short i; ‘ Wait for LCD to power up


Pause 500
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
‘ Display count-down from 10 to 0 on the LCD
void setup() { For i = 10 To 0
lcd.begin (16, 2); ‘ Clear the LCD and display count-down text
} ‘ and value
Lcdout $FE, 1, "count down: "
// Display count-down from 10 to 0 on the LCD Lcdout $FE, $C0, “ “, Dec i
for (i=10; i>0; i--) { Pause 1000; ‘ pause for 1 second
/* Clear the lcd and display the count-down Next i
text and value */
lcd.clear(); ‘ Clear the LCD
lcd.print("count down:"); Lcdout $FE, 1
// move cursor to 2nd row, 2nd col
lcd.setCursor(1, 1);
lcd.print(i);
delay(1000); // pause for 1 second
}

// Clear LCD
lcd.clear();

Keypad Serial Interface and Compound Logic: Keypad Serial Interface and Compound Logic:

// Define variables ‘ Define variables


byte key_val; // button value key_pin Var PORTB.0 ‘ input pin
byte number; // value changed by buttons key_val Var BYTE ‘ button value
key_mode Con 0 ‘ selects 2400 baud
// Define keypad button codes number Var BYTE ‘ value changed by buttons
const byte key1=0x30;
const byte key2=0x31; ‘ Define keypad button codes
const byte key3=0x32; key1 Con $30
key2 Con $31
void setup() { key3 Con $32
/* Initialize serial communication
(receiving on pin 0) */ ‘ Keypad processing loop
Serial.begin(2400); Do While (1) ‘ do always (infinite loop)
} ‘ Wait for a keypad button to be pressed
‘ and read the value
// Keypad processing loop Serin key_pin, key_mode, key_val
while (true) { // do always (infinite loop)
// Wait for a keypad button to be pressed ‘ Perform the appropriate function
while (Serial.available() == 0); If ((key_val = key1) And (number > 0)) Then
// Read the keypad value from the buffer ‘ decrement
key_val = Serial.read(); number = number – 1
ElseIf (key_val = key2) Then
// Perform the appropriate function ‘ reset
if ((key_val == key1) && (number > 0)) { number = 0
// decrement ElseIf ((key_val = key3) And (number < 255)) Then
number--; ‘ increment
} number = number + 1
else if (key_val == key2) { EndIf
// reset
number = 0; ‘ Call a subroutine to process the number
} Gosub process_display
else if ((key_val == key3) && (number < 255)) { Loop
// increment End
number++;
} ‘ Define function
process_display:
// Call a function to process the number …
process_display(); Return
}

// Define function
void process_display (void) {

}

Servo Motor Control: Servo Motor Control:

#include <Servo.h> ‘ Servo duty cycle info:


‘ position (degrees) = pulse width (ms) =
// Define variables ‘ duty cycle (%) = Hpwm 0-255 value
const int servoPin=9; ‘ 0 degrees = 1 ms = 5% = 13
const int sensorPin=2; ‘ 90 degrees = 1.5 ms = 7.5% = 19
Servo myServo; ‘ 180 degrees = 2 ms = 10% = 25
int ang; // servo angle servoFreq Con 50 ‘ 1/(20ms) = 50Hz

// Initialize I/O pins ‘ Define variables


void setup() { dutyCycle Var WORD
pinMode(sensorPin, INPUT); servoPin Var PORTB.0 ‘ RB0 (pin 9 set to CCP1)
myServo.attach(servoPin); sensorPin Var PORTA.3
} duty_cycle Var BYTE ‘ servo angle

/* Continually sweep over the full servo range ‘ Initialize pins


checking a digital sensor every 15 degrees */ Input sensorPin;
void loop() { ‘ Output servoPin ‘ not required (done by Hpmw)
// Start in the 0 degree servo position
myServo.write(0); ‘ Continually sweep over the full servo range
// Wait for servo to go to (or return to) 0 position ‘ checking a digital sensor every 15 degrees
delay(1000) Do While (1) ‘ do always (infinite loop)
‘ Start in the 0 degree servo position
for (ang=15; ang<=180; ang+=15) { Hpwm 1, 13, servoFreq
myServo.write(ang); ‘ Wait 1s for servo to go to (or return to) 0 position
delay(500); // wait 0.5s for servo to move Pause 1000

// Read the sensor and react accordingly For dutyCycle = 13 To 25 ‘ step = 1 = 15 deg.
if (digitalRead(sensorPin) == HIGH) Hpwm 1, dutyCycle, servoFreq
sensor_react(); ‘ Can use PULSOUT instead to get finer control
} Pause 500 ' wait 0.5s for servo to move
}
‘ Read the sensor and react accordingly
// Process the sensor detect event If (sensorPin) Then Gosub sensorReact
void sensor_react() { Next dutyCycle
// Do something here Loop
} End

‘ Process the sensor detect event


sensorReact:
‘ Do something here
Return

Sending a Song to a Speaker: Sending a Song to a Speaker:

// Define note pitches (in Hz) ‘ Define note pitches (in Hz)
// (can put in “pitches.h” instead NOTE_C Con 262
// with #include “pitches.h”): NOTE_D Con 294
#define NOTE_C 262 NOTE_E Con 330
#define NOTE_D 294 NOTE_G Con 392
#define NOTE_E 330
#define NOTE_G 392 ' Define variables
speakerPin Var PORTB.0
// Define variables buttonPin Var PORTA.3
const int speakerPin=9; n Var BYTE
const int buttonPin=2; i Var BYTE
const int n=30; // number of notes
int i; ' Song notes
n = 30 ‘ number of notes
// Song notes notes Var WORD[30]
int notes[] = { NOTE_E, NOTE_D, NOTE_C, ‘ Note – the compact Arraywrite function works
NOTE_D, NOTE_E, NOTE_E, NOTE_E, 0, ‘ only for BYTE variables
NOTE_D, NOTE_D, NOTE_D, 0, notes[0]=NOTE_E : notes[1]=NOTE_D
NOTE_E, NOTE_G, NOTE_G, 0, notes[2]=NOTE_C : notes[3]=NOTE_D
NOTE_E, NOTE_D, NOTE_C, NOTE_D, notes[4]=NOTE_E : notes[5]=NOTE_E
NOTE_E, NOTE_E, NOTE_E, NOTE_E, notes[6]=NOTE_E : notes[7]=0
NOTE_D, NOTE_D, NOTE_E, NOTE_D, notes[8]=NOTE_D : notes[9]=NOTE_D
NOTE_C, 0 }; notes[10]=NOTE_D : notes[11]=0
notes[12]=NOTE_E : notes[13]=NOTE_G
// Song note durations (in ms) notes[14]=NOTE_G : notes[15]=0
int durations[] = { 500, 500, 500, notes[16]=NOTE_E : notes[17]=NOTE_D
500, 500, 500, 500, 500, notes[18]=NOTE_C : notes[19]=NOTE_D
500, 500, 500, 500, notes[20]=NOTE_E : notes[21]=NOTE_E
500, 500, 500, 500, notes[22]=NOTE_E : notes[23]=NOTE_E
500, 500, 500, 500, notes[24]=NOTE_D : notes[25]=NOTE_D
500, 500, 500, 500, notes[26]=NOTE_E : notes[27]=NOTE_D
500, 500, 500, 500, notes[28]=NOTE_C : notes[29]=0
1500, 500 };
' Song note durations (in ms)
// Initialize I/O pins durations Var WORD[30]
void setup() { durations[0]=500 : durations[1]=500
pinMode(speakerPin, OUTPUT); durations[2]=500 : durations[3]=500
pinMode(buttonPin, INPUT); durations[4]=500 : durations[5]=500
} durations[6]=500 : durations[7]=500
durations[8]=500 : durations[9]=500
// Play “Mary Had a Little Lamb” while button down durations[10]=500 : durations[11]=500
void loop() { durations[12]=500 : durations[13]=500
if (digitalRead(buttonPin) == HIGH) { durations[14]=500 : durations[15]=500
for (i=0; i<n; i++) { durations[16]=500 : durations[17]=500
tone (speakerPin, notes[i], durations[i]); durations[18]=500 : durations[19]=500
// Add slight pause (50 ms) between notes durations[20]=500 : durations[21]=500
delay (50); durations[22]=500 : durations[23]=500
} durations[24]=500 : durations[25]=500
} durations[26]=500 : durations[27]=500
} durations[28]=1500 : durations[29]=500

' Initialize I/O pins


‘ Output speakerPin ‘ not necessary
Input buttonPin

‘ Play “Mary Had a Little Lamb” while button down


Do While (buttonPin)
For i = 0 To n-1
Freqout speakerPin, durations[i], notes[i]
‘ could use the Sound command instead
‘ Add slight pause (50 ms) between notes
Pause 50;
Next i
Loop

For additional help, compare commands in the Arduino language reference page and the PicBasic Pro manual.

You might also like