Let's Make Robots!

Converting a PIC program, to arduino C

Hi, i found this code on the net, but i need it in arduino C, i have no knowledge on it, and no idea where to start translating...

there is also this one, in basicstamp i presume...

 

Thanks.

AttachmentSize
TCS230.zip2.81 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

you should be able to attach a file to the main post, no?

Completely forgot about .zip, it's there now :)

Nope, not a rar file

Hey if you get it working (either with this or some other way) please let me know for future reference.

Cheers

That one didn't work, this one works:

#include <TCS230.h>

/*
This sketch uses the TCS230 light sensor and outputs its data to the
computer. It assumes the use of John Sarik's TCS230-to-Arduino
mounting board. Sending it X characters over the serial port will
cause it to return X readings back through the serial port.
*/

TCS230 sensor;
char buffer;

void setup()
{
 
    Serial.begin(9600);
    sensor.initialize();
    Serial.println("serial communication initialized");
}

void loop()
{
    if (Serial.available())
    {
      buffer = Serial.read();
      Serial.print("red: ");
      Serial.println(sensor.readRed());
   
      Serial.print("blue: ");
      Serial.println(sensor.readBlue());
   
      Serial.print("green: ");
      Serial.println(sensor.readGreen());
   
      Serial.print("clear: ");
      Serial.println(sensor.readClear());
   
      Serial.print("\n\n");
     
    } 
}

though it needs a library to compile, and since lmr doesn't accept .rar files i dunno how to put it here...

 

Hi Amando,

DISCLAIMER: I don't have a TCS230 to try this out on, use at your own risk.

This is more or less a direct port of the code you linked to. Hopefully you can use it with your sensor. Note that the picbasic code uses a function "count" that we don't have in Arduino C. I've tried to use pulseIn() in its place, and I think that will work.

Anyway, give it a go. You may need to change the output frequency scaling setting - right now it's at 100%. You can change it in the setup() function below:

This gives 100%:

  digitalWrite(s0, HIGH);
  digitalWrite(s1, HIGH);

This gives 20%:

  digitalWrite(s0, HIGH);
  digitalWrite(s1, LOW);

And this gives 2%:

  digitalWrite(s0, LOW);
  digitalWrite(s1, HIGH);

Cheers,

Phil

// Note this defines the digital pins on your arduino - e.g., connect the OE leg of the ic to digital pin 2 on your arduino etc

// TCS230 connections:
const int outputEnabled = 2; // write LOW to turn on
const int s0 = 3; // sensor pins
const int s1 = 4;
const int s2 = 5;
const int s3 = 6;
const int nLED = 7; // illuminating LED
const int out = 8; // TCS230 output

// variables to store color values
int red = 0;
int green = 0;
int blue = 0;

void setup() {
  pinMode(outputEnabled, OUTPUT);
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(nLED, OUTPUT);
  pinMode(out, INPUT);
  Serial.begin(9600);
 
  digitalWrite(outputEnabled, LOW);
  digitalWrite(s0, HIGH);
  digitalWrite(s1, HIGH);
  digitalWrite(nLED, LOW);
}

void loop() {

  colour();

  Serial.print("R");
  Serial.print(red, DEC);
  Serial.print(" G");
  Serial.print(green, DEC);
  Serial.print(" B");
  Serial.print(blue, DEC);
  Serial.println();
}

void colour() {
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  //   count OUT, pRed, RED
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s3, HIGH);
  //count OUT, pBLUE, BLUE
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s2, HIGH);
  // count OUT, pGreen, GREEN
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}