Functions or Library?

Hello all.

I am working on a color sorting project similar to the skittles sorting machine. I have the code written, and It compiles. My question is this: The code is very ugly as is, and I would like to make it look better. Should I put the color reading in a function, or should I make it in a library? If I made it into a function, how would I return the data that is returned originally?

//These are the Libraries that MUST be included
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <Servo.h>
#include <Stepper.h>
//These are the pin definitions
#define redpin 9
#define greenpin 10
#define bluepin 11
#define commonAnode true
//These are the positions
int posMetal = 45;
int posPlastic = 90;
int posWood = 135;
//Servo and Stepper
Stepper rotStep = Stepper(32, 4, 5, 6, 7);
Servo posServo;
//This is the Color sensor setup
byte gammatable[256];
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_2_4MS, TCS34725_GAIN_4X);



void setup() {
  Serial.begin(9600);
  Serial.println("Recycling Sorting Machine!");

  //This is the setup for the color sensor
  if (tcs.begin()) {
    Serial.println("Found Sensor");
  } else {
    Serial.println("No sensor found...Check your connections");
    while (1);
  }
  //This sets up the RGB LED outputs.
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  //Speet for stepper motor
  rotStep.setSpeed(20);
  //Setup for servo
  posServo.attach(3);

  //This is the gamma table
  for (int i=0; i<256; i++) {
    float x = i;
    x /= 255;
    x = pow(x, 2.5);
    x *= 255;

    if (commonAnode) {
      gammatable[i] = 255 - x;
    } else {
      gammatable[i] = x;
    }
    //Gamma table end
  }
}

void loop() 
{
  rotStep.step(8); //steps the motor 90 degrees
  delay(200); //Gives the program time to drop ball before moving on

This is the part that I would like to be seperated from the rest of the program

  //BELOW IS THE COLOR SENSING AND STORAGE
  uint16_t clear, red, green, blue;

  tcs.setInterrupt(false);// turn on LED

  delay(60);  // takes 50ms to read 
  
  tcs.getRawData(&red, &green, &blue, &clear);

  tcs.setInterrupt(true);
  
  Serial.print("C:\t"); Serial.print(clear);
  Serial.print("\tR:\t"); Serial.print(red);
  Serial.print("\tG:\t"); Serial.print(green);
  Serial.print("\tB:\t"); Serial.print(blue);

  // Figure out some basic hex code for visualization
  uint32_t sum = clear;
  float r, g, b;
  r = red; r /= sum;
  g = green; g /= sum;
  b = blue; b /= sum;
  r *= 256; g *= 256; b *= 256;

And this is the remaining code

  //Now It will take that data and compare
  if (1 == 1)//replace with if it matches criteria
  {
    posServo.write(posMetal);
  }
  if (1 == 1) //replace with critea for wood
  {
    posServo.write(posWood);
  }
  if (1 == 1) //replace with plastic
  {
    posServo.write(posPlastic);
  }
}

Function. You can actually see an example of how the data can be returned in this method:

  tcs.getRawData(&red, &green, &blue, &clear);

aarg:
Function. You can actually see an example of how the data can be returned in this method:

  tcs.getRawData(&red, &green, &blue, &clear);

So, to define a function that returns those, could I write this?

fuctionName(returnVal1, returnVal2, etc);

And how do I actually return those values?

So, to define a function that returns those, could I write this?

A function returns a value using a return statement.One value.

The function that you were looking at passes by reference - that is, the address of a variable is passed, not the value in the variable. Since you have the address, you can write at that location.

void fun(int &ref1, int &ref2, int &ref3)
{
   ref1 = 47;
   ref2 = -14;
   ref3 = 30000;
}

Then call the function:

   int val1 = 4;
   int val2 = 5;
   int val3 = 98;

   fun(val1, val2, val3);

Print val1, val2, and val3 to see what they contain after the function ends.

Ok, that makes sense. Except for your use of void. Is this because the function is not returning a value but addressing it that you use void anyways?
Thanks

Is this because the function is not returning a value

Yes.

but addressing it that you use void anyways?

I don't understand this part of the question. Every function needs to have a return type. That type can be void if the function has nothing to return.

I have tried to make it a function, but I guess because I don't know what exactly everything in the function is doing, I must have made a mistake. The code is below.

//These are the Libraries that MUST be included
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <Servo.h>
#include <Stepper.h>

//These are the pin definitions
#define redpin 9
#define greenpin 10
#define bluepin 11
#define commonAnode true

//These are the positions
int posMetal = 45;
int posPlastic = 90;
int posWood = 135;

