#include "BluetoothSerial.h"
BluetoothSerial serialBT;
const int numReadings = 100;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int prev = 0;
int inputPin = 4;
int data = 0;
void setup() {
Serial.begin(9600);
serialBT.begin("botol");
pinMode(4, INPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(inputPin);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
if (abs(average - prev) > 20) {
Serial.println(average);
serialBT.println(average);
prev = average;
delay(1);
// delay in between reads for stability
}
}
One way to analyze a piece of code is to examine the life history of each variable.
The variable datatar is unusual, in that it starts out 0 in the blocks, and is never changed.
So it's always zero.
It's used to compare abs(datacur-dataprev) to 0, so the IF/then fires only if datacur equals dataprev.
datacur is what just arrived from BlueTooth.
dataprev starts out as zero.
dataprev gets to change only if both if/then blocks fire, because they are nested.
But the two conditions are incompatible.
Do you have a reference to where you got this algorithm?
P.S. it would be helpful to accumulate the incoming reading in a list, inserting the latest at slot 1, and refreshing a debug List Picker's Elements from that list.
Working from the title, it looks like you need to keep a list of all the input readings in AI2, and let the list get long enough to cover that certain time, using the certain time divided by the Clock Timer interval to get desired length of list.
The list of readings need to get new readings added at the end, and if it gets too long, the oldest should be removed from slot 1.
After the list has reached the required length, the first and last items of the list should be compared.
If the last is less than the first, you have your decrease so do your reset (?) else send your notification.
Ah, thanks for pointing out my mistake, i mght have to research the list stream though, but i might have to come back later to you for that later,
The algorithm is well just came from me, while the millis came from this forum, agin i'm gonna come back to you later with the reference link.
On the design side, i also add the value of the datacur and dataprev, but somehow it doesn't display correctly.
Thank you for replying.
So the datacur is what i get from the data that received from the bluetooth, and the dataprev is what i get after the first comparison happened.
I actually confused myself, haha..
So anyway, its like this i guess
Datacur - dataprev < 100 =
4095 - 0 < 100
Actually, the dataprev will not get the set.datacur = get.dataprev didn't it?
So the data prev will always be zero, that's why the notification didn't get sent?
So what should i do? Should i set the dataprev initial value to 4095?