fbpx

www.huvitera.ee/arduino-20M

3 potentiomeetrit, 3 LEDi. 

Kui 0 potentiomeetrit on õiges kohas, on tuled kustus.

Kui 1, siis punane. Kui 2, siis kollane. Kui 3, siis roheline ja relee avaneb.

				
					#include <Wire.h> // Include the Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal_I2C library for I2C LCD

const int potPin1 = A0; // Analog pin for potentiometer 1
const int potPin2 = A1; // Analog pin for potentiometer 2
const int potPin3 = A2; // Analog pin for potentiometer 3
const int redLED = 8; // Digital pin connected to red LED
const int yellowLED = 9; // Digital pin connected to yellow LED
const int greenLED = 10; // Digital pin connected to green LED

// Set the LCD address to 0x27 for a 16x2 LCD
// You may need to adjust this address based on your LCD module
LiquidCrystal_I2C lcd(0x27, 16, 2); // Specify the address and LCD size, and also the SDA and SCL pins

bool prevPot1InRange = false;
bool prevPot2InRange = false;
bool prevPot3InRange = false;

void setup() {
  pinMode(redLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);

  // Initialize LCD with columns and rows, and specify SDA and SCL pins
  lcd.init();
  lcd.backlight(); // Turn on backlight
  lcd.clear(); // Clear the LCD screen

  // Initialize serial communication
  Serial.begin(9600);
}

void loop() {
  int potValue1 = analogRead(potPin1);
  int potValue2 = analogRead(potPin2);
  int potValue3 = analogRead(potPin3);

  // Print potentiometer values to Serial Monitor
  Serial.print("Pot1: ");
  Serial.print(potValue1);
  Serial.print("   Pot2: ");
  Serial.print(potValue2);
  Serial.print("   Pot3: ");
  Serial.println(potValue3);

  // Check if potentiometer values fall within specified ranges
  bool pot1InRange = (potValue1 >= 150 && potValue1 <= 200);
  bool pot2InRange = (potValue2 >= 450 && potValue2 <= 550);
  bool pot3InRange = (potValue3 >= 880 && potValue3 <= 980);

  // Clear the LCD screen
  lcd.clear();

  // Print "HuviTERA" on LCD after correct positions of 3 potentiometers
  if (pot1InRange && pot2InRange && pot3InRange) {
    digitalWrite(greenLED, HIGH); // Activate green LED
    lcd.setCursor(0, 0); // Set cursor to first column, first row
    lcd.print("HuviTERA"); // Print on LCD
  } else if (pot1InRange && pot2InRange && !pot3InRange || pot1InRange && !pot2InRange && pot3InRange || !pot1InRange && pot2InRange && pot3InRange) {
    digitalWrite(yellowLED, HIGH); // Activate yellow LED
  } else if (pot1InRange || pot2InRange || pot3InRange) {
    digitalWrite(redLED, HIGH); // Activate red LED
  }

  // Check if the amount of correct range potentiometers changed
  if (pot1InRange != prevPot1InRange || pot2InRange != prevPot2InRange || pot3InRange != prevPot3InRange) {
    digitalWrite(redLED, LOW); // Turn off red LED
    digitalWrite(yellowLED, LOW); // Turn off yellow LED
    digitalWrite(greenLED, LOW); // Turn off green LED
  }

  // Update previous potentiometer range states
  prevPot1InRange = pot1InRange;
  prevPot2InRange = pot2InRange;
  prevPot3InRange = pot3InRange;

  delay(100); // Delay for stability
}

				
			

NB! Lisa auku nr 7 roheline LED!

Ülesanne 1: nupupusle. Peab nuppe vajutama õiges järjekorras, et mõistatus lahendada.
Pärast ühendamist kopeeri kood ning katseta. 

1) Ühenda skeemi 3 nuppu ja 5 LEDi, värv pole oluline.

2) Lisa juurde auku nr 7 roheline LED!

3) Kopeeri allolev kood

LEDi pikem klemm on numbriga klemmi poole mitte Groundi poole!
Takisti tugevus ei ole nii määrav.

4) Kui programm töötab, siis muuda rida 27 NumSteps sulgudes numbrid ära, et teha oma mõistatus. Mõtle juurde mingi reaalne tegevus põgenemistoas, mida osaleja peab tegema, et õige kombinatsioon leida. 
0 tähendab 1. nupp, 1 tähendab 2. nupp, 2 tähendab 3. nupp.

				
					/**


* Input Sequence
* 
* Alastair Aitchison (c) 2017
* 
* This puzzle requires the user to enter a set of inputs in the correct order. 
* i.e. press a sequence of buttons. 
* The number of inputs and the length of sequence are customisable.
*/

// DEFINES
// Provides debugging information over serial connection
#define DEBUG

// CONSTANTS
// Define the number of possible "inputs" - i.e. the number of switches, buttons etc. that the player can press
const byte numInputs = 3;
// What pins are those buttons connected to? (other wire should go to ground)
const byte inputPins[numInputs] = {4, 3, 2};
// Define the number of steps in the sequence that the player must follow
const byte numSteps = 5;
// The correct sequence of inputs required to solve the puzzle.
const byte steps[numSteps] = {0, 2, 1, 1, 0}; // i.e. press button #2 once, then button #3 twice, then button #0, then button #1.
// These pins are used to light up LEDs to show the player's progress, so one output pin per step in the puzzle.
const byte ledPins[numSteps] = {12, 11, 10, 9, 8};
// This pin will be driven LOW to release a lock when puzzle is solved
const byte lockPin = A0;
int SuccessPin = 7; // define digital pin 10.

