I have made some progress with my combined servo tester and pulse generator project. I am happy with the servo tester part so I have decided to add an OLED display (128 x 64) and a button to select different modes. The Servo Tester will be the first mode and the second mode I want to add will be a pulse generator. At present I have not written the code for the Pulse Generator but I now have the display working and the button to change between modes.
The revised circuit diagram is show below. I have just added a push-to-make button on D3 for mode selection and a 128x64 OLED display (SDA on A4 and SCL on A5). An updated circuit diagram is shown below.
The display is Adafruit compatible so I just used their libraries. I have not used button inputs on my projects for a very long time so it was a challenge to get that working. Identifying how the debounce would work and then how to use it to select the mode. I now have an infinite while statement inside the main loop, which will just cycle sequentially through the modes available. At present there are only have two modes: Servo Tester and Pulse Generator, but it would be easy to add further modes, perhaps such as a DVM. The main loop part of the programme is listed below.
void loop(void)
{
int width;
int freq;
int modeselect;
int newmode;
width = 0;
freq = 0;
modeselect = 1; // Servo tester
newmode = 1; // Used to read button
Serial.println("Servo Motor Tester " );
Serial.println("Dubbie Dubbie : Just for Fun " );
Serial.println(" 5th Feb'21 ");
Serial.println();
display.clearDisplay(); // Clear the display so we can refresh
display.setCursor(0,10); // Bottom left
display.println("Dubbie Dubbie ");
display.println("Combined ");
display.println("Servo Tester and ");
display.println("Pulse Generator ");
display.display(); // This outputs to the screen
delay(1500);
digitalWrite(ledblue, HIGH); // Shows it's working
delay(500);
digitalWrite(ledblue, LOW);
while(1)
{
servotestermode();
pulsegeneratormode();
} /* while */
} /* loop */
void mydisplaywidth(void)
{
int pulsevalue;
pulsevalue = 0;
display.setCursor(80, 50); // Output the pulse width - bottom right
display.fillRect(80, 50, 45, 8, 0); // Clear previous value
pulsevalue = analogRead(adcwidth);
display.setCursor(80, 50); // Output the pulse width - bottom right
display.print(pulsevalue);
display.print(" us");
display.display();
} /* mydisplaywidth */
bool buttonpress(int buttonselect)
{
int buttonvalue;
buttonvalue = 0;
buttonvalue = digitalRead(buttonselect);
if (buttonvalue > 0)
{
return(false);
}
else
{
do
{
delay(20); // 20 ms debounce delay // press debounce
} while (digitalRead(buttonselect) < 1);
do
{
delay(20); //20 ms debounce delay // release debounce
} while (digitalRead(buttonselect) < 1);
return(true);
} /* if */
} /* buttonpress */
void servotestermode(void)
{
int pwidth;
pwidth = 0;
display.clearDisplay(); // Clear the display so we can refresh
display.setCursor(0,50); // Bottom left
display.println("Servo Tester ");
while (!buttonpress(button))
{
digitalWrite(testservo, HIGH);
delayMicroseconds(200); // Start of the 1 ms pulse
delayMicroseconds(pwidth); // Makes up the rest of the pulse
digitalWrite(testservo, LOW);
Serial.println(pwidth);
pwidth = analogRead(adcwidth); //Assumes 100 us conversion time
pwidth = pwidth * 2;
mydisplaywidth();
delay(18); // The rest of the pulse
} /* while */
} /* servotestermode */
void pulsegeneratormode(void)
{
while (!buttonpress(button))
{
display.clearDisplay(); // Clear the display so we can refresh
display.setCursor(0,25); // Middle left
display.println("Pulse Generator ");
display.display(); // This outputs to the screen
} /* while */
}
After spending a couple of days trying to get the push button and debounce to work I was pleased when it all came together this morning, as illustrated in the following video.
All I have to do now is write the code for the pulse generator and if time permits, create some sort of 3D printed box, or possibly buy a case.
Dubbie
Top Comments