{"id":3873,"date":"2022-12-12T19:17:39","date_gmt":"2022-12-12T10:17:39","guid":{"rendered":"https:\/\/programresource.net\/?p=3873"},"modified":"2022-12-12T19:17:39","modified_gmt":"2022-12-12T10:17:39","slug":"simple-rpm-counter-using-m5stickc","status":"publish","type":"post","link":"https:\/\/programresource.net\/en\/2022\/12\/12\/3873.html","title":{"rendered":"Simple RPM counter using M5StickC"},"content":{"rendered":"\n<p>Simple sketch using M5 StickC for counting stepper motor RPM.<\/p>\n\n\n\n<p>Sensor using laser and laser detector. Connect laser detector signal to pin 33.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/sQqezEsiuUU?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<p>Modified from other sketch, there are some unused display related codes.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;M5StickC.h&gt;\n#include &lt;Wire.h&gt;\n\n#define PIN_SENSOR\t33\nint lastdata&#91;2];\nchar chbuff&#91;32], chbuff_num&#91;32];\nunsigned long current_millis;\nunsigned long last_data_update = 0;\n\nvoid drawflame() {\n\t\/\/resolution 160x80\n\tM5.Lcd.fillScreen(BLACK);\n\tM5.Lcd.drawRoundRect(5, 5, 75, 35, 3, DARKCYAN);\n\tM5.Lcd.drawRoundRect(5, 5 + 40, 75, 35, 3, DARKCYAN);\n\tM5.Lcd.drawRoundRect(5 + 80, 5 + 40, 75, 35, 3, DARKCYAN);\n\tM5.Lcd.setTextColor(WHITE, BLACK);\n\tM5.Lcd.setTextSize(1);\n\tM5.Lcd.setCursor(10, 2);\n\tM5.Lcd.print(&quot;rpm&quot;);\n\tM5.Lcd.setCursor(10, 42);\n\tM5.Lcd.print(&quot;---&quot;);\n\tM5.Lcd.setCursor(90, 42);\n\tM5.Lcd.print(&quot;---&quot;);\n}\n\nvoid showtext(char *msg) {\n\tM5.Lcd.fillScreen(BLACK);\n\tM5.Lcd.setTextColor(WHITE);\n\tM5.Lcd.setCursor(10, 30);\n\tM5.Lcd.setTextSize(1);\n\tM5.Lcd.print(msg);\n}\n\nvoid showtext_relay(char *val) {\n\tint xshift = 80, yshift = 40;\n\tM5.Lcd.fillRect(10 + xshift, 10 + yshift, 65, 25, BLACK);\n\tM5.Lcd.setTextSize(2);\n\tM5.Lcd.setTextDatum(MC_DATUM);\n\tM5.Lcd.drawString(val, 40 + xshift, 24 + yshift);\n}\n\nvoid showtext_data(int pos, int val, char *unit) {\n\tint xshift = 0, yshift = 0;\n\tif (lastdata&#91;pos] == val)\n\t\treturn;\n\tlastdata&#91;pos] = val;\n\n\tif (pos == 1)\n\t\tyshift = 40;\n\tM5.Lcd.fillRect(10 + xshift, 10 + yshift, 65, 25, BLACK);\n\tM5.Lcd.setTextSize(1);\n\tM5.Lcd.setTextDatum(BR_DATUM);\n\tM5.Lcd.drawString(unit, 78 + xshift, 34 + yshift);\n\tM5.Lcd.setTextSize(2);\n\tM5.Lcd.setTextDatum(MC_DATUM);\n\tsprintf(chbuff, &quot;%d&quot;, val);\n\tM5.Lcd.drawString(chbuff, 40 + xshift, 24 + yshift);\n}\n\nint last_sensor = LOW;\nint new_sensor;\nunsigned long last_sensor_time;\nunsigned long rot_diff;\n\nvoid setup() {\n\tM5.begin();\n\tdelay(10);\n\n\tSerial.println(&quot;boot&quot;);\n\n\tM5.Lcd.setRotation(1);\n\tM5.Axp.ScreenBreath(10);\n\n\tpinMode(PIN_SENSOR,INPUT);\n\tlast_sensor = digitalRead(PIN_SENSOR);\n\t\n\tdrawflame();\n\t\/\/showtext_relay(&quot;Off&quot;);\n\tSerial.println(&quot;ready&quot;);\n}\n\nvoid loop() {\n\tM5.update();\n\n\tnew_sensor = digitalRead(PIN_SENSOR);\n\tif (new_sensor != last_sensor){\n\t\tlast_sensor = new_sensor;\n\t\tif (new_sensor == HIGH){\n\t\t\t\/\/Serial.println(&quot;detect&quot;);\n\t\t\tif (millis() - last_sensor_time &gt; 22){\n\t\t\t\trot_diff = millis() - last_sensor_time;\n\t\t\t\tSerial.println(rot_diff);\n\t\t\t\tlast_sensor_time = millis();\n\t\t\t}\n\t\t}\n\t}\n\n\tcurrent_millis = millis();\n\tif ((unsigned long)(current_millis - last_data_update) &gt;= 300) {\n\t\tlast_data_update = current_millis;\n\t\tif (rot_diff &gt; 0)\n\t\t\tshowtext_data(0, (int)(60000\/rot_diff), &quot;rpm&quot;);\n\t}\n}\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Simple sketch using M5 StickC for counting stepper motor RPM. Sensor using laser and laser detector. Connect laser detector signal to pin 33. Modified from other sketch, there are some unused display related codes.<\/p>\n","protected":false},"author":2,"featured_media":3871,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[868],"tags":[870,1077,893,1078,1076],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/programresource.net\/images\/2022\/12\/20221212.png","jetpack_shortlink":"https:\/\/wp.me\/p3pJyQ-10t","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/3873"}],"collection":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/comments?post=3873"}],"version-history":[{"count":1,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/3873\/revisions"}],"predecessor-version":[{"id":3874,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/3873\/revisions\/3874"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/media\/3871"}],"wp:attachment":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/media?parent=3873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/categories?post=3873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/tags?post=3873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}