// GLOBALS
// Assume the default state of each switch is HIGH. 
bool lastInputState[] = {HIGH, HIGH, HIGH, HIGH};
// What step of the sequence is the player currently on?
int currentStep = 0;

// Switches can "bounce" when they open/close, generating a flurry of false readings
// To prevent this, we'll add a short delay between each time an input value
// is read.
// The last time the input switch was toggled
unsigned long lastDebounceTime = 0;
// The amount of time (in ms) to wait before reading again 
unsigned long debounceDelay = 50;

// Setup function runs once when first starting (or resetting) the board
void setup() {

  // Initialise the input pins that have switches attached
  for(int i=0; i< numInputs; i++){
    pinMode(inputPins[i], INPUT_PULLUP);
  }
  
  // Initialise the LED pins that show progress through the sequence 
  for(int i=0; i< numSteps; i++){
    pinMode(ledPins[i], OUTPUT);
  }

  // Set the lock pin as output and secure the lock
  pinMode(lockPin, OUTPUT);
  digitalWrite(lockPin, HIGH);

  #ifdef DEBUG
    // Open communications on serial port
    Serial.begin(9600);
    Serial.println(F("Serial communication started"));
  #endif
}

// The main program loop runs continuously
void loop() {

  // Check that we've waited at least "debounceDelay" since last input
  if ( (millis() - lastDebounceTime) > debounceDelay) {
  
    // Loop through all the inputs
    for(int i=0; i<numInputs; i++){
      int currentInputState = digitalRead(inputPins[i]);

      // If the input has changed, reset the debounce timer
      if(currentInputState != lastInputState[i]) {
        lastDebounceTime = millis();    
      }
        
      // If the input is currently being pressed (and wasn't before)
      // Note that since the input pins are configured as INPUT_PULLUP,
      // they read as LOW when pressed and HIGH when not.
      if(currentInputState == LOW && lastInputState[i] == HIGH) {
        // Was this the correct input for this step of the sequence?
        if(steps[currentStep] == i) {
          currentStep++;
          
          #ifdef DEBUG
            Serial.print(F("Correct input! Onto step #"));
            Serial.println(currentStep);
          #endif
        }
        // Incorrect input!
        else {
          currentStep = 0;
          Serial.println(F("Incorrect input! Back to the beginning!"));
        }
      }
      
      // Update the stored value for this input
      lastInputState[i] = currentInputState;
    }
  }

  // Check whether the puzzle has been solved
  if(currentStep == numSteps){
    onSolve();
  }

  // Turn on the number of LEDs corresponding to the current step
  for(int i=0; i<numSteps; i++){
    digitalWrite(ledPins[i], (i < currentStep ? HIGH : LOW));
  }
}

// Takes action when the puzzle becomes solved
void onSolve(){

  #ifdef DEBUG
    // Print a message
    Serial.println(F("Mõistatus lahendatud!"));
  #endif 

  // Release the lock
  digitalWrite(lockPin, LOW);
  digitalWrite(SuccessPin, HIGH);


  // Loop forever
  while(true){

    // Flash LEDs
    for(int i=0; i<numSteps; i++){
      digitalWrite(ledPins[i], HIGH);
      delay(100);  
    }
    delay(100);  
    for(int i=0; i<numSteps; i++){
      digitalWrite(ledPins[i], LOW);
      delay(100);  
    }
  }
}

				
			

NR2 – Potentsiomeetri ja RGB LED Mõistatus
1) Ühenda 3 potentsiomeetrit, augud A1, A2, A3.

2) Lisa RGB LED

3) Kopeeri allolev kood

4) Muuda potentsiomeetri väärtusi, et mõistatus keerulisemaks teha. Mõtle juurde reaalne olukord põgenemistoas, kus sellist mõistatust saaks kasutada.

5) Arvesta, et tegelikult saaks juurde lisada ka nupu, mida vajutada, kui keeratavad nupud on õiges asendis. Vastasel juhul ei juhtuks veel midagi mõistatuses. 

				
					int potpin1=1;
int potpin2=2;
int potpin3=3;
int redpin = 11; //select the pin for the red LED
int greenpin =10;// select the pin for the green LED
int bluepin =9; // select the pin for the blue LED
int inpin=7;// initialize pin 7
int val=0;// Temporarily store variables' value from the sensor
int val2=0;
int val1=0;


void setup()
{
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(inpin,INPUT);
Serial.begin(9600);
}

void loop()
{
  //ülesanne ise vahetada õiget vastust, jälgi serial monitori!
val=analogRead(potpin1)+analogRead(potpin2)+analogRead(potpin3);// read the analog value from the sensor and assign it to val
Serial.println(val);// display value of val
 if (val <= 1200){
 ;
  analogWrite(redpin, HIGH);
  analogWrite(bluepin, LOW);  
  analogWrite(greenpin, LOW); 
  }
if (val >= 1200 and val <=2500)
{
  analogWrite(redpin, LOW);
  analogWrite(bluepin, HIGH);  
  analogWrite(greenpin, LOW);  
  }
if (val >= 2500)
{
  analogWrite(redpin, LOW);
  analogWrite(bluepin, LOW);  
  analogWrite(greenpin, HIGH);   
  }
delay(100);
}