independent blinking lights in Arduino - arduino

I need to write a program in Arduino that turns ON or OFF two LEDs independently depending on the data received via serial communication. For example, the first LED will turn on if the Arduino board receives a1,and it will turn OFF if a2 is received. but I require help on doing this. i havent started on this yet but i am trying to do it using 2 blinking light code. I am new to Arduino programming and i don't understand some concepts. but i can definitely use help on doing this.
int LEDred = 3;
int LEDgreen = 6;
void setup() {
pinMode (LEDred, OUTPUT);
pinMode (LEDgreen, OUTPUT);
}
void loop() {
digitalWrite (LEDred, HIGH);
digitalWrite (LEDgreen, LOW);
delay (1000);
digitalWrite (LEDred, LOW);
digitalWrite (LEDgreen, HIGH);
delay (1000);
}

Use 1 or 2 instead of a1 and a2. Because it's easier to process.Consider you send 1 via serial port. Read the signal by
byte val;
if(Serial.available()>0){
val=Serial.read();
}
Then use another if statement in order to blink red or green LED

Related

How do I make pwm signals with arduino ide without useing analogwrite?

Hi can someone help with crating a pwm(or very similar) signal without analogwrite on the Arduino IDE because analogwrite wont work on the esp32.
And without using a library.
I need it to take values from 0 to 255.
I was thinking to use the second core one the esp32 but I have to control more than one pin.
you can create PWM pulse with delay or millis function. for example:
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delayMicroseconds(100);
digitalWrite(13, LOW);
delayMicroseconds(1000 - 100);
}
when you change the delay value, you can change duty-cycle level!

Arduino is freezing even with watchdog enabled

I have made a hardware development based on an ATmega328P and programmed with the Arduino IDE.
The board has one relay output that switchs an AC load.
From time to time, the microcontroller is restarted when the load is switched ON or OFF. No mystery so far. It is probably some EMI interference that is causing the reboot.
BUT, sometimes the microcontroller freezes completely. I canĀ“t figure out why, as I have enabled the watchdog timer. There shouldn't be any freezing. As far as I know, the watchdog timer should restart the microcontroller after 2 seconds.
I would need help understanding WHY I am getting this behaviour and of course, if there could be any software fix.
This is a simple code to show this behaviour. I already tried to change some fuses configurations (brownout, wdton, etc) but no luck so far.
Any help would be much appreciated
Thanks in advance
#include <avr/wdt.h>
#define R0 3
#define R1 4
#define R2 5
unsigned int delayTime = 200;
unsigned int counter = 0;
//--------------------------------------------------------------------
void setup() {
MCUSR = 0;
wdt_disable();
Serial.begin (9600);
delay (1000);
Serial.println ("********************RESTARTING*****************");
pinMode (R0, OUTPUT);
pinMode (R1, OUTPUT);
pinMode (R2, OUTPUT);
delay (2000);
wdt_enable (WDTO_2S);
}
//--------------------------------------------------------------------
void loop() {
wdt_reset();
digitalWrite (R1, HIGH);
counter++;
Serial.print ("R1 activated, counter = "); Serial.println (counter);
delay (delayTime);
digitalWrite (R1, LOW);
Serial.print ("R1 deactivated, counter = "); Serial.println (counter);
delay (delayTime);
}
You not have enabled watchdog from reset for three second. I recomend use WDR activation by fuse bit WDTON, not by sw after three second.
void setup() {
MCUSR = 0;
wdt_disable(); //WDR disabled
Serial.begin (9600);
delay (1000); //one second delay
Serial.println ("********************RESTARTING*****************");
pinMode (R0, OUTPUT);
pinMode (R1, OUTPUT);
pinMode (R2, OUTPUT);
delay (2000); //two second delay
wdt_enable (WDTO_2S);

How to make arduino go in two direction using h-bridge?

I am tring to build a line follower robot, i have an h-bridge and arduino to control the direction and speed.
I burnt this code to my arduino board, but the robot just go forward and never go on the oppsite direction:
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;
void setup()
{
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void demoOne()
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 200);
// turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 200);
delay(2000);
// now change motor directions
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop()
{
demoOne();
delay(1000);
demoTwo();
delay(1000);
}
How to adjust the code to make it go for both directions?
UPDATE:
I am using h bridge l298n and four dc motors "2 right and 2 left", powering it with two 9v batteries and powering the arduino uno with 5v which i get from the h bridge based on this article:
https://hackerstore.nl/PDFs/Tutorial298.pdf
enA, in1, and in2 for the two right motors
enB, in3, and in4 for the two left motors
Your code looks perfectly fine, make sure your hardware is working properly and that everything is connected properly, also make sure that you are not using the 5V from your Arduino to power the motors since the small voltage regulator on the Arduino board is not capable of handling high currents such as 500mA (Unless you replace it with a bigger regulator and give it a big heatsink), if you are powering the arduino and motors from USB make sure your USB port can handle the power required by the two motors, oh and make sure pins 10 and 5 are PWM pins
Also using a dedicated motor library may help to simplify your code, I wrote you one here:
#include <Arduino.h>
#include <stdbool.h>
#define enA 10
#define enB 5
#define i1 8
#define i2 9
#define i3 6
#define i4 7
void initMotors(){
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(i1, OUTPUT);
pinMode(i2, OUTPUT);
pinMode(i3, OUTPUT);
pinMode(i4, OUTPUT);
}
void motor1Stop(){
digitalWrite(i1, LOW);
digitalWrite(i2, LOW);
}
void motor2Stop(){
digitalWrite(i3, LOW);
digitalWrite(i4, LOW);
}
void motorsEnabled(bool en){
digitalWrite(enA, en);
digitalWrite(enB, en);
}
void enableMotors(){
motorsEnabled(HIGH);
}
void motor1Forward(){
digitalWrite(i1, HIGH);
digitalWrite(i2, LOW);
}
void motor1Backward(){
digitalWrite(i1, LOW);
digitalWrite(i2, HIGH);
}
void motor2Forward(){
digitalWrite(i3, LOW);
digitalWrite(i4, HIGH);
}
void motor2Backward(){
digitalWrite(i3, HIGH);
digitalWrite(i4, LOW);
}
To include this just make a new tab and name it whatever you want (as long as there are no spaces and it ends in .h), then add #include "name.h" to the beginning of your code, this will help you to reduce future bugs
For speed control you can add this:
void setMotorSpeeds(int motor1, int motor2){
analogWrite(enA, motor1);
analogWrite(enB, motor2);
}
Your code is fine, so you will need to identify the problem by "divide and conquer"
Connect a single motor to each channel, and forget the arduino part for now.
Connect the L298 power supply. If you read the instructions you will see that you can use the integrated 5V power regulator, or you can supply an external 5V. If using a different power supply for 5V and Motors make sure you connect both GND together otherwise it won't work.
Make sure you have the driver properly connected to power supply, take two cables and use them to connect in1 to GND, in2 to 5V and enA to 5V (without the arduino, just manually connect them with a cable). One of the motors should turn one side.
Now connect in1 to 5V and in2 to GND (keeping enA connected to 5V), the motor should turn to the other side.
If this does not happen then you are not properly connecting the L298 or it is damaged.
If it works then the problem comes from your arduino. Connect 3 LEDs, one in the pin9, one in the pin 8 and one in the pin 10 (which are the pins you connect to in1 and in2 and enA).
Run your program and you should see the LEDS turn on and off accordingly.
If not, your arduino has a problem in some pins (maybe you short circuited them?)
If both experiments work there is absolutely no reason for them to not work together, so start small, divide your problem into pieces and identify it.
Good luck.

Arduino Uno DC-Motor Controller concept clarification

I just started learning Arduino. I thought about going hands on with a Robot which I want to get some autonomy in. I got stuck trying to understand the motor control for the DC motor. I've tried to look up resources but can't find one that adequately explains my problem.
I can't understand (being a comp sci undergrad) how or what I am doing wrong writing the code for the motor.
My code just for running the motors for a test is :
// MotorA
int dir1PinA = 7;
int dir2PinA = 5;
int speedPinA = 6;
//motor B
int dir1PinB = 4;
int dir2PinB = 2;
int speedPinB = 3;
void setup() {
//Serial.begin(9600);
pinMode (dir1PinA, OUTPUT);
pinMode (dir2PinA, OUTPUT);
pinMode (speedPinA,OUTPUT);
pinMode (dir1PinB, OUTPUT);
pinMode (dir2PinB, OUTPUT);
pinMode(speedPinB, OUTPUT);
// put your setup code here, to run once:
}
void loop() {
analogWrite(speedPinA, 500);
analogWrite(speedPinB, 500);
digitalWrite (dir1PinA, LOW);
digitalWrite (dir2PinA, HIGH);
digitalWrite (dir1PinB,HIGH);
digitalWrite (dir2PinB, LOW);
// put your main code here, to run repeatedly:
}
I am just trying to run the motor at a certain speed to no avail. I found a sample code that uses the same method to control the motor but has just an extra loop and variable speed.
The test code which works is :
//Motor A
int dir1PinA = 7;
int dir2PinA = 5;
int speedPinA = 6;
//motor B
int dir1PinB = 4;
int dir2PinB = 2;
int speedPinB = 3;
unsigned long time;
int speed;
int dir;
void setup ()
{
pinMode (dir1PinA, OUTPUT);
pinMode (dir2PinA, OUTPUT);
pinMode (speedPinA, OUTPUT);
pinMode (dir1PinB, OUTPUT);
pinMode (dir2PinB, OUTPUT);
pinMode (speedPinB, OUTPUT);
time = millis ();
speed = 0;
dir = 1;
}
void loop ()
{
analogWrite (speedPinA, speed);
analogWrite (speedPinB, 555 - speed);
// set direction
if (1 == dir)
{
digitalWrite (dir1PinA , LOW);
digitalWrite (dir2PinA, HIGH);
digitalWrite (dir1PinB, HIGH);
digitalWrite (dir2PinB, LOW);
}
else
{
digitalWrite (dir1PinA, HIGH);
digitalWrite (dir2PinA, LOW);
digitalWrite (dir1PinB, LOW);
digitalWrite (dir2PinB, HIGH);
}
if (millis () - time> 5000)
{
time = millis ();
speed += 20;
if (speed> 555) {speed = 0;}
if (1 == dir)
{dir = 0;}
else
{dir = 1;}
}
}
Now I understand there are loops and extra variables but theoretically they shouldn't be required to just run a motor at a certain speed. My question is that What am I missing from the top sketch that is in the second one but is absolutely critical to the running of the motor?
I want to apologize in advance if this is a noob moment but I tried and can't find any logic lol.
In the "working" Arduino code ( c++ , yes it's compiled by a c++ compiler ) one of your both motors (B) starts immediately, and the other one 5 sec after reset (or even later)?
And the immediately started motor changes direction after 5 sec?
Else, try small modifications to the working sample, towards your simplification, to get some more understanding.
E.g. first remove the "dir" change and "else" part
Okay first I will debug your code, I will make a some assumptions about your hardware since you didn't post any photos or schematics, finally I will provide a working solution.
Your Code:
So my first assumption is that your Motor has 3 pins because you're toggling two between high and low, presumably to reverse current, and then one to set the speed.
As I am guessing you know, in order to get a motor to reverse you need to flip the direction the current is going. Setting these pins high and low won't actually reverse the current. Also, the Arduino doesn't really have enough output current to drive a DC motor. You can probably slowly run one. If you want to do more you'll need an external power source.
Drive a DC motor in one direction:
This is probably the easiest one to do. You just need a transistor, diode and resistor. Here is a tutorial
Drive up to four DC motors in both directions
If you want to drive your DC motor in two directions then you need to flip the direction the current is going. This seems complex at first until you realize they've already built something for you to do this exact thing. It's called a Motor Control. It's just a shield with a few H-Bridges on it. They're pretty cheap.
I like AdaFruit's. I have a couple and I built a robot with it not to long ago.
Tutorial on how to use the Adafruit MotorShield

Arduino Ultra Sonic Sensor always returns 0

I am doing a basic project in Arduino UNO connecting an Ultra Sonic sensor (HC-SR04) which should print in the serial monitor the distance of the closest object but it always print 0.
This is my code:
long distance;
long time;
void setup(){
Serial.begin(9600);
pinMode(4, OUTPUT);
pinMode(2, INPUT);
}
void loop(){
digitalWrite(2,LOW);
delayMicroseconds(5);
digitalWrite(2, HIGH);
delayMicroseconds(10);
time = pulseIn(4, HIGH);
distance = int(0.017*time);
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm.");
delay(1000);
}
And this is the breadboard:
The primary issue that I see is that your code doesn't match your wiring diagram.
For example, your diagram shows Trig connected to pin 4. The Trig should be the output from your Arduino but you have it defined as an input.
The Echo is connected to pin 2 and it should be an input, but you have it defined as an output.
Finally, in your loop(), you are not even using pin 2 or pin 4, but pins 9 and 8. Another issue is the timing you use in setting the trigger pulse - it does not match the datasheet. I would do something like this (assuming that you are actually connected to the pins shown in your diagram):
#define sensorTrigPin 4
#define sensorEchoPin 2
void setup()
{
Serial.begin(9600);
pinMode(sensorTrigPin, OUTPUT);
pinMode(sensorEchoPin, INPUT);
}
void loop()
{
int pulseWidth = 0;
digitalWrite(sensorTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(sensorTrigPin, LOW);
pulseWidth = pulseIn(sensorEchoPin, HIGH);
Serial.print("Pulse Width: ");
Serial.print(pulseWidth);
delay(1000);
}
Note that pulseWidth is just the amount of time that it takes from the beginning of the Echo pulse going high to the end of the same pulse (when it goes low). You would still have to calculate the distance based on the value of pulseWidth.
UPDATE BASED ON RECENT EDIT TO THE QUESTION
If you change a portion of your loop() code to this, it should work:
void loop(){
digitalWrite(4, HIGH); //was (2, LOW)
delayMicroseconds(10); //was (5)
digitalWrite(4, LOW); //was (2, HIGH)
//REMOVED EXTRA DELAY
time = pulseIn(2, HIGH); //was (4,HIGH);
... //Keep the rest of your code the same.
}
Try connecting your VCC of the sensor to 3V3 instead of 5V. This might sound odd, but I tried it and it worked well. Also, please make sure that your echo and trig pin match the code.

Resources