Coding for Marble sorter (Deadline)

Hello all,

I am working on a project to sort three different types of marbles into three different boxes.

We are using the Adafruit TCS34725 color sensor for color.
We are using an RGB LED to verify color.
We are using a stepper motor with 4 slots, marble droped in on top, on side it is sensed, on bottom it is dropped, on other side is empty.
We are using a servo to control the redirection of the marble.

I have the necessary libraries imported and the setup code written for the color sensor and the servo, and I am waiting on an H bridge for the stepper.

I am wondering what would be the best way to go about coding this. Should I write a void() function for each of the processes, to rotate stepper, then jump to read color, then jump to switch servo, then to rotate stepper again? If I do that method, how can I transfer variables across multiple functions? Shoud I just write the code all in the loop function, then have it flow from one to the next with if statements? Could I specify variabes and colors for each and then have a simple command to see whether it fits within those properties, then jump to labels that will move it to the position?

Thanks a lot! I really appreciate any advice or examples. Below is the code so far.

//These are the Libraries that MUST be included
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <Servo.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 definition
Servo posServo;
//This is the Color sensor setup
byte gammatable[256];
Adafruit_TCS34725 tcs = Adafruit_TCS 34725(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);

  //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() {
  // put your main code here, to run repeatedly:

}

yikes ! go back to the main forum, read HOW TO USE THIS FORUM
then scroll down to #7 about the use of code tags
com back and fix you post

tyler_newcomb:
Should I write a void() function for each of the processes

Very definitely. It will make development and debugging very much easier. By the way the name of the thing is "function". The word void just tells the compiler that the function does not return a value - some functions do return values.

If you define your variables as global variables (at the top of the code before setup() ) they will be accessible everywhere in the program. However it is good practice to define variables inside a function if they are only used within that function. You can certainly pass variables from one function to another and that, also, is good practice - but it can become complicated for a newbie.

Have a look at Planning and Implementing a Program

...R

The title of this thread indicates you have a deadline. When is this due?

Pete

Thanks Robin! For processes where the color sensor reads the inputs, would it be preferable to write a function that DOES return a value? How would I do this?

Pete, prototyping was supposed to be done this week or early next week. We are going to need to focus on the physical design and building, however, since we found out that 3D printing is not an option.

How would I do this?

void someFunc() { }

doesn't return a value

int someFunc() { }

returns an int

How on earth are you doing such a project and you have not been taught this basic stuff?

Graynomad:
int someFunc() { }

returns an int

A function that returns an int must also include return nnn - something like

int someFunc() {
   int someValue = 0;
   // stuff to update someValue
   return someValue;
}

...R

I new I should have put

return x;

in there :slight_smile:

Graynomad:
I knew I should have put

And a k as well :slight_smile:

...R

Bugger. I'll leave it there for posterity.

Graynomad, I have not worked with arduino much before, but other C based programming. What I've used is very beginer oriented and not great to work with.

Thanks Robin for the help!

but other C based programming

C is C, doesn't matter what the platform is.