求助大神-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9447|回复: 8

求助大神

[复制链接]
发表于 2016-11-24 20:00 | 显示全部楼层 |阅读模式
这样的错误代表什么意思

'LEFT'的值在常量表达式中不可用
QQ截图20161124195854.jpg
发表于 2016-11-24 21:00 | 显示全部楼层
应该是你代码前面少了 #define FRONT xxx  这样的定义

最好贴完整代码上来
 楼主| 发表于 2016-11-24 22:18 | 显示全部楼层
好的,谢谢,
下面是完整代码,请大神救急
int L_IN1 = 7;  // IN1:0 IN2:1 正转
int L_IN2 = 5;  // IN1:1 IN2:0 反转
int L_IN3 = 4;
int L_IN4 = 2;
int L_ENA = 6;   //PWM输出口
int L_ENB = 3;   //PWM输出口

int R_ENA = 13;   //PWM输出口
int R_IN1 = 12;  // IN1:0 IN2:1 正转
int R_IN2 = 11;  // IN1:1 IN2:0 反转
int R_IN3 = 10;
int R_IN4 = 9;
int R_ENB = 8;   //PWM输出口

int MOTO_LF_EN = 22;
int MOTO_RF_EN = 23;
int MOTO_LB_EN = 24;
int MOTO_RB_EN = 25;

int FRONT;
int BACK;
int LEFT;
int RIGHT;

int LF;
int RF;
int LB;
int RB;

void setup() {
  // put your setup code here, to run once:
  pinMode(L_IN1, OUTPUT);
  pinMode(L_IN2, OUTPUT);
  pinMode(L_IN3, OUTPUT);
  pinMode(L_IN4, OUTPUT);
  pinMode(L_ENA, OUTPUT);
  pinMode(L_ENB, OUTPUT);

  pinMode(R_IN1, OUTPUT);
  pinMode(R_IN2, OUTPUT);
  pinMode(R_IN3, OUTPUT);
  pinMode(R_IN4, OUTPUT);
  pinMode(R_ENA, OUTPUT);
  pinMode(R_ENB, OUTPUT);

  pinMode(MOTO_LF_EN, INPUT);
  pinMode(MOTO_RF_EN, INPUT);
  pinMode(MOTO_LB_EN, INPUT);
  pinMode(MOTO_RB_EN, INPUT);
}

void forward()   //正转
{
  digitalWrite(L_ENA, HIGH);
  digitalWrite(L_ENB, HIGH);
  //analogWrite(ENA, 220);
  //analogWrite(ENB, 220);
  digitalWrite(R_ENA, HIGH);
  digitalWrite(R_ENB, HIGH);
  //analogWrite(ENA, 220);
  //analogWrite(ENB, 220);

  digitalWrite(L_IN1, LOW);
  digitalWrite(L_IN2, HIGH);
  digitalWrite(L_IN3, HIGH);
  digitalWrite(L_IN4, LOW);

  digitalWrite(R_IN1, HIGH);
  digitalWrite(R_IN2, LOW);
  digitalWrite(R_IN3, LOW);
  digitalWrite(R_IN4, HIGH);
}

void reverse()   //反转
{
  digitalWrite(L_ENA, HIGH);
  digitalWrite(L_ENB, HIGH);
  //analogWrite(ENA, 220);
  //analogWrite(ENB, 220);
  digitalWrite(R_ENA, HIGH);
  digitalWrite(R_ENB, HIGH);
  //analogWrite(ENA, 220);
  //analogWrite(ENB, 220);

  digitalWrite(L_IN1, HIGH);
  digitalWrite(L_IN2, LOW);
  digitalWrite(L_IN3, LOW);
  digitalWrite(L_IN4, HIGH);

  digitalWrite(R_IN1, LOW);
  digitalWrite(R_IN2, HIGH);
  digitalWrite(R_IN3, HIGH);
  digitalWrite(R_IN4, LOW);
}

void turn_left()
{
  digitalWrite(L_ENA, HIGH);
  digitalWrite(L_ENB, HIGH);
  //analogWrite(ENA, 220);
  //analogWrite(ENB, 220);
  digitalWrite(R_ENA, HIGH);
  digitalWrite(R_ENB, HIGH);
  //analogWrite(ENA, 220);
  //analogWrite(ENB, 220);

  digitalWrite(L_IN1, HIGH);
  digitalWrite(L_IN2, LOW);
  digitalWrite(L_IN3, LOW);
  digitalWrite(L_IN4, HIGH);

  digitalWrite(R_IN1, HIGH);
  digitalWrite(R_IN2, LOW);
  digitalWrite(R_IN3, LOW);
  digitalWrite(R_IN4, HIGH);
}
void turn_right()
{
  digitalWrite(L_ENA, HIGH);
  digitalWrite(L_ENB, HIGH);
  //analogWrite(ENA, 220);
  //analogWrite(ENB, 220);
  digitalWrite(R_ENA, HIGH);
  digitalWrite(R_ENB, HIGH);
  //analogWrite(ENA, 220);
  //analogWrite(ENB, 220);

  digitalWrite(L_IN1, LOW);
  digitalWrite(L_IN2, HIGH);
  digitalWrite(L_IN3, HIGH);
  digitalWrite(L_IN4, LOW);

  digitalWrite(R_IN1, LOW);
  digitalWrite(R_IN2, HIGH);
  digitalWrite(R_IN3, HIGH);
  digitalWrite(R_IN4, LOW);
}

void stop_car()      //电机自由停止状态
{
  digitalWrite(L_ENA, LOW);
  digitalWrite(L_ENB, LOW);
  //analogWrite(ENA, 220);
  //analogWrite(ENB, 220);
  digitalWrite(R_ENA, LOW);
  digitalWrite(R_ENB, LOW);
  //analogWrite(ENA, 220);
  //analogWrite(ENB, 220);

  digitalWrite(L_IN1, LOW);
  digitalWrite(L_IN2, LOW);
  digitalWrite(L_IN3, LOW);
  digitalWrite(L_IN4, LOW);

  digitalWrite(R_IN1, LOW);
  digitalWrite(R_IN2, LOW);
  digitalWrite(R_IN3, LOW);
  digitalWrite(R_IN4, LOW);

}

void brake()     //电机制动
{
  digitalWrite(L_ENA, HIGH);
  digitalWrite(L_ENB, HIGH);
  //analogWrite(ENA, 220);
  //analogWrite(ENB, 220);
  digitalWrite(R_ENA, HIGH);
  digitalWrite(R_ENB, HIGH);
  //analogWrite(ENA, 220);
  //analogWrite(ENB, 220);

  digitalWrite(L_IN1, LOW);
  digitalWrite(L_IN2, LOW);
  digitalWrite(L_IN3, LOW);
  digitalWrite(L_IN4, LOW);

  digitalWrite(R_IN1, LOW);
  digitalWrite(R_IN2, LOW);
  digitalWrite(R_IN3, LOW);
  digitalWrite(R_IN4, LOW);
}








int data_reading()    //读取 SLAMWARE breakout 3 板子 电机控制板的信号
{
  LF = digitalRead(MOTO_LF_EN); //电机的使能信号,低电平有效
  RF = digitalRead(MOTO_RF_EN);
  LB = digitalRead(MOTO_LB_EN);
  RB = digitalRead(MOTO_RB_EN);

  if ((LF == LOW) && (RF == LOW) && (LB == HIGH) && (RB == HIGH)) //前进
  {
    return FRONT;
  }

  else if ((LF == LOW) && (RF == HIGH) && (LB == HIGH) && (RB == LOW)) //右转
  {
    return RIGHT;
  }

  else if ((LF == HIGH) && (RF == LOW) && (LB == LOW) && (RB == HIGH)) //左转
  {
    return LEFT;
  }

  else if ((LF == HIGH) && (RF == HIGH) && (LB == LOW) && (RB == LOW)) //后退
  {
    return BACK;
  }

}






void loop() {
  // put your main code here, to run repeatedly:

  switch (data_reading())
  {
    case FRONT:
      forward();
      break;

    case BACK:
      reverse();
      break;

    case LEFT:
      turn_left();
      break;

    case RIGHT:
      turn_right();
      break;

    //   case   stop_car:
    //    car_stop();
    //    break;


    default:
      brake();
      break;
  }

}
发表于 2016-11-24 22:18 | 显示全部楼层
他的意思是说front不是常量,常量是像1,2,3,4这种
 楼主| 发表于 2016-11-24 22:22 | 显示全部楼层
所以,怎么改就好了呢,原谅小白一枚
发表于 2016-11-25 08:18 | 显示全部楼层
把  case 改成if 语句应该就可以了
发表于 2016-11-25 09:03 | 显示全部楼层
问题就出现在这段,你只是定义了形参,却没有给它赋值,所以在case中找不到对应的值,C:\Users\PC\Desktop\2$TM3HUL$`%5DGYV7QJCGT_3X.png
发表于 2016-11-25 09:04 | 显示全部楼层
问题就出现在这段,你只是定义了形参,却没有给它赋值,所以在case中找不到对应的值,
int FRONT;
int BACK;
int LEFT;
int RIGHT;
 楼主| 发表于 2016-11-25 15:58 | 显示全部楼层
jackten 发表于 2016-11-25 09:04
问题就出现在这段,你只是定义了形参,却没有给它赋值,所以在case中找不到对应的值,
int FRONT;
int BACK ...

谢谢大神,已经成功跑通
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-27 22:27 , Processed in 0.179622 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表