Arduino-PicBasicPro Code Translation
Arduino-PicBasicPro Code Translation
// 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
// 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
// Clear LCD
lcd.clear();
Keypad Serial Interface and Compound Logic: Keypad Serial Interface and Compound Logic:
// Define function
void process_display (void) {
…
}
// 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
// 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
For additional help, compare commands in the Arduino language reference page and the PicBasic Pro manual.