Main Site Documentation

Need help converting Arduino code to C#


#1

Hi all,

Is anybody willing to help me in converting this Arduino code to C# ?
(i don’t need the Serial.print statements)

I know this should be using OutputCompare, but I have no clue how to construct the timingsBuffer from the code below.


 #define SENDER 12
 #define LED 13

int state = LOW;
const int rfdelay = 345;
const int rf_repeat = 3;

char onCommand[3] = "0F";
char offCommand[3] = "F0";

char Systemcode[6] = "0FFFF";  
char Unit[6]  = "0FFFF";  
char* completeCode="123456789012";

void setup() 
{
  pinMode( LED, OUTPUT );
  Serial.begin(9600); 
  Serial.println("Running"); 
}

void loop() 
{
  state = !state;
  if (state == HIGH) 
  {
    digitalWrite(LED, HIGH);
    Serial.println("Sender ON");
    completeCode[0]='\0';
    strcat(completeCode, Systemcode);
    strcat(completeCode, Unit);
    strcat(completeCode, onCommand);
    strcat(completeCode, '\0');  
    Serial.print("complete on code:  "); 
    Serial.println( completeCode ); // prints 0FFFF0FFFF0F
  } 
  else {
    completeCode[0]='\0';
    strcat(completeCode, Systemcode);
    strcat(completeCode, Unit);
    strcat(completeCode, offCommand);
    strcat(completeCode, '\0'); 
    Serial.print("complete off code: ");
    Serial.println( completeCode ); // prints 0FFFF0FFFFF0
    digitalWrite(LED, LOW);
    digitalWrite(SENDER, LOW);
    Serial.println("Sender OFF");
  }
  sendCodes( completeCode );
  delay(10000);
}

void sendCodes( char* code ) 
{
  char ch;
  for (int i=1; i<=rf_repeat; i++) 
  {
    for(int i2=0; i2<=11; i2++) 
    {
      ch = code[i2];
      switch(ch) {
      case '0':
        sendNull();
        break;
      case 'F':
        sendFloat();
        break;
      case '1':
        sendOne();
        break;
      }
    }
    sendSync();
  }
}

void sender(int state) 
{
  digitalWrite( SENDER, state ); 
}

void wait() 
{
  delayMicroseconds(rfdelay); 
}

void sendNull() 
{
  sender(1);
  wait();
  sender(0);
  wait();
  wait();
  wait();
  sender(1);
  wait();
  sender(0);
  wait();
  wait();
  wait();      
}

void sendFloat() 
{
  sender(1);
  wait();
  sender(0);
  wait();
  wait();
  wait();
  sender(1);
  wait();
  wait();
  wait();
  sender(0);
  wait();
}

void sendOne() 
{
  sender(1);
  wait();
  sender(1);
  wait();
  sender(1);
  wait();
  sender(0);
  wait();
  sender(1);
  wait();
  sender(1);
  wait();
  sender(1);
  wait();
  sender(0);
  wait();
}

void sendSync()
{
  sender(1);
  wait();
  sender(0);
  for (int w = 0; w < 31; w++)
  {
    wait();
  }
}

Thanks,
Eric


#2

The timingsarray is build this way: low, high, low, high, …
For SendNULL()


uint[] timings = new uint[]{0*rfdelay, rfdelay, 3*rfdelay,rfdelay,3*rfdelay};


#3

zerov83, thanks.

Can I call oc1.Set(false, timings, 0, 2, false); multiple times or do I have to construct the buffer completely first.

meaning, will:


uint[] timings1 = new uint[]{0*rfdelay, rfdelay};
uint[] timings2 = new uint[]{rfdelay,3*rfdelay};
oc1.Set(false, timings1, 0, 2, false);
oc1.Set(false, timings2, 0, 2, false);

have the fame effect as


uint[] timings = new uint[]{0*rfdelay, rfdelay, rfdelay,3*rfdelay};
oc1.Set(false, timings, 0, 4, false);

?


#4

Ok, I got it working, though I’m not sure if I’m doing it the right way.


      oc1 = new OutputCompare((Cpu.Pin)RF433pin, false, tim.Length);
      oc1.Set(true, tim, 0, tim.Length, true);
      Thread.Sleep(125);
      oc1.Dispose();

What this does is send the buffer to a RF433 transmitter. If I comment out the oc1.Dispose(), it keeps on transmitting. Is this how OutputCompare has to be used?

Ideally I should send the buffer 3 times in a row.


#5

Nobody?


#6

Dispose will clean up any objects and memory that the OutputCompare uses. This will stop the output compare from sending.

The last parameter, which you have set to true, is defined as:

bufferRepeating
Type: System.Boolean
State transitions loop again when done.

By setting it to true, it will continue sending until it is stopped. Simply set this to false, and run set 3 times OR (probably better) triple the length of your timing array.