Arduino + Color Sensor + Lock mechanism

Hello Everyone!

I am currently working on building a lock that will open to only certain colors. My friend directed to check out using an Arduino connected to a color sensor and to a lock mechanism. I did some research and I wasn't able to confirm that this would be possible....

  1. would I be able to do this with an Arduino + color sensor + a lock mechanism? if so, what would I need to buy to make this happen?
  2. I am a bit rusty on coding and I'm not really sure what software I would use, would this required a lot of code work? would processing 2.1 do the job?

I think thats all! Thanks for your time.

You can find color sensor and tutorials at Adafruit.

The Arduino is a microcontroller, so you need something to drive the lock. Is that an electric lock, or do you want to make that yourself ?

It is an electric lock with an electromagnet similar to this.

I have this code that works with a color sensor and a servo that you might be able to use. Note: this code uses blocking delays, so you will need to change them to use the millis function. (If need, that is)

int s0=3,s1=4,s2=5,s3=6;
int flag=0;
int counter=0;
int countR=0,countG=0,countB=0;
void setup()
{
  Serial.begin(115200);
  pinMode(s0,OUTPUT);
  pinMode(s1,OUTPUT); 
  pinMode(s2,OUTPUT);
  pinMode(s3,OUTPUT);
}
void TCS()
{
  digitalWrite(s1,HIGH);
  digitalWrite(s0,LOW);
  flag=0;
  attachInterrupt(0, ISR_INTO, CHANGE);
  timer2_init();
}
void ISR_INTO()
{
  counter++;
}
void timer2_init(void)
{
  TCCR2A=0x00;
  TCCR2B=0x07; //the clock frequency source 1024 points
  TCNT2= 100;    //10 ms overflow again
  TIMSK2 = 0x01; //allow interrupt
}
int i=0;
ISR(TIMER2_OVF_vect)//the timer 2, 10ms interrupt overflow again. Internal overflow interrupt executive function
{
  TCNT2=100;
  flag++;
  if(flag==1)
  {
    counter=0;
  }
  else if(flag==2)
  {
    digitalWrite(s2,LOW);
    digitalWrite(s3,LOW); 
    countR=counter/1.051;
    Serial.print("red=");
    Serial.println(countR,DEC);
    digitalWrite(s2,HIGH);
    digitalWrite(s3,HIGH);   
  }
  else if(flag==3)
  {
    countG=counter/1.0157;
    Serial.print("green=");
    Serial.println(countG,DEC);
    digitalWrite(s2,LOW);
    digitalWrite(s3,HIGH); 
  }
  else if(flag==4)
  {
    countB=counter/1.114;
    Serial.print("blue=");
    Serial.println(countB,DEC);
    digitalWrite(s2,LOW);
    digitalWrite(s3,LOW);
  }
  else
  {
    flag=0; 
    TIMSK2 = 0x00;
  }
  counter=0;
  delay(2);
}
void loop()
{
  delay(10);
  TCS();
  if((countR>10)||(countG>10)||(countB>10))
  {
    if((countR>countG)&&(countR>countB))
    {
      Serial.print("red");
      Serial.print("\n");
      delay(1000); // give time for the servo to move to its correct location.
    }
    else if((countG>=countR)&&(countG>countB))
    {
      Serial.print("green");
      Serial.print("\n");
      delay(1000);
    } 
    else if((countB>countG)&&(countB>countR))
    {
      Serial.print("blue");
      Serial.print("\n");
      delay(1000);
    }
  }
  else 
  {
    // Do something here otherwise
    // delay(1000);       
  }
}