Arduino Tipbot


Arduino Nano
(6) 9G Servo
Neopixels
220 ohm Resistor
100np Capacitor
IR Sensor Module
Ultrasonic Sensor Module


  1. // Arduino Tipbot
  2. // pkvi
  3. #include <FastLED.h>
  4. #include <VarSpeedServo.h>
  5. // neopixels
  6. #define NUM_LEDS 11
  7. CRGB leds[NUM_LEDS];
  8. #define PIN 9
  9. // random
  10. int fun_ran = 0;
  11. // ping
  12. const int us_pin = 8;
  13. // left front
  14. VarSpeedServo one_u;
  15. const int one_u_pin = 2;
  16. VarSpeedServo one_d;
  17. const int one_d_pin = 3;
  18. // right front
  19. VarSpeedServo two_u;
  20. const int two_u_pin = 6;
  21. VarSpeedServo two_d;
  22. const int two_d_pin = 7;
  23. // rear
  24. VarSpeedServo thr_u;
  25. const int thr_u_pin = 4;
  26. VarSpeedServo thr_d;
  27. const int thr_d_pin = 5;
  28. // ir
  29. int ir_pin = 10;
  30. // values
  31. int slow = 20;
  32. int reg = 40;
  33. int fast = 60;
  34. int state = 0;
  35. int other = 0;
  36. int coins = 0;
  37. void setup() {
  38. Serial.begin(9600);
  39. FastLED.addLeds<WS2812B, PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  40. one_u.attach(one_u_pin);
  41. one_d.attach(one_d_pin);
  42. two_u.attach(two_u_pin);
  43. two_d.attach(two_d_pin);
  44. thr_u.attach(thr_u_pin);
  45. thr_d.attach(thr_d_pin);
  46. pinMode(ir_pin, INPUT);
  47. setAll(0, 0, 0);
  48. for (int s = 0; s < 11; s++) {
  49. setPixel(s, 0, 255, 0);
  50. showStrip();
  51. delay(100);
  52. }
  53. up();
  54. delay(1000);
  55. down();
  56. delay(1000);
  57. stand();
  58. for (int s = 0; s < 11; s++) {
  59. setPixel(s, 0, 0, 0);
  60. showStrip();
  61. delay(10);
  62. }
  63. setAll(0, 0, 0);
  64. showStrip();
  65. delay(1000);
  66. }
  67. void loop() {
  68. // standing?
  69. if (state == 0) {
  70. stand();
  71. state = 1;
  72. }
  73. // coins?
  74. coin_check();
  75. // distance
  76. long dur, cm;
  77. pinMode(us_pin, OUTPUT);
  78. digitalWrite(us_pin, LOW);
  79. delayMicroseconds(2);
  80. digitalWrite(us_pin, HIGH);
  81. delayMicroseconds(5);
  82. digitalWrite(us_pin, LOW);
  83. pinMode(us_pin, INPUT);
  84. dur = pulseIn(us_pin, HIGH);
  85. cm = mstocm(dur);
  86. Serial.println(cm);
  87. // coins?
  88. coin_check();
  89. if (cm < 50 && cm > 7) {
  90. look_up();
  91. delay(4000);
  92. look_down();
  93. delay(3000);
  94. stand();
  95. state = 1;
  96. } else if (cm < 7) {
  97. scared();
  98. delay(3000);
  99. save();
  100. state = 0;
  101. } else {
  102. fun_ran = random(0, 100);
  103. if (fun_ran == 0) {
  104. up();
  105. delay(3000);
  106. state = 0;
  107. } else if (fun_ran == 1) {
  108. down();
  109. delay(3000);
  110. state = 0;
  111. } else if (fun_ran == 2) {
  112. bounce();
  113. delay(3000);
  114. state = 0;
  115. } else if (fun_ran == 3) {
  116. left();
  117. delay(3000);
  118. state = 0;
  119. } else if (fun_ran == 4) {
  120. right();
  121. delay(3000);
  122. state = 0;
  123. } else if (fun_ran == 5) {
  124. look_up();
  125. delay(3000);
  126. state = 0;
  127. } else if (fun_ran == 6) {
  128. look_down();
  129. delay(3000);
  130. state = 0;
  131. }
  132. }
  133. // coins?
  134. coin_check();
  135. delay(5);
  136. }
  137. void coin_check() {
  138. coins = digitalRead(ir_pin);
  139. if (coins == LOW) {
  140. dance();
  141. }
  142. }
  143. void stand() {
  144. one_u.write(45, slow, false);
  145. one_d.write(50, slow, false);
  146. two_u.write(55, slow, false);
  147. two_d.write(50, slow, false);
  148. thr_u.write(45, slow, false);
  149. thr_d.write(50, slow, true);
  150. for (int s = 255; s > 0; s--) {
  151. setAll(0, s, 0);
  152. showStrip();
  153. delay(1);
  154. }
  155. for (int s = 0; s < 255; s++) {
  156. setAll(0, s, 0);
  157. showStrip();
  158. delay(1);
  159. }
  160. }
  161. void up() {
  162. one_u.write(90, reg, false);
  163. one_d.write(20, reg, false);
  164. two_u.write(100, reg, false);
  165. two_d.write(20, reg, false);
  166. thr_u.write(90, reg, false);
  167. thr_d.write(20, reg, true);
  168. setAll(0, 255, 0);
  169. showStrip();
  170. }
  171. void down() {
  172. one_u.write(30, reg, false);
  173. one_d.write(60, reg, false);
  174. two_u.write(35, reg, false);
  175. two_d.write(60, reg, false);
  176. thr_u.write(30, reg, false);
  177. thr_d.write(60, reg, true);
  178. setAll(0, 255, 0);
  179. showStrip();
  180. }
  181. void bounce() {
  182. for (int x = 0; x < 5; x++) {
  183. up();
  184. glitter(80);
  185. down();
  186. }
  187. stand();
  188. }
  189. void dance() {
  190. for (int x = 0; x < 5; x++) {
  191. one_u.write(30, reg, false);
  192. one_d.write(60, reg, true);
  193. two_u.write(30, reg, false);
  194. two_d.write(60, reg, false);
  195. one_u.write(45, reg, false);
  196. one_d.write(50, reg, true);
  197. thr_u.write(30, reg, false);
  198. thr_d.write(60, reg, false);
  199. two_u.write(45, reg, false);
  200. two_d.write(50, reg, true);
  201. thr_u.write(45, reg, false);
  202. thr_d.write(50, reg, true);
  203. }
  204. stand();
  205. }
  206. void left() {
  207. one_u.write(90, reg, false);
  208. one_d.write(20, reg, false);
  209. two_u.write(40, reg, false);
  210. two_d.write(40, reg, false);
  211. thr_u.write(30, reg, false);
  212. thr_d.write(60, reg, true);
  213. setAll(0, 255, 0);
  214. showStrip();
  215. }
  216. void right() {
  217. one_u.write(40, reg, false);
  218. one_d.write(40, reg, false);
  219. two_u.write(90, reg, false);
  220. two_d.write(20, reg, false);
  221. thr_u.write(30, reg, false);
  222. thr_d.write(60, reg, true);
  223. setAll(0, 255, 0);
  224. showStrip();
  225. }
  226. void scared() {
  227. one_u.write(5, fast, false);
  228. one_d.write(60, fast, false);
  229. two_u.write(5, fast, false);
  230. two_d.write(60, fast, false);
  231. thr_u.write(5, fast, false);
  232. thr_d.write(60, fast, true);
  233. setAll(0, 0, 255);
  234. showStrip();
  235. }
  236. void look_up() {
  237. // front up
  238. one_u.write(90, reg, false);
  239. one_d.write(20, reg, false);
  240. two_u.write(90, reg, false);
  241. two_d.write(20, reg, false);
  242. // back down
  243. thr_u.write(30, reg, false);
  244. thr_d.write(60, reg, true);
  245. setAll(0, 255, 0);
  246. showStrip();
  247. }
  248. void look_down() {
  249. // front down
  250. one_u.write(30, reg, false);
  251. one_d.write(60, reg, false);
  252. two_u.write(30, reg, false);
  253. two_d.write(60, reg, false);
  254. // back up
  255. thr_u.write(90, reg, false);
  256. thr_d.write(20, reg, true);
  257. setAll(0, 255, 0);
  258. showStrip();
  259. }
  260. long mstocm(long microseconds) {
  261. return microseconds / 29 / 2;
  262. }
  263. void save() {
  264. one_d.write(80, reg, false);
  265. one_u.write(20, reg, false);
  266. two_d.write(80, reg, false);
  267. two_u.write(20, reg, false);
  268. thr_d.write(80, reg, false);
  269. thr_u.write(20, reg, true);
  270. }
  271. void glitter(fract8 chance) {
  272. if (random8() < chance) {
  273. leds[random16(NUM_LEDS)] += CRGB::White;
  274. }
  275. }
  276. void showStrip() {
  277. #ifdef ADAFRUIT_NEOPIXEL_H
  278. // NeoPixel
  279. strip.show();
  280. #endif
  281. #ifndef ADAFRUIT_NEOPIXEL_H
  282. // FastLED
  283. FastLED.show();
  284. #endif
  285. }
  286. void setPixel(int Pixel, byte red, byte green, byte blue) {
  287. #ifdef ADAFRUIT_NEOPIXEL_H
  288. // NeoPixel
  289. strip.setPixelColor(Pixel, strip.Color(red, green, blue));
  290. #endif
  291. #ifndef ADAFRUIT_NEOPIXEL_H
  292. // FastLED
  293. leds[Pixel].r = red;
  294. leds[Pixel].g = green;
  295. leds[Pixel].b = blue;
  296. #endif
  297. }
  298. void setAll(byte red, byte green, byte blue) {
  299. for (int i = 0; i < NUM_LEDS; i++ ) {
  300. setPixel(i, red, green, blue);
  301. }
  302. showStrip();
  303. }

Menu
Index
Engineering
Entertainment
Literature
Miscellaneous
Contact
Search
tiktok.com/@pkvi.xyz
Why Ayh?
Miter
Miter
@pkvi
"...may not meet professional standards."
2,363 miters
123 tenons
Subscribe
0.00512