#include <Ticker.h>
int serialA;
int val;
int A=0;
int B=0;
//-----------------------------------------------------------//
int inputpin0 = A0;
const int n1 = 4;
int data1[n1]; // all array data
int readIndex1 = 0; // the index1 of the current reading
const int n2 = 256;
int data2[n2]; // all array data
int readIndex2 = 0; // the index1 of the current reading
//-----------------------------------------------------------//
void setup() {
for (int i = 0; i < n1 ;i++) //0~4
{
data1[i] = 0;
}
for (int i = 0; i < n2 ;i++) //256
{
data2[i] = 0;
}
pinMode(A0,INPUT);
Serial.begin(115200);
timer1_attachInterrupt(timerIsr); //宣告初始化
timer1_enable(TIM_DIV16, TIM_EDGE,TIM_LOOP);
timer1_write(20833.3);
}
void loop() {
serialA=Serial.read();
Serial.println(serialA);
delay(100);
}
void timerIsr(){
data1[readIndex1] = analogRead(inputpin0);
data2[readIndex2]=(data1[0]+data1[1]+data1[2]+data1[3])/4;
readIndex2 = readIndex2 + 1;
if(readIndex2 >= n2 )
{
readIndex2 = 0;
}
int Data[2];
B=B+1;
Data[0]=data2[readIndex2]/256;
Data[1]=data2[readIndex2]%256;
if (A==1){
if (B>=4){
for(int j=0;j<6;j++)
Serial.write(Data[j]);
B=0;
}
}
readIndex1 = readIndex1 + 1;
if(readIndex1 >= n1 )
{
readIndex1 = 0;
}
}
Finally,Serial.write(Data[j]);
and serialA=Serial.read();
1.I want to print out the value of serialA
But the answer I get is "-1"
2.If I use analogRead(serialA)
the answer I get is "0"
I don't know what's wrong!
1.I want to print out the value of serialA
But the answer I get is "-1"
That is the value you get when you read from Serial and no data is available.
Test whether data is available by using the Serial.available() function and only read and print the data if it is available
Declare all of the variables modified in the ISR as volatile to avoid losing their values and note that Serial does not work properly inside an ISR because it used interrupts and they are automatically disabled on entry to the ISR
UKHeliBob:
That is the value you get when you read from Serial and no data is available.
Test whether data is available by using the Serial.available() function and only read and print the data if it is available
Declare all of the variables modified in the ISR as volatile to avoid losing their values and note that Serial does not work properly inside an ISR because it used interrupts and they are automatically disabled on entry to the ISR
Declare all of the variables modified in the ISR as volatile to avoid losing their values
I don’t know which variable.
I have been thinking the final output value is Data[j].
I don't know which variable.
Declare all of the variables modified in the ISR as volatile
It actually only really applies to variables modified by the ISR and used outside of it but I suspect that you have a bigger problem because you use Serial inside the ISR with interrupts disabled and printing in loop() without testing whether data is available
UKHeliBob:
It actually only really applies to variables modified by the ISR and used outside of it but I suspect that you have a bigger problem because you use Serial inside the ISR with interrupts disabled and printing in loop() without testing whether data is available
At first, I used Bluetooth to transfer to my phone to see the wave shape.
But now I want to print out the value
now I want to print out the value
OK, but don't do it inside the ISR if that is what you are doing.
Instead set a flag in the ISR to indicate that data is available then in loop() if the flag is set print the data and reset the flag
UKHeliBob:
It actually only really applies to variables modified by the ISR and used outside of it but I suspect that you have a bigger problem because you use Serial inside the ISR with interrupts disabled and printing in loop() without testing whether data is available
Can I not use Serial in ISR but I want to set a flag and print?
Can I not use Serial in ISR but I want to set a flag and print?
As I said, you cannot use Serial in an ISR. Instead, where you would have printed in the ISR set a global boolean to true. Then, in loop(), if the boolean is true do the printing and set the boolean to false to prevent printing again until the ISR has been triggered and returns to loop()
If I have understood @UKHeliBob correctly, then you should bring the following changes in your sketch. (Compare the following sketch line by line with your original sketch; you will be able to see the changes.)
#include <Ticker.h>
int serialA;
int val;
int A = 0;
int B = 0;
//-----------------------------------------------------------//
int inputpin0 = A0;
const int n1 = 4;
int data1[n1]; // all array data
int readIndex1 = 0; // the index1 of the current reading
const int n2 = 256;
int data2[n2]; // all array data
int readIndex2 = 0; // the index1 of the current reading
//-----------------------------------------------------------//
bool flag = false;
void setup()
{
for (int i = 0; i < n1 ; i++) //0~4
{
data1[i] = 0;
}
for (int i = 0; i < n2 ; i++) //256
{
data2[i] = 0;
}
pinMode(A0, INPUT);
Serial.begin(115200);
timer1_attachInterrupt(timerIsr); //宣告初始化
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
timer1_write(20833.3);
}
void loop()
{
byte n = Serial.available();
if (n != 0)
{
serialA = Serial.read();
Serial.println(serialA);
delay(100);
}
if (flag == true)
{
data1[readIndex1] = analogRead(inputpin0);
data2[readIndex2] = (data1[0] + data1[1] + data1[2] + data1[3]) / 4;
readIndex2 = readIndex2 + 1;
if (readIndex2 >= n2 )
{
readIndex2 = 0;
}
int Data[2];
B = B + 1;
Data[0] = data2[readIndex2] / 256;
Data[1] = data2[readIndex2] % 256;
if (A == 1)
{
if (B >= 4)
{
for (int j = 0; j < 6; j++)
Serial.write(Data[j]);
B = 0;
}
}
readIndex1 = readIndex1 + 1;
if (readIndex1 >= n1 )
{
readIndex1 = 0;
}
flag = false;
}
}
void timerIsr()
{
flag = true;
}
UKHeliBob:
As I said, you cannot use Serial in an ISR. Instead, where you would have printed in the ISR set a global boolean to true. Then, in loop(), if the boolean is true do the printing and set the boolean to false to prevent printing again until the ISR has been triggered and returns to loop()
The value I want is always displayed
So I will not set true and false
The value I want is always displayed
So I will not set true and false
That is no problem if you don't mind writing the values each time through loop()
GolamMostafa:
If I have understood @UKHeliBob correctly, then you should bring the following changes in your sketch. (Compare the following sketch line by line with your original sketch; you will be able to see the changes.)
#include <Ticker.h>
int serialA;
int val;
int A = 0;
int B = 0;
//-----------------------------------------------------------//
int inputpin0 = A0;
const int n1 = 4;
int data1[n1]; // all array data
int readIndex1 = 0; // the index1 of the current reading
const int n2 = 256;
int data2[n2]; // all array data
int readIndex2 = 0; // the index1 of the current reading
//-----------------------------------------------------------//
bool flag = false;
void setup()
{
for (int i = 0; i < n1 ; i++) //0~4
{
data1[i] = 0;
}
for (int i = 0; i < n2 ; i++) //256
{
data2[i] = 0;
}
pinMode(A0, INPUT);
Serial.begin(115200);
timer1_attachInterrupt(timerIsr); //宣告初始化
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
timer1_write(20833.3);
}
void loop()
{
byte n = Serial.available();
if (n != 0)
{
serialA = Serial.read();
Serial.println(serialA);
delay(100);
}
if (flag == true)
{
data1[readIndex1] = analogRead(inputpin0);
data2[readIndex2] = (data1[0] + data1[1] + data1[2] + data1[3]) / 4;
readIndex2 = readIndex2 + 1;
if (readIndex2 >= n2 )
{
readIndex2 = 0;
}
int Data[2];
B = B + 1;
Data[0] = data2[readIndex2] / 256;
Data[1] = data2[readIndex2] % 256;
if (A == 1)
{
if (B >= 4)
{
for (int j = 0; j < 6; j++)
Serial.write(Data[j]);
B = 0;
}
}
readIndex1 = readIndex1 + 1;
if (readIndex1 >= n1 )
{
readIndex1 = 0;
}
flag = false;
}
}
void timerIsr()
{
flag = true;
}
thanks I will try it
GolamMostafa:
If I have understood @UKHeliBob correctly, then you should bring the following changes in your sketch. (Compare the following sketch line by line with your original sketch; you will be able to see the changes.)
#include <Ticker.h>
int serialA;
int val;
int A = 0;
int B = 0;
//-----------------------------------------------------------//
int inputpin0 = A0;
const int n1 = 4;
int data1[n1]; // all array data
int readIndex1 = 0; // the index1 of the current reading
const int n2 = 256;
int data2[n2]; // all array data
int readIndex2 = 0; // the index1 of the current reading
//-----------------------------------------------------------//
bool flag = false;
void setup()
{
for (int i = 0; i < n1 ; i++) //0~4
{
data1[i] = 0;
}
for (int i = 0; i < n2 ; i++) //256
{
data2[i] = 0;
}
pinMode(A0, INPUT);
Serial.begin(115200);
timer1_attachInterrupt(timerIsr); //宣告初始化
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
timer1_write(20833.3);
}
void loop()
{
byte n = Serial.available();
if (n != 0)
{
serialA = Serial.read();
Serial.println(serialA);
delay(100);
}
if (flag == true)
{
data1[readIndex1] = analogRead(inputpin0);
data2[readIndex2] = (data1[0] + data1[1] + data1[2] + data1[3]) / 4;
readIndex2 = readIndex2 + 1;
if (readIndex2 >= n2 )
{
readIndex2 = 0;
}
int Data[2];
B = B + 1;
Data[0] = data2[readIndex2] / 256;
Data[1] = data2[readIndex2] % 256;
if (A == 1)
{
if (B >= 4)
{
for (int j = 0; j < 6; j++)
Serial.write(Data[j]);
B = 0;
}
}
readIndex1 = readIndex1 + 1;
if (readIndex1 >= n1 )
{
readIndex1 = 0;
}
flag = false;
}
}
void timerIsr()
{
flag = true;
}
I thought I shouldn't use Serial in Isr.
But seeing you write it in loop.
I feel refreshed.
"flag" should be qualified "volatile"
TheMemberFormerlyKnownAsAWOL:
"flag" should be qualified "volatile"
Do you mean use 'volatile int' write?
volatile bool flag = false;
Is it a formula that we have to use the volatile keyword before a global variable whose value might be changed by an ISR? If volatile keyword is not used, what kind of mess (confusing situation) would be there?
The literature says that the absence of the volatile keyword may prompt the compiler to optimize the code (bool flag = false;); the compiler will keep the value of the variable in a register (rather than in a RAM location) to speed up program execution (speed/execution time optimization). Why does it matter that the global variable must be allocated space in the RAM during compilation process particularly in a sketch that involves ISR which might change the variable?