//Servo and Stepper
Stepper rotStep = Stepper(32, 4, 5, 6, 7);
Servo posServo;

//This is the Color sensor setup
byte gammatable[256];
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_2_4MS, TCS34725_GAIN_4X);

//This is the function for reading color
void readcolor(uint16_t &red, uint16_t &green, uint16_t &blue, uint16_t &clear)
{
  tcs.getRawData(&red, &green, &blue, &clear);

  tcs.setInterrupt(true);
  
  Serial.print("C:\t"); Serial.print(clear);
  Serial.print("\tR:\t"); Serial.print(red);
  Serial.print("\tG:\t"); Serial.print(green);
  Serial.print("\tB:\t"); Serial.print(blue);

  // Figure out some basic hex code for visualization
  uint32_t sum = clear;
  float r, g, b;
  r = red; r /= sum;
  g = green; g /= sum;
  b = blue; b /= sum;
  r *= 256; g *= 256; b *= 256;
}




void setup() {
  Serial.begin(9600);
  Serial.println("Recycling Sorting Machine!");

  //This is the setup for the color sensor
  if (tcs.begin()) {
    Serial.println("Found Sensor");
  } else {
    Serial.println("No sensor found...Check your connections");
    while (1);
  }
  //This sets up the RGB LED outputs.
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  //Speet for stepper motor
  rotStep.setSpeed(20);
  //Setup for servo
  posServo.attach(3);

  //This is the gamma table
  for (int i=0; i<256; i++) {
    float x = i;
    x /= 255;
    x = pow(x, 2.5);
    x *= 255;

    if (commonAnode) {
      gammatable[i] = 255 - x;
    } else {
      gammatable[i] = x;
    }
    //Gamma table end
  }
}

void loop() 
{
  rotStep.step(8); //steps the motor 90 degrees
  delay(200); //Gives the program time to drop ball before moving on
  //BELOW IS THE COLOR SENSING AND STORAGE
  uint16_t clear, red, green, blue;

  tcs.setInterrupt(false);// turn on LED

  delay(60);  // takes 50ms to read 
  readcolor(&red, &green, &blue, &clear);
  /*
  tcs.getRawData(&red, &green, &blue, &clear);

  tcs.setInterrupt(true);
  
  Serial.print("C:\t"); Serial.print(clear);
  Serial.print("\tR:\t"); Serial.print(red);
  Serial.print("\tG:\t"); Serial.print(green);
  Serial.print("\tB:\t"); Serial.print(blue);

  // Figure out some basic hex code for visualization
  uint32_t sum = clear;
  float r, g, b;
  r = red; r /= sum;
  g = green; g /= sum;
  b = blue; b /= sum;
  r *= 256; g *= 256; b *= 256;
  */
  //Now It will take that data and compare
  if (1 == 1)//replace with if it matches criteria
  {
    posServo.write(posMetal);
  }
  if (1 == 1) //replace with critea for wood
  {
    posServo.write(posWood);
  }
  if (1 == 1) //replace with plastic
  {
    posServo.write(posPlastic);
  }
}
/*
void readcolor(&red, &green, &blue, &clear)
{
  tcs.getRawData(&red, &green, &blue, &clear);

  tcs.setInterrupt(true);
  
  Serial.print("C:\t"); Serial.print(clear);
  Serial.print("\tR:\t"); Serial.print(red);
  Serial.print("\tG:\t"); Serial.print(green);
  Serial.print("\tB:\t"); Serial.print(blue);

  // Figure out some basic hex code for visualization
  uint32_t sum = clear;
  float r, g, b;
  r = red; r /= sum;
  g = green; g /= sum;
  b = blue; b /= sum;
  r *= 256; g *= 256; b *= 256;
}
*/

On the line that calls the function

readcolor(&red, &green, &blue, &clear);

I get an error that says
invalid initialization of non-const reference of type 'uint16_t& {aka unsigned int&}' from an rvalue of type 'uint16_t* {aka unsigned int*}'

Which I assume means that the data types are inconsistent, but what have I done wrong?
Thanks.

You defined the function to take a reference. You do not then use the address of operator to get the address to pass.

Change your call to

   readColor(red, green, blue, clear);

PaulS:
You defined the function to take a reference. You do not then use the address of operator to get the address to pass.

Change your call to

   readColor(red, green, blue, clear);

Ok. So, what I am getting is that I used &red instead of red, which is adressing the adress instead of the variable?

When I change that, it does compile. Thank you for all the help and explaining everything.