I made my project new with classes but now the time to get the normalized values of the irsensor is so much longer than doing it in the loop (main.cpp) like before. Am I missing something in my code or is it just arduino that can't handle classes good enough?
I have several Ir-Sensors looking for a ball which is emitting a impulse of ir-light which is around maybe 1000 hz or so i dont rly know right now and I wanted to get rid of the 0 values(where it is not emitting) by this code but how would you do it?
Did you change both definitions of premil to unsigned long?
How are you measuring the time taken; with the display.println(millis() - premil) line?
What performance measurements were you getting before on the code that we can't see, compared with the values you are getting now on the code we can see?
The values getting before were immediately there (sry I din't measure it then and I dont have this code) and now it's taken around 10 seconds to get the new values but the performance is around 7 milliseconds.
But it is not neccessary to fix the issue with this code I just need to read the values properly without the values when the ir-lights are not emitting.
const int MapValues = 200;
int IrSensor::Read(){
return analogRead(pinNum);
}
int IrSensor::update()
{
if(count < MapValues)
{
value += Read();
count ++;
}
else
{
normalizedValue = (value / count);
value = 0;
count = 0;
}
return normalizedValue;
}
void loop() //-----------------------------------------------loop------------------------------------------------------
{
...
for (int i = 0; i < 5; i++)
{
values[i] = sensor[i].update();
}
...
display.setCursor(0,0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(values[0]);
display.println(values[1]);
display.println(values[2]);
display.println(values[3]);
display.println(values[4]);
display.println(millis() - premil);
display.display();
display.clearDisplay();
}
For each change of values[] per sensor you do 200 *analogRead()*s. There are 5 sensors, so 1000 reads in total, each of approx 100uS on an Uno. If I calculate correctly that is something like 0.1s before a value will change on the display.
However, each time through loop() you also update the display. That means that you update the display 200 times before you have an updated value.
All those display updates are slooow as they involve communication over I2C.
Try just updating the screen when something has actually changed, or perhaps better, after a fixed time interval, eg. 1 second.
Mika857:
But it is not neccessary to fix the issue with this code I just need to read the values properly without the values when the ir-lights are not emitting.
Sure, but if you can't get this code functional why do you think the next will be any better?
okay so now I changed the mapvalues to 20 and it works but it is still too slow and this project will get a lot bigger so I have to come up with sth new maybe or so. I'm frustrated right now because nothing works.
here is the updated one. It is still nowhere to what I need. I don't know where the lack of performance comes from changed from 200 to 20 and still takes so long.
It may not be the reason for your performance issues, but why are you creating four Adafruit_DCMotor objects in your main .cpp file and then passing them by value to the “Drive” class’s constructor only to be copied into four DIFFERENT Adafruit_DCMotor objects inside that object? That’s ridiculously inefficient. It also assumes that the Adafruit_DCMotor’s default ‘operator =’ works as you think.
Just use a pointer or a reference to the original Adafruit_DCMotor objects from the .cpp file.
Ok, so no timings, just things aren't working as you want.
You seem to have removed most of the display stuff but have now uncommented the motor stuff. You need to focus on getting one thing to work before moving on to the next complication.
Perhaps now is the time for you to explain in more detail what your project is actually about and what exact hardware you are using to attempt build it.
The project is for a football playing robot, but I can't say much about the hardware because most of the hardware I got, is from older guys in my club.
I have a feeling this thread would have been better posted in the "project guidance" subforum...
With so little information to go on I will just suggest three possibilities in a general sense:
A hardware based solution which handles the 1kHz signal detection for you and supplies the Arduino with a simple signal strength for each receiver. I guess this solution is out of the question since you have been handed a bunch of unknown hardware and told to get on with it.
Your existing attempt at a software solution - determine the baseline sensor output when no IR is present then, during detection, look for a significant increase in mean received IR over a set of samples. A reasonable strategy, but will perform poorly if there is any stray IR coming from the environment. You just need to improve/fix your implementation.
Detect the 1kHz signal in software using, probably, the Goertzel algorithm (you can find a library). This would be more resistant to stray IR, but you'd have to see if a software solution like this is going to be fast enough to read and process 5 analogue signals in real time.
In any case, there is no point adding code to run motors if you haven't got the IR detection working. Get one step done at a time and keep backups of the sketch at points where you get stuff working.