Skip to content

Instantly share code, notes, and snippets.

@Mackaber
Created September 26, 2025 04:47
Show Gist options
  • Select an option

  • Save Mackaber/ba0ab40cd5df6b8c9236ab1605933ae4 to your computer and use it in GitHub Desktop.

Select an option

Save Mackaber/ba0ab40cd5df6b8c9236ab1605933ae4 to your computer and use it in GitHub Desktop.
/*
Arduino C++ solutions for conditional logic exercises.
This version is updated to only use int, String, and float data types.
Upload this code to your Arduino and open the Serial Monitor (baud rate 9600) to see the output.
*/
void setup() {
// Initialize serial communication to print output
Serial.begin(9600);
delay(1000); // Wait a moment for the serial monitor to connect
// --- Exercise 1: Multiple Conditions ---
// Check if a temperature is too high (above 30°C) AND a humidity level is high (above 70%).
Serial.println("--- Exercise 1 ---");
int temperature = 35; // Example temperature in °C
int humidity = 80; // Example humidity in %
// In C++, the '&&' operator is used for a logical AND.
if (temperature > 30 && humidity > 70) {
Serial.println("Warning: High heat and humidity levels detected. Stay hydrated and cool.");
} else {
Serial.println("Weather conditions are moderate.");
}
Serial.println("\n====================\n");
// --- Exercise 2: Using Logical Operators (OR) ---
// Check if a user is either under 18 years old or over 65 years old for a discount.
Serial.println("--- Exercise 2 ---");
int age = 16; // Example age
// In C++, the '||' operator is used for a logical OR.
if (age < 18 || age > 65) {
Serial.println("You are eligible for a discount.");
} else {
Serial.println("Standard pricing applies.");
}
Serial.println("\n====================\n");
// --- Exercise 3: NOT Operator (using int) ---
// Check if a light is OFF. 0 represents OFF, 1 represents ON.
Serial.println("--- Exercise 3 ---");
int is_light_on = 0; // 0 for false (OFF), 1 for true (ON)
// We check if the integer value is 0.
if (is_light_on == 0) {
Serial.println("The light is off.");
} else {
Serial.println("The light is on.");
}
Serial.println("\n====================\n");
// --- Exercise 4: Combining Conditions ---
// Check if a number is both even AND greater than 10.
Serial.println("--- Exercise 4 ---");
int number = 12; // Example number
// The modulo operator (%) is the same in C++ as in Python.
if (number > 10 && number % 2 == 0) {
Serial.println("Valid number.");
} else {
Serial.println("Invalid number.");
}
Serial.println("\n====================\n");
// --- Exercise 5: Simple Authentication ---
// Check if a name is "Sam" AND the password is 1234.
Serial.println("--- Exercise 5 ---");
String user_name = "Sam"; // Use the String object for easier comparison
int user_password = 1234;
// String comparison works as expected with the '==' operator for the String object.
if (user_name == "Sam" && user_password == 1234) {
Serial.println("Access granted.");
} else {
Serial.println("Access denied.");
}
Serial.println("\n====================\n");
// --- Exercise 6: Nested Conditionals ---
// Check for a username, and if it's correct, then check the password.
Serial.println("--- Exercise 6 ---");
String correct_user = "admin";
String entered_user = "admin";
int correct_pass = 9999;
int entered_pass = 1234;
if (entered_user == correct_user) {
// This 'if' is NESTED inside the first one. It only runs if the username is correct.
if (entered_pass == correct_pass) {
Serial.println("Login successful!");
} else {
Serial.println("Incorrect password.");
}
} else {
Serial.println("Incorrect username.");
}
Serial.println("\n====================\n");
// --- Exercise 7: Number Ranges (Grading) ---
// Check which grade a score falls into using else if.
Serial.println("--- Exercise 7 ---");
int score = 85;
if (score >= 90 && score <= 100) {
Serial.println("Grade: A");
} else if (score >= 80 && score < 90) {
Serial.println("Grade: B");
} else if (score >= 70 && score < 80) {
Serial.println("Grade: C");
} else if (score >= 60 && score < 70) {
Serial.println("Grade: D");
} else {
Serial.println("Grade: F");
}
Serial.println("\n====================\n");
// --- Exercise 8: Combining Ranges and Booleans (using int) ---
// Determine ticket price based on age and student status.
Serial.println("--- Exercise 8 ---");
int person_age = 20;
int is_student = 1; // 0 for false, 1 for true
if (person_age < 5) {
Serial.println("Ticket Price: Free");
} else if (person_age >= 5 && person_age <= 17) {
Serial.println("Ticket Price: $10 (Child)");
} else { // Age is 18 or older
if (is_student == 1) {
Serial.println("Ticket Price: $12 (Student)");
} else {
Serial.println("Ticket Price: $15 (Adult)");
}
}
Serial.println("\n====================\n");
// --- Exercise 9: Complex Nested Logic (Climate Control with float) ---
// Activate different systems based on temperature and humidity ranges.
Serial.println("--- Exercise 9 ---");
float current_temp = 29.5; // Using a float for more precision
int current_humidity = 75;
if (current_temp > 28.0) {
Serial.print("Temperature is high. ");
if (current_humidity > 60) {
Serial.println("Activating AC and Dehumidifier.");
} else {
Serial.println("Activating AC only.");
}
} else if (current_temp >= 20.0 && current_temp <= 28.0) {
Serial.println("Temperature is ideal. Maintaining current settings.");
} else { // current_temp is less than 20.0
Serial.println("Temperature is low. Activating Heater.");
}
Serial.println("\n====================\n");
// --- Exercise 10: Vending Machine Selection ---
// Based on a selection code (e.g., "A1", "B2"), print the selected item.
Serial.println("--- Exercise 10 ---");
String selection = "B2";
if (selection == "A1") {
Serial.println("You get: Potato Chips.");
} else if (selection == "A2") {
Serial.println("You get: Pretzels.");
} else if (selection == "B1") {
Serial.println("You get: Soda.");
} else if (selection == "B2") {
Serial.println("You get: Water.");
} else {
Serial.println("Invalid selection.");
}
Serial.println("\n====================\n");
}
void loop() {
// The code in setup() only runs once. Nothing needs to run repeatedly,
// so the loop() function is empty.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment