monitoring light degree with a sensor.

Hello, everyone, the following are my code to monitor light degree with a sensor. But when compiling , there is alway a error (in the last two line) .

‘’
sketch_dec17b:76:16: error: ‘val’ was not declared in this scope
Serial.write(val>>8);//源源不断的向上位机发送
^~~
exit status 1
‘val’ was not declared in this scope
‘’

I do know why, and the model of my arduino is arduino Nano, and processor is Atmega328p-AU.

Thank you so much.

The following are the code

#include <TimerOne.h>
char buf[128];
int buf_i=0;
long ADLen=0;
byte ADCh=0;
long Fs=1;

void setup() {
Serial.begin(115200);
//串口通信波特率最高
// put your setup code here, to run once:
}

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

}

void serialEvent(){
while(Serial.available()){
char
inChar=(char)Serial.read();
switch(inChar){
//串口命令格式,以#开始,以$结束
case’#’:
buf_i=0;
break;
case’$’:
//串口命令格式,以#开始,以$结束
buf[buf_i]=’\0’;
Operation(buf); //operation对串口命令进行解析
break;
default:
buf[buf_i]=inChar;
buf_i++;
if (buf_i>=128) buf_i=0;
break;
}
}
}

void Operation(char Command){

char s0[128];
char s1;
char data[8][32];
int i;
strcpy(s0,Command);
s1=strtok(s0,’,’);
if (s1!=NULL)strcpy(data[0],s1);
for(i=1;i<8;i++){
s1=strtok(NULL,",");
if (s1!=NULL)strcpy(data
,s1);*

  • }*
  • if(strcmp(data[0],“Shakehand”)==0){*
  • Serial.print(“OK!”);*
  • }*
  • if(strcmp(data[0],“ADStop”)==0){*
  • Timer1.stop();//停止时钟中断*
  • Timer1.detachInterrupt();//卸载时钟函数*
  • }*
  • if(strcmp(data[0],“ADStart”)==0){*
  • ADLen=0;*
  • Fs=atol(data[1]);if(Fs>8000)Fs=8000;//设定采样频率*
  • ADCh=atoi(data[2])&0xff;*
  • Timer1.initialize(1000000/Fs);//初始化时钟函数*
  • Timer1.attachInterrupt(Tim1_handler);//安装时钟中断*
  • }*
    }
    void Tim1_handler(){
  • unsigned int*
  • v=analogRead(ADCh);//特定的通道采样,启动定时采样*
  • Serial.write(val>>8);//源源不断的向上位机发送*
  • Serial.write(val);*
    }
    sketch_dec17b.ino (1.7 KB)
void Tim1_handler(){
  unsigned int
  val=analogRead(ADCh);//特定的通道采样,启动定时采样
  Serial.write(val>>8);//源源不断的向上位机发送
  Serial.write(val);
}

Sorry I am new for this forum, What PM mean? I do not fully understand your expression. I have seen other users post their question in this forum, do I post in a wrong place?

I never mentioned a PM.

You have a programming question, and you posted in the programming section - no problem.

Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

Ooops.

a7

void Tim1_handler(){
  int v = 0;
  v=analogRead(ADCh);//特定的通道采样,启动定时采样
  Serial.write(MightMorphinPowerRangers>>8);//源源不断的向上位机发送
  Serial.write(Krypton);
}

Idahowalker:

void Tim1_handler(){

int v = 0;
v=analogRead(ADCh);//特定的通道采样,启动定时采样
Serial.write(val>>8);//源源不断的向上位机发送
Serial.write(val);
}

?

TheMemberFormerlyKnownAsAWOL:
?

Trying to be less vague so the fellow might see the error and fix the rest of the code.

Idahowalker:

void Tim1_handler(){

int v = 0;
 v=analogRead(ADCh);//特定的通道采样,启动定时采样
 Serial.write(QuantomTheory>>8);//源源不断的向上位机发送
 Serial.write(AstroTurf);
}

I have try this one, but it still not work.

qingdaisky:
I have try this one, but it still not work.

Of course it won’t work it broken on purpose so you can learn a lesson.

Hint #2

void Tim1_handler(){
  int v = 0;
  v=analogRead(ADCh);//特定的通道采样,启动定时采样
  Serial.write(v>>8);//源源不断的向上位机发送
  Serial.write(ImTryingToPrintAnUndeclaredVariableButIKeepGettingAnErrorIWontReadTheErrorMessageAndIExpectSomeElseToFixMyCode);
}

I bet that don’t work either but you just might see your original error.

TheMemberFormerlyKnownAsAWOL
thank you so much for your help, it works. I really appreciate that.