Monday, July 16, 2012

Working with SAM-3

Here's the routine to pass cmd at repeatedly to the SAM-3.



/*
 SAM motor player

 This sketch shows how to use the serial transmit pin (pin 1) to send SAM motor data.


 */
char inByte = 0;         // incoming serial byte
char firstSensor = 0;    // first analog sensor
char secondSensor = 0;   // second analog sensor
char i =0;

void setup() {
  //  Set MIDI baud rate:
  Serial.begin(115200);

}

void loop() {
    if (i >= 200) i = 0;
    CmdOn(0x00, 0x20+i);
    delay(100);
    CmdOn(0x1E, 0x20+i);
    i=i+20;
        Serial.print("i=");            
        Serial.println(i,DEC);            

    delay(100);
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    // read first analog input:
    firstSensor = analogRead(A0);
    // read second analog input:
    secondSensor = analogRead(A1);
    // send sensor values:
    Serial.print(0xFF & firstSensor);
    Serial.print(",");
    Serial.println(0xFF & secondSensor);            
    delay(500);
  }
}

void CmdOn(int data1, int data2) {
  Serial.write(0xFF);
  Serial.write(data1);
  Serial.write(data2);
  Serial.write((data1^data2)&0x7F);  //compute checksum on the fly
}

No comments:

Post a Comment