Hey, just to note out first, I’m an absolute beginner in Arduino so forgive me if I’m being too general on how I’ll interpret things. I started it recently for school and I have only learnt till Arithmetics and Logical operators, and recently I’m assigned to do a project from imagination about a “computational” project that likely involves use of math operators.
The thing is, I have no idea how to code the project I have in mind being an newbie in Arduino. I’m planning to make a project where if I assign some variables or numbers to be compared, they’ll either go to a TRUE or FALSE. Then, on the breadboard depending on whether or not the compared result of the two variables is wrong, will then let an LED lighten up. So technically, if the comparison is wrong it should light out an red LED and otherwise a green one. I also am planning to make it operate continuously till it gets the correct answer and lights up the proper LED, but I’m unsure how can I make this work.
Any tips or starting recommendations for me to get an idea on how I could code this would be a huge help since I’ve only started learning this month. Thanks!
If you know the basics of arithmetic and comparison in c++ language , you just have to write an algorithm -> it helps visualizing your project and it get a good structure .
Int pinRed= 6; // pin 6 used for red led
Int pinGreen= 5; // pin 5 used for green led
Setup ()
{
Serial.begin (9600); // communication speed
pinMode (pinRed , OUTPUT); //define either output or input
pinMode (pinGreen , OUTPUT);
Int Value 1 ; // set your variables
Int value 2 ;
}
Loop()
{
/* For this part check how to read values from Serial */
If (Value1==Value2 ) {
digitalWrite (pinRed ,LOW); // turn off Red led
digitalWrite (pinGreen ,HIGH); // Turn on green led
}
else {
digitalWrite (pinGreen,LOW);
digitalWrite (pinRed ,HIGH);
}
Delay (1000) ;
}
This is approximately how your code will look like , don't forget to include libraries
Hello, thanks for the help! I’m trying your code out but the LEDs don’t seem to light up for some reason in my breadboard. Here’s an image of how I circuited it on Tinkercad, but I tried beforehand on a real breadboard,
Could I be doing some small mistake?
Here’s also the code I did from your help, with slight changes:
const int pinRed = 6;
const int pinGreen = 5;
void setup ()
{
Serial.begin (9600);
pinMode (pinRed , OUTPUT);
pinMode (pinGreen , OUTPUT);
int a = -107;
int b = 36;
}
void loop()
{
if ( -107 > 36 ) {
digitalWrite (pinRed ,LOW);
digitalWrite (pinGreen ,HIGH);
}
else {
digitalWrite (pinGreen,LOW);
digitalWrite (pinRed ,HIGH);
}
delay (1000) ;
}
So far, the only issue is that the LED lights are not lighting up. I’m also thinking of what can I add onto the code you made to make it more functional to my preferences, thanks!
Do you want to compare Arduino generated random numbers to a number you enter?
I put some code together to do that. Here is the output when I entered 22.