Servo inside switch-case routine not working

Hi! I'm trying to convert this project: Machine Learning on an 8-bit Micro written in pure C to Arduino Uno/Nano. Maybe somebody else finds it useful too. As I have gone through all the error and warning messages I still couldn't understand, why servo in switch-case doesn't work. I have added some 'debug' messages inside to be sure the routine is called and performed.
I spent few hours on research, where is the rootcause problem, but didn't succeed.
All the code + libraries in attachments+using standard servo.h library.

Thanks a lot for your help!

ann_tank_original.ino (23.1 KB)

EventFuse.h (2.42 KB)

TimerOne.h (8.8 KB)

SoftPWM.h (1.53 KB)

SimpleFIFO.h (2.87 KB)

nnTypes.h (1.58 KB)

ResHacker:
I still couldn't understand, why servo in switch-case doesn't work.

Please explain what you mean by "doesn't work". Do you mean that it doesn't compile, or that you can upload the program, but it doesn't work as expected?

Hello, sorry for bad problem spec. Code is compiled without any problem, in Setup() routine servo works normaly - means it reacts on myservo.write(xx) command, but in the switch case there's no reaction to the same command, servo doesn't move.. doesn't make sense to me.. I have also tried to add some Serial.println commands to this routine to be sure that is called and running..

Not sure which board you are using, and how the various libraries behave, but possibly a conflict between the Servo.h and TimerOne.h libraries.

Using clone UNO and Nano, both behaves the same way.. Yes, I think so, otherwise it doesn't make a sense to me..

So I have discovered the problem to be not connected with switch case.. I tried to put some command on servo into each line of Setup routine and the servo stops reacting on commands directly after calling routine Timer1.initialize - so it's definitely connected with TimerOne.h library.

Routine Timer1.initialize in TimerOne.h looks like this:

void initialize(unsigned long microseconds=1000000) __attribute__((always_inline)) {
  TCCR1B = _BV(WGM13);        // set mode as phase and frequency correct pwm, stop the timer
  TCCR1A = 0;                 // clear control register A 
  setPeriod(microseconds);

So my question is, what could be the rootcause of such behavior?

Servo.h uses timer 1. So presumably TimerOne.initialize is messing with some settings that Servo.h needs.

You could try using ServoTimer2.h instead but you will need some code changes because commands are not exactly the same as in Servo.h.

Steve

So I have used ServoTimer2.h, servo is moving now into right positions, but it's somehow weird shaking like something is influencing it .. Nothing can go easily xD
I'll maybe go with the SoftwareServo.h library as the original project went..

Problem solved by using SoftwareServo.h library.. Nevertheless I'm curious, why ServoTimer2 library caused such problems...

So last question also answered - Timer2 was actually used by SoftPWM.h library, which is driving the motors via L298P.
Anyway thanks a lot for the support and for those, who want to run this interesting neural network driven robot, do not hesitate to ask :slight_smile: