Go Down

Topic: Send correct data to SC (Read 609 times) previous topic - next topic

Atap

Hi there,

I have a sensor that reads the light.
the sensor is connected to Analog 1.

in Serial Monitor when I print the "light level" I get values from 0 to ~5 as the datasheet refers.

But when I load standar firmata and I connected with sc I get other values 0 - ~632 in SC

why this is happening?

how it is possible to receive the actual values in SC that my arduino sketch reads?

robtillaart

Strange...
can you post your sketch?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Atap

here is the arduino code:

Code: [Select]
#include <Wire.h> //I2C needed for sensors

//Pin definitions

// digital I/O pins

const byte STAT1 = 7;
const byte STAT2 = 8;

// analog I/O pins

const byte LIGHT = A1;

const byte REFERENCE_3V3 = A3;
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\\



//=-=-=-=-=-=-=-=-=-=-= Global Variables =-=-=-=-=-=-=-=-=-=-=\\

long lastSecond; //The millis counter to see when a second rolls by
byte seconds; //When it hits 60, increase the current minute
float light_lvl = 455; //[analog value from 0 to 1023]


void setup() {

  Serial.begin(9600);
  Serial.println("Weather Shield Example");

  pinMode(STAT1, OUTPUT); //Status LED Blue
  pinMode(STAT2, OUTPUT); //Status LED Green

  pinMode(LIGHT, INPUT);

  seconds = 0;
  lastSecond = millis();

  // attach external interrupt pins to IRQ functions


  // turn on interrupts
  interrupts();

  Serial.println("Weather Station online! :)");

}

void loop() {

  //Keep track of which minute it is
  if(millis() - lastSecond >= 1000)
  {
    digitalWrite(STAT1, HIGH); //Blink stat LED

    lastSecond += 1000;

    //Calc the wind speed and direction every second for 120 second to get 2 minute average

        calcWeather();

    digitalWrite(STAT1, LOW); //Turn off stat LED
  }

  delay(100);
}

//light function


void calcWeather() {
 

  //Get light level
  light_lvl = get_light_level();
  Serial.print(" | Light level = ");
  Serial.println(light_lvl, 2);

}

//Read the light level

float get_light_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float lightSensor = analogRead(LIGHT);

  operatingVoltage = 3.3 / operatingVoltage; //The reference voltage is 3.3V

  lightSensor = operatingVoltage * lightSensor;

  return(lightSensor);
}




And here is the SC code



Code: [Select]
SCPyduino

SerialPort.listDevices

// Connect with arduino
(
~connectArduino = Routine{
~arduino = SCPyduino.new("/dev/tty.usbmodem1421", 9600);
4.wait;
"The Firmata firmware version on this Arduino board is: ".post;
~arduino.firmataVersion.postln}.play;
)

//activate the analog pin
(
~arduino.analog[1].active_(1);
)


(
~arduinoLoop = {loop {
~arduino.iterate;
~arduino.analog[1].value.postln;
}}.fork;
)


Arduino serial monitors reads 0 to 3.3 because the light sensor is based on the 3.3V rail
In Supercollider I am reading from Analog 1 but I get values from 0 to ~632

robtillaart


As the Arduino multiplexes the analogReads they affect each other. Could be a cause
One way to handle this is to do multiple reads and throw away the first one.

Code: (compressed a bit) [Select]

float get_light_level()
{
  analogRead(REFERENCE_3V3);  // dummy read
  float operatingVoltage = analogRead(REFERENCE_3V3);

  analogRead(LIGHT); // dummy read
  return analogRead(LIGHT) * 3.3 / operatingVoltage;
}


632  / 3.3  *  5.0 = 957 ( comes close to 1023)  could that be a cause?

632 just being the max value for 3.3 volt
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up