/*
* MP_Final.c
*/
#include <avr/io.h>
#include <util/delay.h>
#include "lcd.h"
/* PIN-OUTS */
#define S0 0
#define S1 1
#define S2 6
#define S3 7
#define out 2
/* MACRO FUNC */
#define buzz() {\
DDRB |= (1<<4); PORTB |= (1<<4);\
_delay_ms(250);\
PORTB &= (~(1<<4));\
}
/* DECLARE FUNCTIONS */
void detectColor();
uint16_t pulseIn();
uint16_t colorRead(int color);
void taosMode(int mode);
void TCS3200setup();
//static uint16_t R, B, G;
int main(void)
{
/* PORTS */
DDRB |= (1<<3)|(1<<5);
DDRD = 0x0F;
/* SENSOR INITIALIZATION */
TCS3200setup();
/* LCD */
InitLCD(0);
//LCD(0,0,"ON");
/* VARIABLES */
int count = 0, i =0, old = 0, new = 0, flag = 1;
uint8_t a = PIND & 0xE0;
/* CALIBERATION SEQUENCE */
/*
uint16_t W = colorRead(0);
uint16_t R = colorRead(1);
uint16_t B = colorRead(2);
uint16_t G = colorRead(3);
*/
/* MOTOR INITIALIZATION */
TCCR0 = (1<<WGM01)|(1<<WGM00)|(1<<COM01)|(1<<CS01)|(1<<CS00); //Play with COM00; prescalar = 64
while(1)
{
flag = 1;
buzz();
old = new;
uint16_t white = colorRead(0);
uint16_t red = colorRead(1);
uint16_t blue = colorRead(2);
uint16_t green = colorRead(3);
LCDvar(0,0,red,3);
LCDvar(5,0,blue,3);
LCDvar(9,0,green,3);
LCDvar(13,0,white,3);
/*
r 1
g 2
b 3
*/
/*
if((red>=48)&&(red<=60)&&(blue>=50)&&(blue<=60)&&(green>=30)&&(green<=40)&&(white>=135)&&(white<=182))
{
LCD(0,1,"__NIL_");
flag = 0;
}
else if((red>=45)&&(red<=65)&&(blue<=40)&&(green<=33)) //red
{
//LCDClear();
LCD(0,1,"RED___");
new = 1;
}
else if((red<=54)&&(blue>=41)&&(green<=33)) //blue
{
//LCDClear();
LCD(0,1,"_BLUE_");
new = 3;
}
else if(green>=33) //(red>=65)&&(red<=74)&&(blue<=45)&&(blue>=38)) //yellow
{
//LCDClear();
LCD(0,1,"GREEN");
new = 2;
}
*/
if((white>=114)&&(blue>=43)&&(red>=54))
{
LCD(0,1,"__NIL_");
flag = 0;
}
else if(red>=43) //red
{
LCD(0,1,"RED___");
new = 1;
}
else if(blue>=41) //blue
{
LCD(0,1,"_BLUE_");
new = 3;
}
else if(green>=33) //(red>=65)&&(red<=74)&&(blue<=45)&&(blue>=38)) //yellow
{
LCD(0,1,"GREEN");
new = 2;
}
for (i = 0; i<=2; i++)
{
_delay_ms(6000);
}
/* CD_ROM motor */
if (old-new >= 0)
{
count = old - new;
}
else
{
count = new - old;
}
while(count >= 1)
{
if(old-new > 0)
{
TCCR0 |= (1<<COM00);
PORTB &= (~(1<<5));
OCR0 = 125;
if((a==192)||(a==160))
{
--count;
}
else if(a == 96)
{
--count;
}
else
{}
_delay_ms(750);
OCR0 = 255;
PORTB &= (~(1<<5));
}
else if(new-old > 0)
{
TCCR0 &= (~(1<<COM00));
PORTB |= (1<<5);
OCR0 = 125;
if((a==192)||(a==160))
{
--count;
}
else if(a == 96)
{
--count;
}
else
{}
_delay_ms(750);
OCR0 = 0;
PORTB &= (~(1<<5));
}
else
{
DDRB &= (~(1<<3))|(~(1<<5));
TCCR0 &= (~(1<<CS01))|(~(1<<CS00));
}
}
count = 0;
/* FLAP */
if (flag == 1)
{
PORTD = 0x01;
for (i = 0; i<=3; i++)
{
_delay_ms(1500);
}
PORTD = 0x02;
for (i = 0; i<=2; i++)
{
_delay_ms(2000);
}
PORTD = 0x00;
}
/* SAMPLING DELAY */
for (i = 0; i<=2; i++)
{
_delay_ms(6000);
}
}
}
/* FUNCTION DEFINITIONS */
/* Pulse reading from sensor variable */
uint16_t readPulse;
/*
This section will return the pulseIn reading of the selected color.
It will turn on the sensor at the start taosMode(1),
and it will power off the sensor at the end taosMode(0)
color codes: 0=white, 1=red, 2=blue, 3=green
if LEDstate is 0, LED will be off. 1 and the LED will be on.
taosOutPin is the ouput pin of the TCS3200.
*/
uint16_t pulseIn()
{
//If the function is entered when the level on OUT line was low
//Then wait for it to become high.
if(!(PINB & (1<<out)))
{
while(!(PINB & (1<<out))); //Wait for rising edge
}
while(PINB & (1<<out)); //Wait for falling edge
TCNT1=0x0000;//Reset Counter
TCCR1B=(1<<CS10); //Prescaller = F_CPU/1 (Start Counting)
while(!(PINB & (1<<out))); //Wait for rising edge
//Stop Timer
TCCR1B=0x00;
return (8000/TCNT1);
}
uint16_t colorRead(int color){
//turn on sensor and use highest frequency/sensitivity setting
taosMode(1);
//setting for a delay to let the sensor sit for a moment before taking a reading.
//set the S2 and S3 pins to select the color to be sensed
if(color == 0){//white
clr(PORTB,S3); //S3
set(PORTB,S2); //S2
// Serial.print(" w");
}
else if(color == 1){//red
clr(PORTB,S3); //S3
clr(PORTB,S2); //S2
// Serial.print(" r");
}
else if(color == 2){//blue
set(PORTB,S3); //S3
clr(PORTB,S2); //S2
// Serial.print(" b");
}
else if(color == 3){//green
set(PORTB,S3); //S3
set(PORTB,S2); //S2
// Serial.print(" g");
}
// wait a bit for LEDs to actually turn on, as directed by sensorDelay var
_delay_ms(100);
// now take a measurement from the sensor, timing a low pulse on the sensor's "out" pin
readPulse = pulseIn();
//if the pulseIn times out, it returns 0 and that throws off numbers. just cap it at 80k if it happens
if(readPulse < 10){
readPulse = 16000;
}
//turn off color sensor and LEDs to save power
taosMode(0);
// return the pulse value back to whatever called for it...
return readPulse;
}
/*Operation modes controlled by S0 and S1 pins. setting mode to zero = low power mode.*/
void taosMode(int mode){
if(mode == 0){
//power OFF mode- LED off and both channels "low"
clr(PORTB,S0); //S0
clr(PORTB,S1); //S1
// Serial.println("mOFFm");
}else if(mode == 1){
//this will put in 1:1, highest sensitivity
set(PORTB,S0); //S0
set(PORTB,S1); //S1
// Serial.println("m1:1m");
}else if(mode == 2){
//this will put in 1:5
set(PORTB,S0); //S0
clr(PORTB,S1); //S1
//Serial.println("m1:5m");
}else if(mode == 3){
//this will put in 1:50
clr(PORTB,S0); //S0
set(PORTB,S1); //S1
//Serial.println("m1:50m");
}
}
void TCS3200setup(){
//color mode selection
set(DDRB,S2); //S2 pinE
set(DDRB,S3); //s3 pinF
//color response pin (only actual input from taos)
clr(DDRB,out); //taosOutPin pinC
//communication freq (sensitivity) selection
set(DDRB,S0); //S0 pinB
set(DDRB,S1); //S1 pinA
}
................error after compiling
ketch_mar15a.ino: In function 'int main()':
sketch_mar15a:46: error: 'InitLCD' was not declared in this scope
sketch_mar15a:50: error: expected unqualified-id before 'new'
sketch_mar15a:62: error: 'TCCR0' was not declared in this scope
sketch_mar15a:62: error: 'COM01' was not declared in this scope
sketch_mar15a:66: error: 'flag' was not declared in this scope
sketch_mar15a:69: error: expected type-specifier before ';' token
sketch_mar15a:76: error: 'LCDvar' was not declared in this scope
sketch_mar15a:115: error: 'LCD' was not declared in this scope
sketch_mar15a:120: error: 'LCD' was not declared in this scope
sketch_mar15a:121: error: expected type-specifier before '=' token
sketch_mar15a:125: error: 'LCD' was not declared in this scope
