B4R Question Battlestar galactica cylon scanner - Arduino c to B4R

h725

Active Member
Licensed User
Longtime User
Hello I am working on a battlestar galactica cylon scanner and have found
the following snippet:

B4X:
void setup() {
  // put your setup code here, to run once:
for (int pin= 2; pin <9; pin++) {
  pinMode(pin,OUTPUT);
}
}

void loop() {
  // put your main code here, to run repeatedly:
  int a = 2;
int t1 =35;
int t2 = 1000;

if (a=1){
for (int i = 2; i<9;i++){
  digitalWrite(i,HIGH);
  delay(t1);
   digitalWrite(i+1,HIGH);
  delay(t1);
   digitalWrite(i+2,HIGH);
  delay(t1);
   digitalWrite(i,LOW);
//  delay(t1);
   digitalWrite(i+1,LOW);
}
for (int i = 8; i>1;i--){
  digitalWrite(i,HIGH);
  delay(t1);
  digitalWrite(i-1,HIGH);
  delay(t1);
   digitalWrite(i-2,HIGH);
  delay(t1);
   digitalWrite(i,LOW);
  delay(t1);
   digitalWrite(i-1,LOW);
}
}
}

I would like to understand how to translate this code to B4R. I looked at the
LED sample snippet and found out, that every LED has to be initialized first.
Can somebody give me an idea what is the best way to translate the shown
code to B4R?

Thank you very much!
h725
 

Cableguy

Expert
Licensed User
Longtime User
Create a for/next loop with step +1 and when at the last step set the step to -1
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Start with:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Dim pins(11) As Pin
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   For i = 1 To 10
       pins(i).Initialize(i, pins(i).MODE_OUTPUT)
   Next
   Dim t1 As Int = 35
   For i = 2 To 8
       pins(i).DigitalWrite(True)
       Delay(t1)
       pins(i + 1).DigitalWrite(True)
       Delay(t1)
       pins(i + 2).DigitalWrite(True)
       Delay(t1)
       pins(i).DigitalWrite(False)
       pins(i + 1).DigitalWrite(False)
   Next
   For i = 8 To 2 Step - 1
       '...
   Next
End Sub
Note that this code writes to pin #1 which is the serial port pin. It doesn't look correct.
 
Upvote 0
Top