颜色传感器和舵机的问题
我想通过颜色传感器 控制舵机角度 颜色传感器使用如下代码
http://www.ncnynl.com/archives/201606/101.html
我在上面代码中 ISR(TIMER2_OVF_vect)方法中加入了
ISR(TIMER2_OVF_vect)//the timer 2, 10ms interrupt overflow again. Internal overflow interrupt executive function
{
TCNT2=50000;
flag++;
if(flag==1){
countR=counter;
Serial.print("red=");
Serial.println(countR,DEC);
digitalWrite(s2,HIGH);
digitalWrite(s3,HIGH);
}else if(flag==2){
countG=counter;
Serial.print("green=");
Serial.println(countG,DEC);
digitalWrite(s2,LOW);
digitalWrite(s3,HIGH);
if(countG>190){
Serial.println("------hui------");
}else if(countG >140 && countG< 190 && countR<150){
Serial.println("@@@@@@@@@hong@@@@@@@@@@@");
myservo.write(60);
}else if(countG <130){
Serial.println("##########huang##########");
myservo.write(120);
}
}else if(flag==3){
countB=counter;
Serial.print("blue=");
Serial.println(countB,DEC);
Serial.println("\n");
digitalWrite(s2,LOW);
digitalWrite(s3,LOW);
}else if(flag==4){
flag=0;
}
counter=0;
}
这样的方法来控制舵机的角度
目前的问题是:ISR(TIMER2_OVF_vect) 这个定时器每10毫秒调用一次 检测频率太快 刚刚检测到颜色 舵机动了一下 就又回去了 我的想法是延迟定时器的时间 让ISR(TIMER2_OVF_vect) 每一秒执行一次 或者每5秒执行一次 这样就能给舵机保持角度的时间,或者检测到值的时候让舵机保持原来的角度 请教各位大神应该怎么做啊?
所有程序代码
#include <Servo.h>
Servo myservo;
int s0=3,s1=4,s2=5,s3=6;
int out=2;
int flag=0;
int dj = 0;
int cou =0;
byte counter=0;
byte countR=0,countG=0,countB=0;
void setup()
{
Serial.begin(115200);
pinMode(s0,OUTPUT);
pinMode(s1,OUTPUT);
pinMode(s2,OUTPUT);
pinMode(s3,OUTPUT);
pinMode(out, INPUT);
myservo.attach(9);
}
void TCS(){
flag=0;
digitalWrite(s1,HIGH);
digitalWrite(s0,HIGH);
digitalWrite(s2,LOW);
digitalWrite(s3,LOW);
attachInterrupt(0, ISR_INTO, CHANGE);
timer0_init();
}
void ISR_INTO()
{
counter++;
// noInterrupts();
// delay(4000);
//interrupts();
}
void timer0_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){
countR=counter;
Serial.print("red=");
Serial.println(countR,DEC);
digitalWrite(s2,HIGH);
digitalWrite(s3,HIGH);
}else if(flag==2){
countG=counter;
Serial.print("green=");
Serial.println(countG,DEC);
digitalWrite(s2,LOW);
digitalWrite(s3,HIGH);
if(countG>190){
/*
Serial.println("------hui------");
if(dj==1){
myservo.write(60);
}if(dj==0){
myservo.write(120);
}
*/
return;
}else if(countG >140 && countG< 190 && countG<150){
Serial.println("@@@@@@@@@hong@@@@@@@@@@@");
myservo.write(60);
dj=1;
for(int i=0;i<1024;i++){
myservo.write(60);
}
cou=0;
return;
}else if(countG <130){
Serial.println("##########huang##########");
myservo.write(120);
dj=0;
}
/*
else if(countR<90){
Serial.print("-----");
return;
}
*/
}
else if(flag==3)
{
countB=counter;
Serial.print("blue=");
Serial.println(countB,DEC);
Serial.println("\n");
digitalWrite(s2,LOW);
digitalWrite(s3,LOW);
}
else if(flag==4)
{
flag=0;
}
counter=0;
}
void loop()
{
TCS();
//noInterrupts();
delay(2000);
Serial.println(countR);
Serial.println("@@@@@@@@@@@@@@@@@");
Serial.println(countG);
Serial.println("$$$$$$$$$$$$$$$$$$");
Serial.println(countB);
Serial.println("%%%%%%%%%%%%%%%%");
// interrupts();
delay(2000);
Serial.println("####################");
}
http://www.ncnynl.com/archives/201606/101.html
我在上面代码中 ISR(TIMER2_OVF_vect)方法中加入了
ISR(TIMER2_OVF_vect)//the timer 2, 10ms interrupt overflow again. Internal overflow interrupt executive function
{
TCNT2=50000;
flag++;
if(flag==1){
countR=counter;
Serial.print("red=");
Serial.println(countR,DEC);
digitalWrite(s2,HIGH);
digitalWrite(s3,HIGH);
}else if(flag==2){
countG=counter;
Serial.print("green=");
Serial.println(countG,DEC);
digitalWrite(s2,LOW);
digitalWrite(s3,HIGH);
if(countG>190){
Serial.println("------hui------");
}else if(countG >140 && countG< 190 && countR<150){
Serial.println("@@@@@@@@@hong@@@@@@@@@@@");
myservo.write(60);
}else if(countG <130){
Serial.println("##########huang##########");
myservo.write(120);
}
}else if(flag==3){
countB=counter;
Serial.print("blue=");
Serial.println(countB,DEC);
Serial.println("\n");
digitalWrite(s2,LOW);
digitalWrite(s3,LOW);
}else if(flag==4){
flag=0;
}
counter=0;
}
这样的方法来控制舵机的角度
目前的问题是:ISR(TIMER2_OVF_vect) 这个定时器每10毫秒调用一次 检测频率太快 刚刚检测到颜色 舵机动了一下 就又回去了 我的想法是延迟定时器的时间 让ISR(TIMER2_OVF_vect) 每一秒执行一次 或者每5秒执行一次 这样就能给舵机保持角度的时间,或者检测到值的时候让舵机保持原来的角度 请教各位大神应该怎么做啊?
所有程序代码
#include <Servo.h>
Servo myservo;
int s0=3,s1=4,s2=5,s3=6;
int out=2;
int flag=0;
int dj = 0;
int cou =0;
byte counter=0;
byte countR=0,countG=0,countB=0;
void setup()
{
Serial.begin(115200);
pinMode(s0,OUTPUT);
pinMode(s1,OUTPUT);
pinMode(s2,OUTPUT);
pinMode(s3,OUTPUT);
pinMode(out, INPUT);
myservo.attach(9);
}
void TCS(){
flag=0;
digitalWrite(s1,HIGH);
digitalWrite(s0,HIGH);
digitalWrite(s2,LOW);
digitalWrite(s3,LOW);
attachInterrupt(0, ISR_INTO, CHANGE);
timer0_init();
}
void ISR_INTO()
{
counter++;
// noInterrupts();
// delay(4000);
//interrupts();
}
void timer0_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){
countR=counter;
Serial.print("red=");
Serial.println(countR,DEC);
digitalWrite(s2,HIGH);
digitalWrite(s3,HIGH);
}else if(flag==2){
countG=counter;
Serial.print("green=");
Serial.println(countG,DEC);
digitalWrite(s2,LOW);
digitalWrite(s3,HIGH);
if(countG>190){
/*
Serial.println("------hui------");
if(dj==1){
myservo.write(60);
}if(dj==0){
myservo.write(120);
}
*/
return;
}else if(countG >140 && countG< 190 && countG<150){
Serial.println("@@@@@@@@@hong@@@@@@@@@@@");
myservo.write(60);
dj=1;
for(int i=0;i<1024;i++){
myservo.write(60);
}
cou=0;
return;
}else if(countG <130){
Serial.println("##########huang##########");
myservo.write(120);
dj=0;
}
/*
else if(countR<90){
Serial.print("-----");
return;
}
*/
}
else if(flag==3)
{
countB=counter;
Serial.print("blue=");
Serial.println(countB,DEC);
Serial.println("\n");
digitalWrite(s2,LOW);
digitalWrite(s3,LOW);
}
else if(flag==4)
{
flag=0;
}
counter=0;
}
void loop()
{
TCS();
//noInterrupts();
delay(2000);
Serial.println(countR);
Serial.println("@@@@@@@@@@@@@@@@@");
Serial.println(countG);
Serial.println("$$$$$$$$$$$$$$$$$$");
Serial.println(countB);
Serial.println("%%%%%%%%%%%%%%%%");
// interrupts();
delay(2000);
Serial.println("####################");
}
1 个回复
ncnynl
赞同来自: