กลับหน้าหลัก
views
Arduino Basics: How to Read and Check Data Values for Conditional Control
Last updated on

Arduino Basics: How to Read and Check Data Values for Conditional Control


Arduino Basics: How to Read and Check Data Values for Conditional Control

When writing Arduino programs for more complex projects, you often need to work with multiple data values. This could be checking passwords, comparing sensor readings, or searching for data that matches certain conditions.

This article teaches techniques for reading and checking data values with Arduino, which is an essential foundation for various projects.

Diagram showing data flow in Arduino from input reception, validation against stored data, and output control

What is Data in the Arduino Context

Arduino can store multiple values in Array variables, which are like tables with rows and columns. You can take values from Arrays to check if they meet your conditions.

// Example: Array storing 4 allowed passwords
const int ALLOWED_PASSWORDS[] = {1234, 5678, 9012, 3456};
const int PASSWORD_COUNT = 4;

// Example: Array storing usernames (using String)
String allowedUsers[] = {"admin", "user1", "user2", "guest"};

[image: Arduino code showing Array declaration for storing passwords and usernames]

How to Check Values with Loops and Conditionals

Once you have data in an Array, the next step is to loop through it to check if your input matches any stored value.

bool checkPassword(int inputPassword) {
  for (int i = 0; i < PASSWORD_COUNT; i++) {
    if (ALLOWED_PASSWORDS[i] == inputPassword) {
      return true;  // Found matching password
    }
  }
  return false;  // No matching password found
}

void setup() {
  Serial.begin(9600);
  
  // Test the verification
  int testCode = 5678;
  
  if (checkPassword(testCode)) {
    Serial.println("Access Granted");
  } else {
    Serial.println("Access Denied");
  }
}

void loop() {
  // No repeated operation
}

Code explanation:

  • The checkPassword() function takes the input password
  • The for loop checks every value in the Array
  • If a match is found, it returns true immediately
  • If no match is found after checking all values, it returns false

[image: Arduino code showing password checking function with for loop and if condition]

Practical Project Applications

This technique can be applied in various ways:

ProjectData in ArrayVerification Method
Keypad Login System4-digit passwordsCompare with Array
RFID Card CheckRegistered card IDsSearch in Array
Code-based Light ControlAllowed codesCheck condition

Example: Basic Login System

const String VALID_USERS[] = {"admin", "engineer", "technician"};
const int VALID_CODES[] = {1111, 2222, 3333};
const int USER_COUNT = 3;

bool authenticateUser(String username, int code) {
  for (int i = 0; i < USER_COUNT; i++) {
    if (VALID_USERS[i] == username && VALID_CODES[i] == code) {
      return true;
    }
  }
  return false;
}

void setup() {
  Serial.begin(9600);
  
  // Test user authentication
  if (authenticateUser("admin", 1111)) {
    Serial.println("Welcome, admin!");
    // Enable system
  }
}

void loop() {
  // Wait for additional input
}

Points to customize further:

  • Add input from Keypad or Serial Monitor
  • Add counter for failed password attempts
  • Add delay after failed attempts

Limitations to Know

Arduino has limited RAM (about 2 KB for Uno), so:

  • Avoid creating unnecessarily large Arrays
  • Use const for data that won’t change to save RAM
  • If you need more data, consider using an SD Card Module instead

Summary

Reading and checking data values is an important foundation for more complex Arduino programming. Using Arrays to store data and for loops with if-else statements to check conditions, this technique can be applied to login systems, access control, or processing data from multiple sensors.

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

จ้างทำโปรเจคเลย

ประเมินราคาอัตโนมัติ + Reference Code

ขอให้ AI ประเมินราคาโปรเจคนี้

กรอกข้อมูลให้ครบ ระบบจะสร้างรหัสอ้างอิงและประเมินราคา/ระยะเวลาคร่าว ๆ จากรายละเอียดงาน แล้วให้กด Add LINE พร้อมพิมพ์รหัสนี้เพื่อคุยต่อ

คำถามให้ AI ประเมินแม่นขึ้น

หลังส่งฟอร์ม ระบบจะโชว์ Reference Code ให้ copy แล้วกด Add LINE เพื่อคุยต่อ ข้อมูลส่วนตัวจะไม่ถูกส่งเข้า GA4

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

กำลังโหลดรีวิว...