1

I have a sketch that works just fine on my Arduino Uno board, but somehow mess up, when I try to retrieve data from a Nano Board.

I have wired a TCS230 color sensor, to get data about some colours. I get the right data using the Uno Board, but not from the Nano.

I'm not that experienced using Nano, so maybe theres some fundamental differences I'm not aware of?

This is the code I use:

#include <TimerOne.h>

#define S0     6
#define S1     5
#define S2     4
#define S3     3
#define OUT    2

int   g_count = 0;    // count the frequecy
int   g_array[3];     // store the RGB value
int   g_flag = 0;     // filter of RGB queue
float g_SF[3];        // save the RGB Scale factor


// Init TSC230 and setting Frequency.
void TSC_Init()
{
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(OUT, INPUT);

  digitalWrite(S0, LOW);  // OUTPUT FREQUENCY SCALING 2%
  digitalWrite(S1, HIGH); 
}

// Select the filter color 
void TSC_FilterColor(int Level01, int Level02)
{
  if(Level01 != 0)
    Level01 = HIGH;

  if(Level02 != 0)
    Level02 = HIGH;

  digitalWrite(S2, Level01); 
  digitalWrite(S3, Level02); 
}

void TSC_Count()
{
  g_count ++ ;
}

void TSC_Callback()
{
  switch(g_flag)
  {
    case 0: 
        // Serial.println("->WB Start");
         TSC_WB(LOW, LOW);              //Filter without Red
         break;
    case 1:
        // Serial.print("->Frequency R=");
        // Serial.println(g_count);
         g_array[0] = g_count;
         TSC_WB(HIGH, HIGH);            //Filter without Green
         break;
    case 2:
        // Serial.print("->Frequency G=");
        // Serial.println(g_count);
         g_array[1] = g_count;
         TSC_WB(LOW, HIGH);             //Filter without Blue
         break;

    case 3:
        // Serial.print("->Frequency B=");
        // Serial.println(g_count);
        // Serial.println("->WB End");
         g_array[2] = g_count;
         TSC_WB(HIGH, LOW);             //Clear(no filter)   
         break;
   default:
         g_count = 0;
         break;
  }
}

void TSC_WB(int Level0, int Level1)      //White Balance
{
  g_count = 0;
  g_flag ++;
  TSC_FilterColor(Level0, Level1);
  Timer1.setPeriod(1000000);             // set 1s period
}

void setup()
{
  TSC_Init();
  Serial.begin(9600);
  Timer1.initialize();             // defaulte is 1s
  Timer1.attachInterrupt(TSC_Callback);  
  attachInterrupt(0, TSC_Count, RISING);  

  delay(4000);

  for(int i=0; i<3; i++)
    Serial.println(g_array[i]);

  g_SF[0] = 255.0/ g_array[0];     //R Scale factor
  g_SF[1] = 255.0/ g_array[1] ;    //G Scale factor
  g_SF[2] = 255.0/ g_array[2] ;    //B Scale factor

  Serial.println(g_SF[0]);
  Serial.println(g_SF[1]);
  Serial.println(g_SF[2]);

}

void loop()
{
   g_flag = 0;
   //for(int i=0; i<3; i++)
    //Serial.println(int(g_array[i] * g_SF[i]));

    int R = int(g_array[0] * g_SF[0]);
    int G = int(g_array[1] * g_SF[1]);
    int B = int(g_array[2] * g_SF[2]);    


    if((R>245) && (R<265) && (G>245) && (G<265) && (B>245) && (B<265))    // White
    {
      Serial.println("Color is white");
    }else if((R>24) && (R<44) && (G>56) && (G<76) && (B>95) && (B<115))  // Blue
    {
      Serial.println("Color is blue");
    }else if((R>56) && (R<76) && (G>107) && (G<127) && (B>80) && (B<100))  // Green
    {
      Serial.println("Color is green");    
    }else if((R>171) && (R<191) && (G>154) && (G<174) && (B>71) && (B<91))  // Yellow
    {
      Serial.println("Color is yellow");  
    }else if((R>77) && (R<97) && (G>46) && (G<66) && (B>43) && (B<63))  // Brown
    {
      Serial.println("Color is brown");    
    }else if((R>39) && (R<59) && (G>38) && (G<58) && (B>36) && (B<56))  // Brown dark
    {
      Serial.println("Color is brown dark");    
    }else if((R>20) && (R<40) && (G>43) && (G<63) && (B>48) && (B<68))  // Turkis
    {
       Serial.println("Color is turkis");   
    }else if((R>39) && (R<59) && (G>77) && (G<97) && (B>55) && (B<75))  // Green
    {
       Serial.println("Color is green");   
    }else
    {
      Serial.print("Red = ");
      Serial.println(R);
      Serial.print("Green = ");
      Serial.println(G);
      Serial.print("Blue = ");
      Serial.println(B);

    };

   delay(400);

}

UPDATE! This is the Serial Monitor output from the Uno:

// Color is white is correct here, since it starts with a calibration
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
// Here it changes, which is correct, since the color in front of the sensor was changed
Red = 270
Green = 336
Blue = 427
Red = 231
Green = 197
Blue = 155
Red = 39
Green = 71
Blue = 149
Red = 22
Green = 36
Blue = 64
Red = 20
Green = 36
Blue = 64

This is the Serial Monitor output from the Nano

// Excepted output, since it starts with calibrating
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
// Here the color is changed, but the output doesn't reflect the output?
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white
Color is white

All help would be appreciated!

  • Did you connect to the correct pins on the new board? – Treesrule14 Nov 6 '14 at 13:32
  • 1
    I did and checked it more than once. I also recieve an output. – Tommy Otzen Nov 6 '14 at 13:45
  • Can you give some more details on what the data looks like from the nano – Treesrule14 Nov 6 '14 at 14:06
  • Could it be that TimerOne.h doesn't work with Nano? – Tommy Otzen Nov 6 '14 at 14:32
  • You should be able to check with print statements. (Ie set up the timer to go off every second and print out a statement and see what happens) Also I did some research and it seems unlikely to me that timer1 doesn't work for two reasons 1) the whole point of arduino is portability 2) the nano has a chip from the same family so the timer should also work – Treesrule14 Nov 6 '14 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.