A pillow or plush toy that makes a sound when you pet it.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

petsound.ino 705B

12345678910111213141516171819202122232425262728293031323334
  1. #define SENSORPIN A7
  2. #define OUTPIN 13
  3. #define DELAYMILLIS 50
  4. #define NSAMPLES 20
  5. #define MINCHANGE 13
  6. #define ON HIGH
  7. #define OFF LOW
  8. int samples[NSAMPLES] = {0};
  9. int index;
  10. void setup() {
  11. pinMode(OUTPIN, OUTPUT);
  12. Serial.begin(115200);
  13. index = 0;
  14. }
  15. void loop() {
  16. samples[index] = analogRead(SENSORPIN);
  17. index = (index + 1) % NSAMPLES;
  18. int maxlevel = -1;
  19. int minlevel = 2000;
  20. for (int i = 0; i < NSAMPLES; i++) {
  21. if (samples[i] < minlevel) {
  22. minlevel = samples[i];
  23. }
  24. if (samples[i] > maxlevel) {
  25. maxlevel = samples[i];
  26. }
  27. }
  28. Serial.println(maxlevel - minlevel);
  29. digitalWrite(OUTPIN, (maxlevel - minlevel) > MINCHANGE ? ON : OFF);
  30. delay(DELAYMILLIS);
  31. }