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
}

Sunday, July 15, 2012

Using Arduino Firmata with Processing

Attempted to use the Standard Firmata on the Arduino Uno R2 to control 2 servos on pin 9 & 10 however it didsn't work.   It was replaced by the Analog Firmata and only the following Processing program worked.


import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int servoPinX = 10;
int servoPinY = 9;
int sensorPin = 0;
float sensorValue = 0;
int posX=0, posY=0; //servo position in degrees (0..180)

void setup()
{
 size(360, 360);

 arduino = new Arduino(this, Arduino.list()[1], 57600); //your offset may vary
 arduino.pinMode(servoPinX,4);
 arduino.pinMode(servoPinY,4);
 arduino.pinMode(sensorPin,0);
}

void draw()
{
 sensorValue = arduino.analogRead(0);
 println(sensorValue);

// read mouseX coordinate
 int newPosX = constrain(mouseX/2,40,140); // update bg & servo position if mouseX changed
// read mouseY coordinate
 int newPosY = constrain(mouseY/2,40,140); // update bg & servo position if mouseX changed

 if(newPosX != posX ||newPosY != posY)
 {
   background(newPosX);
      arduino.analogWrite(servoPinX, newPosX);
      arduino.analogWrite(servoPinY, newPosY);
   println (" newPos = " + newPosX );
   posX=newPosX; //update servo position storage variable
   posY=newPosY; //update servo position storage variable
 
 }
}