marquessamackenzie

Cactus Analog Sensor

Dec 11th, 2020 (edited)
38
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*******
  2. * Marquessa MacKenzie 3174722
  3. * Dec 11th, 2020
  4. *
  5. * Code Modified from:
  6. Code for the Textile Game Controller Jam by Social Body Lab, Dames Making Game, & Game Play Lab.
  7. May 30, 2020 - June 6, 2020.
  8. This is code follows along the "How to Make an E-Textile Analog Sensor" here: https://youtu.be/tA37mGEnPes
  9. The code contains 1 analog sensor. The amount of lights that turn on is related to the reading of the pressure sensor.
  10. If high force is applied onto the sensor, the most of the lights will turn on. If light pressure is applied, only a few will turn on.
  11.  
  12. Circuit Playground Library:
  13. https://learn.adafruit.com/circuit-playground-lesson-number-0
  14. *
  15. * and includes a commented out section from Hello_NeoPixels Adafruit Demo
  16. *********/
  17.  
  18. /**Must include the Adafruit_CircuitPlayground.h**/
  19. #include <Adafruit_CircuitPlayground.h>
  20.  
  21. int analogPin = 6; // select the input pin for the potentiometer
  22. int analogValue; // variable to store the value coming from the sensor
  23. int analogMin = 950; // minimum sensor value
  24. int analogMax = 1023; // maximum sensor vlalue
  25. int colour; // value for brightness
  26.  
  27. void setup() {
  28.  
  29. Serial.begin(9600); // Starting the serial monitor
  30.  
  31. CircuitPlayground.begin(); // Initializing the Circuit Playground
  32.  
  33. /**Calibrating the values for the analog sensor**/
  34. /**Press as hard as your can on the analog sensor for 3 seconds and then let it rest for two seconds
  35. This snippet of code finds the highest value and lowest value from the analog sensor**/
  36.  
  37. while (millis() < 5000) {
  38. analogValue = analogRead(analogPin); // Read all of the incoming analog values
  39. Serial.println(analogValue); // print out these values
  40.  
  41. // record the maximum sensor value
  42. if (analogValue > analogMax) { // if the value that is read is higher than the last highest value
  43. analogMax = analogValue; // analogMax is now the highest value that is read
  44. }
  45.  
  46. // record the minimum sensor value
  47. if (analogValue < analogMin) { // if the value that is read is lower than the last lowest value
  48. analogMin = analogValue; // analogMin is now the lowest value that is read
  49. }
  50. }
  51.  
  52. }
  53.  
  54. void loop() {
  55. analogValue = analogRead(analogPin); // Read the Analog Sensor
  56.  
  57. colour = map(analogValue, analogMin, analogMax, 0, 1023); // Map the incoming value. If it is near the maxium value that was calibrated, the variable colour will be assigned a number close to 250.
  58. int len = colour / 100; // The highest value is 250, and there are 10 pixels. Dividing by 25 will give us a value between 1-10.
  59. Serial.println(len); // print out the value of len
  60.  
  61. if (len > 1) { // if the sensor is activated
  62. for (int i = 0; i < len; i++) { // loop through until how "hard" the user was pushing.
  63. CircuitPlayground.setPixelColor(i, 0, 128, 0); // set the lights to green
  64. delay(50); // delay allows us to see the LEDs light up individually
  65. //CircuitPlayground.setPixelColor(0, 255, 0, 0); // this was me trying to see if I could get individual lights to change different colours but then I realized that I would need to add a delay to every single light
  66. //CircuitPlayground.setPixelColor(1, 128, 128, 0); // with the delay it slowed down the processing too much and was no longer immediately responsive.
  67. //CircuitPlayground.setPixelColor(2, 0, 255, 0);
  68. //CircuitPlayground.setPixelColor(3, 0, 128, 128);
  69. //CircuitPlayground.setPixelColor(4, 0, 0, 255);
  70.  
  71. //CircuitPlayground.setPixelColor(5, 0xFF0000);
  72. //CircuitPlayground.setPixelColor(6, 0x808000);
  73. //CircuitPlayground.setPixelColor(7, 0x00FF00);
  74. //CircuitPlayground.setPixelColor(8, 0x008080);
  75. //CircuitPlayground.setPixelColor(9, 0x0000FF);
  76. }
  77. } else { // loop backwards and decress
  78. for (int i = 10; i >= 0 ; i--) { // when the pressure is released, start from the highest number (9) and go to the lowest (0).
  79. CircuitPlayground.setPixelColor(i, 0, 0, 0);
  80. delay(50); // delay allows us to see the LEDs turn off individually
  81. }
  82. }
  83. }
RAW Paste Data