Project with TCS3200 colour sensor

Hey there

So I’m doing a project where I have some coloured discs and I’m sorting them on colour with the TCS3200 colour sensor, 2 servo’s and a custom built casing.

So I got a question is there a possibility, I only have a pushbutton so I want to start the code when I push the button once and want to stop the code when I push again, I would like to stop the code with looping, so at the end I would like to have a START/EMERGENCY button.

Already tried the Arduino - Switch but I dont know where to put my code. Or how to get it working. My code is included as well.

#include <Servo.h>

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

Servo bovservo;
Servo benservo;

int frequentie=0;
int kleur=0;
int leeskleur()
  {
    digitalWrite(S2,LOW);
    digitalWrite(S3,LOW);
    frequentie=pulseIn(sensorout, LOW);
    int R=frequentie;
    Serial.print("R=");
    Serial.print(frequentie);
    Serial.print(" ");
    delay(50);

    digitalWrite(S2,HIGH);
    digitalWrite(S3,HIGH);
    frequentie=pulseIn(sensorout,LOW);
    int G=frequentie;
    Serial.print("G=");
    Serial.print(frequentie);
    Serial.print(" ");
    delay(50);

    digitalWrite(S2,LOW);
    digitalWrite(S3,HIGH);
    frequentie=pulseIn(sensorout,LOW);
    int B=frequentie;
    Serial.print("B=");
    Serial.print(frequentie);
    Serial.print(" ");
    delay(50);

    if(R< 45 & R>32 & G<65 &G>55)
    {
      kleur=1;//Rood
    }
    if(R<53 & R>40 & G<53 & G>40)
    {
      kleur=2;//Groen
    }
    if(R<38 & R>24 & G<44 & G>30)
    {
      kleur=3;//Geel
    }
    return kleur;
  }
int drukknop=9;

void setup()
{
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorout, INPUT);
  pinMode(drukknop, INPUT);

  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);

  bovservo.attach(7);
  benservo.attach(8);

  Serial.begin(9600);
}

void loop ()
    {
    bovservo.write(106);
    delay(500);

    for(int i=106; i>65; i--)
    {
      bovservo.write(i);
      delay(10);
    }
    delay(2000);

    kleur=leeskleur();
    delay(10);

    switch (kleur)
    {
      case 1:
      benservo.write(50);
      break;

      case 2:
      benservo.write(125);
      break;
    
    case 3:
    benservo.write(200);
    break;

    case 0:
    break;

  }
  delay(300);

  for(int i=65;i>29; i--)
  {
  bovservo.write(i);
  delay(30);
  }
  delay(200);

  for(int i=29; i<106; i++)
  {
    bovservo.write(i);
    delay(15);
  }
  kleur=0;
}

Greetings
Brent

Basic state machine. Have button toggle a bool (let's call it running) and then run the colour routine based on that.

bool running;
void loop() {
  if (buttonPressed()) running = !running; // toggle bool value based on button press.
  if (running) {
    sortColours();
  }
}

Thanks for the fast response, trying it out atm Do I put the input of the button on digital right?

Start by writing button code that switches on and off an LED (use the internal one for convenience).

You need to do state change detection (you want to toggle on the button change: from unpressed to pressed), and do some debouncing to prevent multiple triggers on a single press.

Will do, thanks!

Somehow I get an error "'buttonPressed' cannot be used as a function

int button=9;
int led=13;
int buttonPressed=0;
bool running;

void setup ()
{
pinMode(drukknop, INPUT);
Serial.begin(9600);
}

void loop() {
  buttonPressed=digitalRead(button);
  if (buttonPressed()) running = !running; // toggle bool value based on button press.
  if (running) 
  {
    digitalWrite(led, HIGH);
  }

That's because you declared it as variable, not as function.

Also this code will toggle all the time while the button is pressed. No state detection (i.e. record state, compare to new state).

Take a look at this tutorial.