Saturday, November 9, 2024

Photoresistor, Temperature Sensor, and Potentiometer!

I found out that our library had Arduino kits available for checkout and they had photoresistors in them!  It gave me just enough hope to work on putting together the same logic for the temperature sensor and then throwing in the previous work from the potentiometer.

Got it all working in Tinkercad, so that was Step 1.

Unfortunately, in the real world, I couldn't really do much with changing the temperature sensor - it didn't seem to respond to me touching it to try to make it warmer, but it did work in the Tinker model, and using the photoresistor and potentiometer worked just fine!



I didn't see any other sensors in the kit that I could mess with - I thought about maybe something that could sense sound - maybe that's the Piezo piece?  I'm not sure and I didn't have enough time to do more on it, so I just left it with what I had.  I really did enjoy getting all the colors to come out - I wish that my phone camera did a better job of capturing it.  This has made me realize the importance of having a good camera in the makerspace for the students to use!

And here's the code!

const int lightInputPin = 0;
const int tempInputPin = 1;
const int potInputPin = 2;

const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;

// We'll also set up some global variables for the light level:
// I initially set them at 200 and 300, but will allow them to
//   change based on what happens in the system
int lightLevel, lightLow = 200, lightHigh = 300;

int tempLevel, tempLow = 200, tempHigh = 100;

int potLevel = 0;

void setup(){
 
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  Serial.begin(9600);
}


void loop(){

  lightLoop();
  tempLoop();
  potLoop();

 
} // end main loop


void lightLoop(){
  int range = lightHigh - lightLow;

  lightLevel = analogRead(lightInputPin);
 
  if (lightLevel < lightLow) {
    lightLow = lightLevel;
  }
 
  if (lightLevel > lightHigh) {
    lightHigh = lightLevel;
  }

  // You don't need to cast as a float in tinkercad
  //   but when you move to Arduino IDE it's required
  lightLevel = (float) (lightLevel - lightLow) * 255 / range;
 
  analogWrite(BLUE_PIN, lightLevel);
} // end lightLoop


void tempLoop(){
   int range = tempHigh - tempLow;
 
  tempLevel = analogRead(tempInputPin);

  Serial.println("OG TempLevel");
  Serial.println(tempLevel);

  Serial.println("tempLow");
  Serial.println(tempLow);

  Serial.println("tempHigh");
  Serial.println(tempHigh);

  if (tempLevel < tempLow) {
    tempLow = tempLevel;
  }
 
  if (tempLevel > tempHigh) {
    tempHigh = tempLevel;
  }
 
  // You don't need to cast as a float in tinkercad
  //   but when you move to Arduino IDE it's required
  tempLevel = (float) (tempLevel - tempLow) * 255 / range;

  Serial.println("Adjusted TempLevel");
  Serial.println(tempLevel);

  analogWrite(GREEN_PIN, tempLevel);  
 
} // end temploop


void potLoop(){
  potLevel = analogRead(potInputPin);    
  potLevel = static_cast<int>((potLevel / 1024.0) * 255);
 
  analogWrite(RED_PIN, potLevel);
}

No comments:

Post a Comment

Nooooo, don't make me reflect on my learning!

 ... I just want to stick wires in breadboards and make froggies dance!!! Okay, but seriously, that stupid dancing frog was clearly my most ...