Pages: [1]   Go Down
Author Topic: Sketch and C#  (Read 981 times)
0 Members and 1 Guest are viewing this topic.
Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 7
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using a sketch from http://amblone.com/ and trying to make my own program in C# to control it... But I can't get it to do anything, it works with the program from amblone, so it must have something to do with the way I tell my arduino to do it.

I have been trying different ways of sending the message to it, and latest attempt was
Code:
serialPort1.Write("0xF1 0xD50xD50xD5 0x33");

The documentation for it says
Quote
| Start flag 1     | 0xF1 |
| End flag         | 0x33 |

- Standard packet in 1-channel mode:

    RGB Values: {213, 119, 22}

    Packet:     <0xF1> - [0xD5] [0xC7] [0x16] - <0x33>

Can any of you see how it should be send, when looking at the sketch?

Code:
// Amblone code for the Arduino Mega
// Author: Bart van der Drift

// License:
// Anyone is free to change, redistribute or copy parts of this code
// as long as it is not for commercial purposes
// Please be so kind to pay credit where due

//---------------------------------------------------------------------------
//---------------------------------- DEFINES --------------------------------
//---------------------------------------------------------------------------

// Flags for the USB communication protocol
#define C_SF1 0xF1 // Startflag for 1-channel mode (1 RGB channel)
#define C_SF2 0xF2 // Startflag for 2-channel mode (2 RGB channels)
#define C_SF3 0xF3 // Startflag for 3-channel mode (3 RGB channels)
#define C_SF4 0xF4 // Startflag for 4-channel mode (4 RGB channels)
#define C_END 0x33 // End flag
#define C_ESC 0x99 // Escape character

// States for receiving the information, see the flow chart for more info
#define S_WAIT_FOR_SF  0
#define S_RECV_RGB     1
#define S_RECV_RGB_ESC 2

//---------------------------------------------------------------------------
//--------------------------- FUNCTION DECLARATIONS -------------------------
//---------------------------------------------------------------------------

// Receives bytes and returns true if a valid packet was received
boolean PacketReceived();

// Uses the rgb values to set the PWMs
void SetPWMs();

//---------------------------------------------------------------------------
//--------------------------- VARIABLE DECLARATIONS -------------------------
//---------------------------------------------------------------------------

int pulse = 0;

// State we are in: one of the S_* defines
int State = 0;
// The payload of a received message
int Payload[32];
// The amount of RGB values we have received
int ByteCount = 0;
// The character we received
int Recv;

// The amount of RGB channels we are using
int ChannelMode;

// PWM pins for channel 1
int r1_pin = 3;
int g1_pin = 5;
int b1_pin = 6;

// PWM pins for channel 2
int r2_pin = 9;
int g2_pin = 10;
int b2_pin = 11;



//---------------------------------------------------------------------------
//----------------------------- IMPLEMENTATIONS -----------------------------
//---------------------------------------------------------------------------

void setup()   {                
  // initialize the serial communication
  Serial.begin(256000);      // opens serial port, sets data rate to 256000 bps
  
  TCCR0B = TCCR0B & 0b11111000 | 0x2;
  TCCR1B = TCCR0B & 0b11111000 | 0x2;
  TCCR2B = TCCR0B & 0b11111000 | 0x2;
 
  
  State = S_WAIT_FOR_SF;
}
//---------------------------------------------------------------------------

void loop()                    
{
  if (Serial.available() > 0) {
    if (PacketReceived()) {
      SetPWMs();
    }
  }
}
//---------------------------------------------------------------------------

boolean PacketReceived() {
  Recv = Serial.read();
  
  switch (State) {
    case S_WAIT_FOR_SF:
      // =============================== Wait for start flag state
      switch (Recv) {
        case C_SF1:
          // Start flag for 1-channel mode
          ChannelMode = 1;
          State = S_RECV_RGB;
          ByteCount = 0;
          return false;
        case C_SF2:
          // Start flag for 2-channel mode
          ChannelMode = 2;
          State = S_RECV_RGB;
          ByteCount = 0;
          return false;
        case 243://C_SF3:
          // Start flag for 3-channel mode
          ChannelMode = 3;
          State = S_RECV_RGB;
          ByteCount = 0;
          return false;
        case C_SF4:
          // Start flag for 4-channel mode
          ChannelMode = 4;
          State = S_RECV_RGB;
          ByteCount = 0;
          return false;
        default:
          // No action for all other characters
          return false;
      }
      break;
    case S_RECV_RGB:
      // =============================== RGB Data reception state
      switch (Recv) {
        case C_SF1:
          // Start flag for 1-channel mode
          ChannelMode = 1;
          State = S_RECV_RGB;
          ByteCount = 0;
          return false;
        case C_SF2:
          // Start flag for 2-channel mode
          ChannelMode = 2;
          State = S_RECV_RGB;
          ByteCount = 0;
          return false;
        case C_SF3:
          // Start flag for 3-channel mode
          ChannelMode = 3;
          State = S_RECV_RGB;
          ByteCount = 0;
          return false;
        case C_SF4:
          // Start flag for 4-channel mode
          ChannelMode = 4;
          State = S_RECV_RGB;
          ByteCount = 0;
          return false;
        case C_END:
          // End Flag
          // For each channel, we should have received 3 values. If so, we have received a valid packet
          if (ByteCount == ChannelMode * 3) {
            State = S_WAIT_FOR_SF;
            ByteCount = 0;
            return true; // <------------------------ TRUE IS RETURNED
          }
          else {
            // Something's gone wrong: restart
            State = S_WAIT_FOR_SF;
            ByteCount = 0;
            return false;
          }
        case C_ESC:
          // Escape character
          State = S_RECV_RGB_ESC;
          return false;
        default:
          // The character received wasn't a flag, so store it as an RGB value        
          Payload[ByteCount] = Recv;
          ByteCount++;
          return false;
      }
      case S_RECV_RGB_ESC:
        // =============================== RGB Escaped data reception state
        // Store the value in the payload, no matter what it is
        Payload[ByteCount] = Recv;
        ByteCount++;
        State = S_RECV_RGB;
        return false;
  }
  
  return false;
}
//---------------------------------------------------------------------------

