I am doing a project using sensors (LDR & Ultrasonic) and LEDs using the Arduino Software. I have managed to get the light bulb to work, however I would like to know on how to combine two different Arduino programs into one. Attached below are the two different programs
Program 1:
int ldr=A0;//Set A0(Analog Input) for LDR.
int value=0;
void setup() {
Serial.begin(9600);
pinMode(3,OUTPUT);
}
void loop() {
value=analogRead(ldr);//Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if(value<300)
{
digitalWrite(3,HIGH);//Makes the LED glow in Dark.
}
else
{
digitalWrite(3,LOW);//Turns the LED OFF in Light.
}
}
Program 2:
#define trigPin 13
#define echoPin 12
#define led 11
void setup()
{ Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
}
void loop()
{ long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 10)
{ digitalWrite(led,HIGH);
}
else {
digitalWrite(led,LOW);
}
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Thank you!
You can use a timer and each 500 milliseconds run loop2
The first loop run normaly but the second loop run each 500 milliseconds
#define trigPin 13
#define echoPin 12
#define led 11
#define DELAY 500
int ldr = A0; //Set A0(Analog Input) for LDR.
int value = 0;
long timer = millis(); // a timer for timing 500 milliseconds
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
value = analogRead(ldr); //Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if (value < 300)
{
digitalWrite(3, HIGH); //Makes the LED glow in Dark.
}
else
{
digitalWrite(3, LOW); //Turns the LED OFF in Light.
}
if (millis() - timer >= DELAY ) {
timer = millis();//reset timer
loop2();
}
}
void loop2() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10)
{ digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
Serial.print(distance);
Serial.println(" cm");
}
Related
So, I know it's probably SUPER simple, but I'm new to arduino and I'm just drawing a blank. I'm making a motion detector with an HC-SRO4 ultrasonic sensor. Right now I have it set so whenever it senses an object within 60 cm, it turns on an LED light, but when the object disappears, the LED turns off. What I would like to happen is that it would stay on until I press a button to reset it. Any help is really appreciated and i thank you in advance.
void setup() {
#define LED 8
#define trigPin 12
#define echoPin 13
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1
;if (distance >= 60 || distance <= 0){
Serial.println("no object detected");
digitalWrite(LED, LOW);}
else {
Serial.println("object detected");
digitalWrite(LED, HIGH);
}}
for this you have make low pushbutton pin while pressing.once the oobject came near the flag will set once the set it will move to while loop and run until the button pressed
#define LED 8
#define trigPin 12
#define echoPin 13
#define push_button 5
int flag=0;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT);
pinMode(push_button,INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if(digitalRead()==0)
{
flag=0;
}
if (distance >= 60 || distance <= 0){
Serial.println("no object detected");
digitalWrite(LED, LOW);}
else {
Serial.println("object detected");
flag=1;
while(flag=1)
{
digitalWrite(LED, HIGH);}
}}
at the moment I'm trying that my Rovers drives until he found an Object. Then he should go to the right, stops and making a photo. After this first tour, the Rover should drive around the object and should always make a photo.
The problem is that he goes first to the right but drives always forward without driving around.
I already ask in this group for help that he only moves and drives around but I would try for myself that he drives around but without success.
Here you can see my Arduino Code that I am using.
// Define SensorS pins
#define trigPin 15
#define echoPin 2
//Define SensorXL pins
#define trigPinXL 14
#define echoPinXL 13
//Define Raspberry Pin
#define RaspiPin 26
//Define Motor pins
#define motorIn3 16 //Input 3
#define motorIn1 17 //Input 1
#define motorIn4 18 //Input 4
#define motorIn2 19 //Input 2
// Defines variables
long duration;
int distance;
// Define ActivateDistance
const int activateDistance = 40;
const int activateDistance2 =40;
void setup()
{
// Sets the trigPin as an Output
pinMode(trigPin, OUTPUT);
pinMode(trigPinXL, OUTPUT);
// Sets the echoPin as an Input
pinMode(echoPin, INPUT);
pinMode(echoPinXL, INPUT);
// sets the Motorpins as outputs:
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(motorIn3, OUTPUT);
pinMode(motorIn4, OUTPUT);
//Sets Raspberry Pin as output
pinMode(RaspiPin, OUTPUT);
// Starts the serial communication
Serial.begin(9600);
}
void stop()
{
// stop motor without duration
Serial.println("STOP");
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, LOW);
}
void stopp(int duration)
{
// stop motor without duration
Serial.println("STOP");
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, LOW);
delay(duration);
stop();
}
void left(int duration)
{
//Motor goes to left
Serial.println("LEFT");
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, HIGH);
digitalWrite(motorIn3, HIGH);
digitalWrite(motorIn4, LOW);
delay(duration);
stop();
}
void right(int duration)
{
//Motor goes to left
Serial.println("RIGHT");
digitalWrite(motorIn1, HIGH);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, HIGH);
delay(duration);
stop();
}
void forward(int duration)
{
//Motor goes forward
Serial.println("FORWARD");
digitalWrite(motorIn2, HIGH);
digitalWrite(motorIn4, HIGH);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn1, LOW);
delay(duration);
stop();
}
long get_distance(void)
{
//get distance from sensor
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
return distance;
}
long get_distanceXL(void)
{
//get distance from sensor
// Clears the trigPin
digitalWrite(trigPinXL, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPinXL, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinXL, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPinXL, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
return distance;
}
int turn = 0;
void loop()
{
//Define Raspi Pin as Low
digitalWrite(RaspiPin, LOW);
// check sensor
if (get_distance() <= activateDistance)
{
// go right for 1 second
right(1000);
//stop for 2 seconds
stopp(2000);
//make a Photo
digitalWrite(RaspiPin, HIGH);
delay(100);
digitalWrite(RaspiPin, LOW);
while(turn<4)
{
if(get_distanceXL()>activateDistance2)
{
//go left for 1 second
left(1000);
forward(1000);
stopp(2000);
digitalWrite(RaspiPin, HIGH);
delay(100);
digitalWrite(RaspiPin, LOW);
forward(500);
turn = turn + 1;
}
else
//go forward for 1 second
forward(500);
stopp(2000);
digitalWrite(RaspiPin, HIGH);
delay(100);
digitalWrite(RaspiPin, LOW);
forward(500);
}
}
else
// go forward for 1 second
forward(1000);
}
I have a game that consist of 4 direction of movement (up down left and right)
using Arduino Nano and analog joystick, seems like code is right as check before other posts.
This is the Arduino code:
byte x_axis = A3;
byte y_axis = A1;
byte btn1 = 8;
byte btn2 = 9;
byte btn3 = 10;
byte btn4 = 11;
byte btn5 = 12;
byte led = 13;
void setup(){
pinMode(x_axis, INPUT);
pinMode(y_axis, INPUT);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(btn3, INPUT);
pinMode(btn4, INPUT);
pinMode(btn5, INPUT);
digitalWrite(btn1, HIGH);
digitalWrite(btn2, HIGH);
digitalWrite(btn3, HIGH);
digitalWrite(btn4, HIGH);
digitalWrite(btn5, HIGH);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
}
void loop(){
Int read_x = analogRead(x_axis);
int read_y = analogRead(y_axis);
if(read_x > 600){
Serial.println("R");
digitalWrite(led, HIGH);
}
if(read_x < 400){
Serial.println("L");
digitalWrite(led, HIGH);
}
if(read_y > 600){
Serial.println("D");
digitalWrite(led, HIGH);
}
if(read_y < 400){
Serial.println("U");
digitalWrite(led, HIGH);
}
if(digitalRead(btn1) == LOW){
Serial.println("1");
digitalWrite(led, HIGH);
}
if(digitalRead(btn2) == LOW){
Serial.println("2");
digitalWrite(led, HIGH);
}
if(digitalRead(btn3) == LOW){
Serial.println("3");
digitalWrite(led, HIGH);
}
if(digitalRead(btn4) == LOW){
Serial.println("4");
digitalWrite(led, HIGH);
}
if(digitalRead(btn5) == LOW){
Serial.println("5");
digitalWrite(led, HIGH);
}
delay(10);
digitalWrite(led, LOW);
}
But when I use serial monitor to check it, it non stop show me U and L even without touching the joystick.
How can I fix this problem?
I'm feeling generous, so you have some jitter issues in the code and you really could use some cleanup. This code compiles. Ok now when a joystick is 0,0 in x/y physically it jitters in code. One thing you could do is remap out the jitter to give a wider center. print the raw analog values to the serial monitor and then map them out to your 0 point with a little padding. reference: https://www.arduino.cc/reference/en/language/functions/math/map/
byte x_axis = A3;
byte y_axis = A1;
byte btn[] = {8, 9, 10, 11, 12}; // 2,3,4,5,6
byte stat[] = {1, 2, 3, 4, 5};
byte led = 13;
int dval = 50;
void setup() {
pinMode(x_axis, INPUT);
pinMode(y_axis, INPUT);
for (int i = 0; i < 5; i++) {
pinMode(btn[i], INPUT);
digitalWrite(btn[i], HIGH);
}
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
}
void loop() {
int read_x = analogRead(x_axis);
delay(dval);
int read_y = analogRead(y_axis);
delay(dval);
if (read_x > 600) {
Serial.println("R");
digitalWrite(led, HIGH);
}
if (read_x < 400) {
Serial.println("L");
digitalWrite(led, HIGH);
}
if (read_y > 600) {
Serial.println("D");
digitalWrite(led, HIGH);
}
if (read_y < 400) {
Serial.println("U");
digitalWrite(led, HIGH);
}
for (int i = 0; i < 5; i++) {
if (digitalRead(btn[i]) == LOW) {
Serial.println(stat[i]);
digitalWrite(led, HIGH);
}
}
delay(10);
digitalWrite(led, LOW);
}
Emad joon:
1- Check that the ground of your joy stick and the Vdd is connected to your arduino.
2- connect the x and y of the joy stick to the arduino analog inputs.
use this code as starter:
#define X_AXIS A1
#define Y_AXIS A3
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("X = ");
Serial.println(analogRead(X_AXIS);
Serial.print("Y = ");
Serial.println(analogRead(Y_AXIS);
delay(150);
}
See what values you get when your Joy Stick is at initial position. You can build up on this code for buttons.
Also, do not forget to use debouncing for your button readings:
if(digitalRead(Button1)==0){
delay(40);
if(digitalRead(Button1==0){
buttonPressed=true;
}
}
I just don't know how to describe this. It was working, but then I changed the value of distance = duration*0.034/2;. I then changed it back but it still gave me these errors. Is it the Arduino? (Bluno nano) Is it the code?
const int trigPin = 9;
const int echoPin = 10;
int duration;
int distance;
void setup(){
Serial.begin(9600);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
}
void loop(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
Serial.print("Distance: " + distance);
delay(500);
}
You can't concatenate c-strings with a + operator.
You should print each part separately.
Serial.print("Distance: ");
Serial.println(distance);
I don't know how I get inaccurate readings from HC-SR04 when I tried to observe its data.
I think I uploaded the program right and perfectly connected the wires to my arduino board (no loose connections/insulations/ etc.) and I think I provided enough power supply, 9v to my arduino uno board.
const int Trigger = 9;
const int Echo = 8;
long durationg, inches;
void setup()
{
pinMode(Trigger, OUTPUT);
pinMode(Echo, INPUT);
Serial.begin (9600);
}
void loop()
{
digitalWrite(Trigger, LOW);
delay(2);
digitalWrite(Trigger, HIGH);
delay(5);
digitalWrite(Trigger, LOW);
duration = pulseIn(Echo, HIGH);
inches = duration / 72 / 2;
Serial.print(inches);
Serial.print("in ,");
Serial.println();
delay(25);
}
Try this code:
const int trigPin = 7;
const int echoPin = 4;
long duration, distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
if (distance < 150) {
Serial.print(distance);
Serial.println(" cm");
}
if (distance > 150) {
Serial.println("OUT OF RANGE");
}
delay(20);
}