Vytvorená stránka „ == Software for Color Sensors == Demo program: <source lang="c"> /*==================================================== / Connect ColorPAL SIG signal to Arduino pin ...“
/*====================================================/ Connect ColorPAL SIG signal to Arduino pin 2 and 3/ Baud Rate = 9600 kbps/ Works with Arduino 0.20, not with 1.00 and above?/====================================================*/#include<SoftwareSerial.h>SoftwareSerialColorPAL(2,3);// rx = 2, tx = 3intred,grn,blu;intgotcolor=0;intletter;charcolorByte[9];voidsetup(){// for colordemo 9600,Serial.begin(9600);// Start communication with serial port read valueColorPAL.begin(4800);// Send signal to led to read valuepinMode(2,INPUT);// serial pin out from color palpinMode(3,INPUT);// from same serial pin, signal pulls up, sends, pulls down, readsdigitalWrite(2,HIGH);// Enable the pull-up resistordigitalWrite(3,HIGH);// Enable the pull-up resistorpinMode(2,OUTPUT);// send signal outpinMode(3,OUTPUT);digitalWrite(2,LOW);// turn pin off so pin 3 can go highdigitalWrite(3,LOW);pinMode(2,INPUT);// Input signal to printpinMode(3,INPUT);delay(20);while(digitalRead(2)!=HIGH||digitalRead(3)!=HIGH){delay(50);}pinMode(2,OUTPUT);pinMode(3,OUTPUT);digitalWrite(2,LOW);digitalWrite(3,LOW);delay(100);// spec is 80, but not all ColorPAL units work with 80pinMode(2,INPUT);pinMode(3,OUTPUT);delay(100);delay(20);ColorPAL.begin(4800);ColorPAL.print("= (00 $ m) !");// set up loop to continuously send color datapinMode(3,INPUT);}// This oscillates back and forth on one wire to turn off led, send signal,// turn on led, read signal. very fast strobe read - arduino is not capable of// one wire signal communication over digital ports, so this is a way around// that over 2 wires communicating with 1 pin on the sensor.//---------------------------------voidloop(){readColor();gotcolor=0;delay(100);}/* End of loop() *//* ***************************************** *//* Function readColor() *//* Reads ColorPAL, putting results in the *//* red, grn, blu variables *//* ***************************************** */voidreadColor(){charrByte[9];chardummy[4];// delay(20);// ColorPAL.begin(4800);// ColorPAL.print("= (00 $ m) !"); // set up loop to continuously send color datapinMode(3,INPUT);gotcolor=0;while(gotcolor==0){rByte[0]=ColorPAL.read();if(rByte[0]=='$'){gotcolor=1;for(inti=0;i<9;i++){rByte[i]=ColorPAL.read();}Serial.print("R0");Serial.print(rByte[0]);Serial.print(rByte[1]);Serial.print(rByte[2]);Serial.print(" G0");Serial.print(rByte[3]);Serial.print(rByte[4]);Serial.print(rByte[5]);Serial.print(" B0");Serial.print(rByte[6]);Serial.print(rByte[7]);Serial.print(rByte[8]);Serial.println();}}}/* End of function readColor() */