void SetPWMs() {
  // Channel 1
  analogWrite(r1_pin, Payload[0]);
  analogWrite(g1_pin, Payload[1]);
  analogWrite(b1_pin, Payload[2]);
  
  // Channel 2
  if (ChannelMode > 1) {
    analogWrite(r2_pin, Payload[3]);
    analogWrite(g2_pin, Payload[4]);
    analogWrite(b2_pin, Payload[5]);
  }
  else {
    // turn the rest to 0 (black)
    analogWrite(r2_pin, 0);
    analogWrite(g2_pin, 0);
    analogWrite(b2_pin, 0);
    
  }
}
//---------------------------------------------------------------------------
Logged

captain-slow.dk | non contagious!

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The sketch is expecting some bytes.
Code:
serialPort1.Write("0xF1 0xD50xD50xD5 0x33");
This is sending a string of characters. "0xF1" is 4 characters that bear no resemblance to the value 0xF1.
Code:
serialPort1.Write(0xF1);
serialPort1.Write(0xD5);
serialPort1.Write(0xD5);
serialPort1.Write(0xD5);
serialPort1.Write(0x33);
might produce better results.




64 bit Win7 on DELL laptops sucks. The f**king insertion point when I type keeps jumping all over the place.
Logged

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 7
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
serialPort1.Write(0xF1);
serialPort1.Write(0xD5);
serialPort1.Write(0xD5);
serialPort1.Write(0xD5);
serialPort1.Write(0x33);

I have been trying that too, the problem there is then that it isn't possible.
Code:
Error      1      The best overloaded method match for 'System.IO.Ports.SerialPort.Write(string)' has some invalid arguments      C:\Users\Michael Andresen\documents\visual studio 2010\Projects\AmbiLEDS\AmbiLEDS\Form1.cs      182      13      AmbiLEDS
Error      2      Argument 1: cannot convert from 'int' to 'string'      C:\Users\Michael Andresen\documents\visual studio 2010\Projects\AmbiLEDS\AmbiLEDS\Form1.cs      182      31      AmbiLEDS

It is my first time I play around with serial ports, and serial.read on the arduino, so I am still learning everything as I go smiley-wink
Logged

captain-slow.dk | non contagious!

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 7
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Got it!!!

cR
cG
cB

is the integer of the average color from the field it is looking at

Code:
serialPort1.Write(new byte[] { 0xF1, (byte)cR, (byte)cG, (byte)cB, 0x33 }, 0, 5);
Logged

captain-slow.dk | non contagious!

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this.
Code:
byte[] stuff = {0xF1, 0xD5, 0xD5, 0xD5, 0x33};
serialPort1.Write(stuff, 0, 5);

Why the f**k there isn't an overload to send a single byte is a mystery to me.
Logged

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 7
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Next up is that (byte)cR isn't that easy.. damn smiley-razz

need to do more to convert an integer to a byte... and it is then converted to a byte[] which can't be used in the "byte[] stuff"

Update...

Or what the hell is going on here O.O bed time! Will look at it again tomorrow, leds turning up and down, and refuses to do anything when yellow is detected...
« Last Edit: November 28, 2010, 06:21:50 pm by bld » Logged

captain-slow.dk | non contagious!

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 7
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Damn... I failed in going to bed..

[media]http://www.youtube.com/watch?v=gW_dMuFXK6I[/media]
But it works now...
« Last Edit: November 28, 2010, 06:56:45 pm by bld » Logged

captain-slow.dk | non contagious!

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure what the first minute and half of that video had to do with anything, but, I'm glad it's working for you.
Logged

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 7
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Was an attempt to show them changing with the picture... With a bit tweaking, I got it running at 25 samples/sec with 4% CPU load
Logged

captain-slow.dk | non contagious!

Pages: [1]   Go Up
Jump to: