I just got an arduino and im trying to learn basics of coding by myself and help of internet, I just wanted to drive 2 different engines by 2 seperate IR sensors, i cannot seperate them. I know it's very basic stuff but i cannot understand.
`void loop() {
int value1 = analogRead(value_A0);
int value2 = analogRead(value_A1);
if (value1 < 50){
digitalWrite(A1A,HIGH);
digitalWrite(A1B,LOW);
}
else{
digitalWrite(A1A,LOW);
digitalWrite(A1B,LOW);
}
if (value2 < 50){
digitalWrite(B1A,HIGH);
digitalWrite(B1B,LOW);
}
else{
digitalWrite(B1A,LOW);
digitalWrite(B1B,LOW);
}
}
`
Thats the part where i screw up.
In this situation only one sensor is driving two of the motors in the same time.
I want to drive 2 different output with two different analog inputs seperately using if statements. I tried to create 2 if statements to avhieve that but still only one analog input drives the outputs. ( Sorry its hard to describe my problem because of the my lack of english)
basicly If value1 < 50 left motor goes else it stops
if value2 < 50 right motor goes and if its not it stops
I expecting to work both of them in the same time independently
maybe it's because the code is not complete
Example:
if (value1 < 50){
digitalWrite(A1A,HIGH);
digitalWrite(A1B,LOW);
digitalWrite(B1A,HIGH);
digitalWrite(B1B,LOW)
;
}
else{
digitalWrite(A1A,LOW);
digitalWrite(A1B,LOW);
digitalWrite(B1A,LOW);
digitalWrite(B1B,LOW
)
}
if (value2 < 50){
digitalWrite(B1A,LOW);
digitalWrite(B1B,LOW);
digitalWrite(A1A,HIGH);
digitalWrite(A1B,LOW)
;
}
else{
digitalWrite(B1A,LOW);
digitalWrite(B1B,LOW);
digitalWrite(A1A,LOW);
digitalWrite(A1B,LOW)
;
}
but this code it seems to me that there are 4 motors.... It would be easier to call motor 1 and motor 2
"basicly If value1 < 50 left motor goes else it stops
if value2 < 50 right motor goes and if its not it stops
I expecting to work both of them in the same time independently"
More simple code like this
const int motorPin1 = 9; // Pin to control motor 1
const int motorPin2 = 10; // Pin to control motor 2
int sensorValue; // Input value from some sensor
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
}
void loop() {
// Read the sensor value (change this based on how you're getting the value)
sensorValue = analogRead(A0);
if (sensorValue > 50) {
digitalWrite(motorPin1, HIGH); // Turn on motor 1
digitalWrite(motorPin2, HIGH); // Turn on motor 2
} else if (sensorValue < 50) {
digitalWrite(motorPin1, HIGH); // Turn on only motor 1
digitalWrite(motorPin2, LOW); // Turn off motor 2
} else {
digitalWrite(motorPin1, LOW); // Turn off both motors
digitalWrite(motorPin2, LOW);
}
}
*Dont copy and paste the code, I haven't tried it, it's just an idea, that can help you to write the code... See you
So A1A and B1A are the only control pins that change.
The code looks plausible, please post a schematic showing all the parts and how they are connected with particular attention to power - where it comes from and how it gets to the parts that need it.
value_A0 and value_A1 are both globals assigned no value. So they are both zero. So the analogReads that you hoped would be independent are not, they both look at analog zero.
Yes the code is not precise, I think In theory it shouldn't stop, since there are only 3 states 0 = LOW, < and > we would have an infinite loop. (normally I use sliders) meanwhile when I work with motors I have learned that the maximum of a motor the integer numerical value would be 255 (maximum power like a LED) ....., in the code I use 50 because that's what they mentioned in the question, but it is still a true/false value.... HIGH value for me... If the sensor read 50 < or > will always work like HIGH unless we get 0 = LOW however It's just a simple sample how he could start writing the code.
The problem would be with the value 50 alone, we should make a new line, we should create a new line like == if we have 50 make it HIGH/True or change the number 50 for 49 and use =< / =>
The motors are controlled with a digital signal written HIGH or LOW.
Presumably the sensor value that controls the on/off setting written to the motor returns values in some part of the 0..1023 that analogWrite()analogRead() returns. THX @anon56112670, good eye.
The OP can decide where is on and off in that range, and whether to include a dead zone as I have proposed.
And of course she has two motors or devices under control, so a simple approach is to duplicate the code as was done in the first offered sketch.
Which may have been fine except for the confusion between the pin and the value on the pin in the statements using analogRead().
It is true we can use the digital pins, we only have 2 values
Here is better code:
const int sensorPin = 3; // Pin to which the sensor is connected
const int motorPin1 = 9; // Pin to control motor 1
const int motorPin2 = 10; // Pin to control motor 2
int sensorValue; // Input value from the sensor
void setup() {
pinMode(sensorPin, INPUT); // Set sensor pin as input
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
}
void loop() {
// Read the sensor value using digitalRead (maybe it only read 0 1 , true or false HIGH or LOW)
sensorValue = digitalRead(sensorPin);
if (sensorValue == 0) {
digitalWrite(motorPin1, LOW); // Turn off both motors
digitalWrite(motorPin2, LOW);
} else if (sensorValue >= 50) {
digitalWrite(motorPin1, HIGH); // Turn on the motor 1 and 2
digitalWrite(motorPin2, HIGH);
} else {
digitalWrite(motorPin1, LOW); // Turn off motor 1
digitalWrite(motorPin2, HIGH); // Turn on motor 2
}
}
Maybe this code can work....
For try the sensor
const int sensorPin = A0; // Pin to which the sensor is connected
int sensorValue; // Input value from the sensor
void setup() {
pinMode(sensorPin, INPUT); // Set sensor pin as input
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
// Read the sensor value using analogRead
sensorValue = analogRead(sensorPin);
// Print the sensor value to the serial monitor
Serial.println(sensorValue);
delay(1000); // Delay for one second
}
Before you cop heat from everyone else, Let me give you a few pointers in the right direction.
You will no doubt hear this from others so i'll save them the trouble and i'll save you the headache
Rules for Posting
First Always , whatever your query is, Use the Search Bar in the
Top right Corner as a few things always happen.
Chances are , As a newbie your question has already been asked many times.
You need to show effort here before you receive help as everyone here is a volinteer.
Take time to appreciate that , Take time to understand that here we have talented
people, Technicians, Engineer's, PhD's and all sorts and they volunteer their solution
to you, so SHOWING EFFORT GETS YOU RESPECT and ATTENTION
Practice using the Utility Bar above the reply box
When replying to someone "Arrow 1" Points to the Reply bubble, Use it so we know what you're referring to
"Arrow 2" is for Bold and Italic
"Arrow 3" is for posting a link to another post on this site or another site.
Quick point on this, don't ever post a link to Github and say "you can find the code here"
or a picture or sketch and say to people "you can find it here"; it's not our job to run around for you, it's your job to bring the information to us to demonstrate the work
you put in
"Arrow 4" - Is for Quoting what someone said in a partial quote. You can also highlight
what they said and a quote bubble will appear automatically"
"Arrow 5" - When you post code Use this always, don't do what you did in thefirst post,
so NOW, i recommend you edit the first post, Copy and paste that code into
the code feature
Hit EDIT at the bottom of the post
Copy the code to be taken out
Hit the "CODE " button
Paste your code where it says put code here
"Arrow 6" Upload photos with this
OK so now When posting a post, Few simple rules
First research the topic in the search bar and spend at least 1 hour trying to come up with the solution
Make sure you're not asking for help for Homework that you should be doing, They don't like that herem Other option you can pay someone to just give you the solution
After that , Provided Details
The board you are Using
The computer and Operating system
A Schematic is necessary, if you don't know how, Draw it on a piece of paper so we can see what's going on
Then a somewhat detailed explanation of your problem
Do that and people will start to like you and help you more.
Now Re Your problem
As a newbie you are struggling right
RE :
Ok, so i can recommend this Arduino Programming Course
Seriously, it'll be the easiest thing you've done
Takes like 2 hours and after it you will be like......... Holy crap!
and then you'll instantly understand more about coding
Believe me.. for the amount of effort and time wasted that you would take here
over maybe 2 to 3 weeks to ask god knows how many questions
this course can put you on the right track so you have handle on things
now, we'll help you anyway, but.. do the course ok, You'll thank me and yourself