I'm trying to make an smart car with Arduino Mega, and I need to turn both of the back wheels on for an specific time sometimes. I've been told that I can set a "digital HIGH" time using tone, But as I need them to work in a same time, Is there a way to set tone for two pins in one line or something to do instead?
Thanks for your help.
#include <Servo.h>
/////////////////////
Servo servo;
/////////////////////
int trig = 12;
int echo = 13;
long duration;
int distance;
int dist_right;
int dist_left;
int ang = 90;
unsigned int value = 255;
unsigned long tone_time = 3000;
float forward_time;
/////////////////////
int ena = 35;
int in1 = 7;
int in2 = 6;
int in3 = 5;
int in4 = 4;
int enb = 47;
/////////////////////
void setup()
{
Serial.begin(9600);
servo.attach(22);
pinMode(trig, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(enb, OUTPUT);
pinMode(echo, INPUT);
digitalWrite(ena, HIGH);
digitalWrite(enb, HIGH);
}
void loop() {
servo.write(90);
distance = dist();
if(distance<=15)
{
for(ang;ang>=0;ang-=2)
{
servo.write(ang);
delay(30);
}
dist_right = dist();
Serial.println(dist_right);
for(ang;ang<=180;ang+=2)
{
servo.write(ang);
delay(30);
}
dist_left = dist();
Serial.println(dist_left);
for(ang;ang>=90;ang-=2)
{
servo.write(ang);
delay(30);
}
if(dist_right>=dist_left)
{
tone(in3, value, tone_time);
}
else if(dist_right<dist_left)
{
tone(in1, value, tone_time);
}
servo.write(90);
ang=90;
}
else{
forward_time=distance/25;
tone((in1,in3), value, forward_time);
}
}
int dist(){
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance= duration*0.034/2;
return distance;
}
I'm 99.99% sure, that your motors will not feel the time differencce if you turn them one by one. Try simplest case and you will see.
// Define your wheel control pins (use same as in your mega)
const int motor1Pin = 5;
const int motor1Pin = 6;
// somewhere in setup method
outputMode(motor1Pin, OUTPUT);
outputMode(motor2Pin, OUTPUT);
// Create function to turn motors and remember the time
unsigned long turnMotorsOn(int seconds) {
// turn motors ant return time when they should be stopped
return millis() + seconds * 1000;
}
// In you code check if it is time to turn off
if (millis() > timeWhenTurnMotorsOff) {
// turn them off
}
Related
I am working on two way communication between arduino and android phone. Currently everything is working, however I have couple of issues I have been trying to solve recently.
How I can ignite ignition for 5 seconds? I mean if IgnitionPin is on HIGH, run it for 5 seconds then automatically turn off? There is an easy way with delay, but it will not work in my case as don't want any other delays to slow up my script.
I am using Arduino Uno. I want to start my Arduino with pin in OFF position. Why pin 10 always turns ON then shuts down, even with digitalWrite(IgnitionPin, HIGH); I have tried other pins and they work fine -> turned OFF on start.
SoftwareSerial BTserial(12,13);
char choice;
const int loopDelay = 50;
int IgnitionPin = 10;
const long ignitionInterval = 5000;
int ignitionState = HIGH;
unsigned long previousMillis = 0;
void setup()
{
BTserial.begin(115200);
digitalWrite(IgnitionPin, HIGH);
pinMode(IgnitionPin, OUTPUT);
}
void loop()
{
if (BTserial.available())
{
choice = BTserial.read();
}
if( choice == 'm' )
{
ignitionState = HIGH;
digitalWrite(IgnitionPin, ignitionState);
ignitionCountTime = millis();
}
if (ignitionCountTime - previousMillis >= ignitionInterval) {
previousMillis = ignitionCountTime;
if (ignitionState == HIGH)
{
ignitionState = LOW;
}
digitalWrite(IgnitionPin, ignitionState);
}
delay(loopDelay);
}
EDIT:
SoftwareSerial BTserial(12,13);
char choice;
const int loopDelay = 50;
int IgnitionPin = 10;
unsigned long startTime;
unsigned long ignitionInterval = 30000;
unsigned long ignitionCountTime = 0;
void setup()
{
BTserial.begin(115200);
digitalWrite(IgnitionPin, HIGH);
pinMode(IgnitionPin, OUTPUT);
}
void loop()
{
if (BTserial.available())
{
choice = BTserial.read();
}
if( choice == 'm' )
{
digitalWrite(IgnitionPin, HIGH);
ignitionCountTime = millis();
}
if (ignitionCountTime - startTime >= ignitionInterval)
{
digitalWrite(IgnitionPin, LOW);
}
delay(loopDelay);
}
#1
Use the TimerOne library or setup an ISR.
Run the ISR at, 5 times per second.
uint32_t timeout = 5 * 60;
uint8_t flag = 1;
digitalWrite (myPin, HIGH);
if (timeout && flag) {
timeout--;
} else {
digitalWrite (myPin, LOW);
flag = 0;
}
OR
by checking time elapsed since some specific point in time.
unsigned long startTime;
unsigned long interval = 60000;
const byte aPin = 13;
void setup()
{
pinMode(aPin, OUTPUT);
digitalWrite(aPin, HIGH);
}
void loop()
{
if (millis() - startTime >= interval)
{
digitalWrite(aPin, LOW);
}
}
EDIT
Arduino is a microcontroller, it can do only one thing at once.
SoftwareSerial BTserial(12,13);
char choice;
const int loopDelay = 50;
int IgnitionPin = 10;
uint32_t timeout = 5 * 60;
uint8_t flag = 0;
void setup()
{
BTserial.begin(115200);
pinMode(IgnitionPin, OUTPUT);
digitalWrite(IgnitionPin, LOW);
}
void loop()
{
if (BTserial.available())
{
choice = BTserial.read();
}
if (choice == "m")
{
timeout = 5 * 60; //modify this timeout.
flag = 1;
digitalWrite(IgnitionPin, HIGH);
}
else if ((timeout > 0) && (flag == 1))
{
timeout--;
}
else
{
digitalWrite(IgnitionPin, LOW);
flag = 0;
}
delay(loopDelay);
}
#2 - In setup you are running 'digitalWrite(IgnitionPin, HIGH);' this will make it high
just use pinMode(IgnitionPin, OUTPUT); for setting pin as output pin
void setup()
{
Serial.begin(115200);
Serial.println("Enter AT commands:");
BTserial.begin(115200);
sensors.begin();
// Set Pin as an output pin
pinMode(IgnitionPin, OUTPUT);
digitalWrite(IgnitionPin, LOW);
}
If you want IgnitionPin as LOW at each restart - use 'digitalWrite(IgnitionPin, LOW);' in setup() after pinMode call.
I wrote a code supposed to do the following: if I press one button while in loop void btnpress(), the program is sent to another function void blink2(), and then one led goes on and after 3 secs the led should go off, and it should also return to void btnpress() again via btnpress();.
The issue is that if i press the button and release, the led goes on and stay still infinitely on, program seems not to execute the following last parts digitalWrite(LED_BUILTIN, LOW); and btnpress();.
const int btnpin = 9;
int btnstate = 0;
unsigned long currentTime;
unsigned long previousTime;
const long period = 3000;
// the setup
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(btnpin, INPUT);
Serial.begin(9600);
}
// the loop
void loop()
{
btnpress();
}
void btnpress()
{
Serial.println("Press button");
delay(500);
btnstate = digitalRead(btnpin);
if (btnstate == HIGH) {
previousTime = millis();
blink2();
}
}
void blink2()
{
if (currentTime - previousTime >= period) {
Serial.println("Led on");
previousTime = currentTime;
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
}
else {
Serial.println("Led off");
digitalWrite(LED_BUILTIN, LOW);
btnpress();
}
}
if I press one button while in loop void btnpress(), the program is
sent to another function void blink2(), and then one led goes on and
after 3 secs the led should go off, and it should also return to void
btnpress() again via btnpress();
According to this statement, I can suggest you to use the following code just to be sure if your functions are working according to your reqirement.
const int btnpin = 9;
int btnstate = 0;
//unsigned long currentTime;
//unsigned long previousTime;
const long period = 3000;
// the setup
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(btnpin, INPUT);
Serial.begin(9600);
}
// the loop
void loop()
{
btnpress();
}
void btnpress()
{
Serial.println("Press button");
delay(500);
btnstate = digitalRead(btnpin);
if (btnstate == HIGH) {
// previousTime = millis();
blink2();
}
}
void blink2()
{
Serial.println("Led on");
digitalWrite(LED_BUILTIN, HIGH);
delay(3000);
Serial.println("Led off");
digitalWrite(LED_BUILTIN, LOW);
//btnpress(); It's already inside the Void loop
}
If it's working well then function call is okay.
And there has no issue with "How to call a void function within if statement (Arduino)"
The problem may be inside your if statement,
if (currentTime - previousTime >= period) {
Serial.println("Led on");
previousTime = currentTime;
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
}
here, the currentTime is not Defined.
Hello i have problem :
state sensor = sensor steps "IF" anyone "steps now = impulse " = all code run"perfect good work im happy" but if sensor LOW a did not detect any motion while loading the code = broke stop all , even if it is at the end close or halfway, it will stop when there is no traffic "" (stage_sensor == HIGH)"", i dont have idea how to fix this//
Please help or sugestion
EDIT : There may be syntax errors (is not consistent), but just wanted to present a problem, the system only works under "if" and this is problem, i dont know how to fix
#include <AccelStepper.h> //accelstepper library
// #define light
// #define move_sensor
// #define pump_water
// #define fan_blowing
// #define fan_extractor
// #define blue
// #define red
// #define green
const byte limitSwitch_1 = 26;
const byte limitSwitch_2 = 25;
bool switchFlipped = false;
bool previousFlip = true;
int switchCounter;
int newSpeed;
int state_sensor = 0; // <--- stage sensor //
int light = 2;
int blue = 3;
int red = 4;
int green = 5;
int fan_blowing = 6;
int water_pump = 8;
int move_sensor = 9;
int fan_extractor = 10;
AccelStepper stepper(1, 22, 23);
void setup()
{
pinMode(water_pump, OUTPUT);
pinMode(light, OUTPUT);
pinMode(fan_blowing, OUTPUT);
pinMode(fan_extractor, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(move_sensor, INPUT);
pinMode(limitSwitch_1, INPUT_PULLUP); //pin 1 engine (IF touch)
pinMode(limitSwitch_2, INPUT_PULLUP); //pin 2 engine (IF touch)
Serial.begin(9600);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(100);
stepper.setSpeed(1000);
delay(500);
}
void loop()
{
digitalWrite(light, HIGH);
digitalWrite(blue, HIGH);
state_sensor = digitalRead(sensor_move);
if (state_sensor == HIGH) // <--- stage sensor IF anyone move = all code run but if sensor LOW did not detect movement all code broke stop all //
{
digitalWrite(blue, LOW);
digitalWrite(red, HIGH);
stepper.runSpeed();
engine();
}
}
void engine()
{
if(digitalRead(limitSwitch_1) == LOW)
{
switchCounter++;
delay(1000);
newSpeed = -1 * (1000 + (switchCounter * 200));
stepper.setSpeed(newSpeed);
}
if(digitalRead(limitSwitch_2) == LOW)
{
switchCounter++;
delay(1000);
newSpeed = -1 * (1000 + (switchCounter * 200));
stepper.stop();
fans();
}
}
void fans()
{
digitalWrite(red, HIGH);
{
digitalWrite(fan_blowing, HIGH);
digitalWrite(fan_extractor, HIGH);
delay(1000);
digitalWrite(water_pump, HIGH);
}
delay(1000);
digitalWrite(red, LOW);
digitalWrite(water_pump, LOW);
digitalWrite(green, HIGH);
delay(1000);
digitalWrite(fan_blowing, LOW);
digitalWrite(fan_extractor, LOW);
digitalWrite(green, LOW);
digitalWrite(blue, HIGH); //this blue RGB light "ON" but if sensor_steps "ON" = active cycle , blue light led off
delay(1000);
}
What you could do is set up a global "stage" variable:
#include <AccelStepper.h> //accelstepper library
const byte limitSwitch_1 = 26;
const byte limitSwitch_2 = 25;
bool switchFlipped = false;
bool previousFlip = true;
int switchCounter;
int newSpeed;
int stage;
AccelStepper stepper(1, 22, 23);
void setup()
{
pinMode(limitSwitch_1, INPUT_PULLUP);
pinMode(limitSwitch_2, INPUT_PULLUP);
Serial.begin(9600);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(100);
stepper.setSpeed(1000);
delay(500);
stage = 0;
}
void loop()
{
stepper.runSpeed();
runner();
}
void runner()
{
if(stage == 0 && digitalRead(limitSwitch_1) == LOW)
{
switchCounter++;
delay(5000);
newSpeed = -1 * (1000 + (switchCounter * 200));
stepper.setSpeed(newSpeed);
++stage;
}
if(stage == 1 && digitalRead(limitSwitch_2) == LOW)
{
stepper.setSpeed(1000);
delay(1000);
stepper.setSpeed(0);
++stage;
}
if (stage == 2) {
// do next thing, then ++stage;, etc.
}
}
Kindly help me in the Arduino code as I am new in this field. I have Arduino code that turns the light in bulb ON and OFF using toggle switch. It is successfully running and giving output.
The following is the code:
int buttonPinBulb = 11;
int relay1 = 10;
void setup() {
pinMode(buttonPinBulb, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
int buttonBulb = digitalRead(buttonPinBulb);
if(buttonBulb == HIGH){
digitalWrite(relay1, HIGH);
} else {
digitalWrite(relay1, LOW);
}
Serial.println(buttonBulb);
}
Before following suggestion in the comment, the output was:
Issue
Bulb is turning ON and OFF when I toggle switch ON and OFF, and the output is showing on serial monitor continuously. But I want only one value that is not repeated. Like if I toggle the switch ON, then the value shown on serial monitor should be 1 and not 11111111....
Please help me about that. How can I do that?
After following suggestion in the comment, the code is:
int buttonPinBulb = 11;
int relay1 = 10;
int buttonBulb;
void setup() {
pinMode(buttonPinBulb, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
Serial.begin(115200);
Serial.println(buttonBulb);
}
void loop() {
// put your main code here, to run repeatedly:
buttonBulb = digitalRead(buttonPinBulb);
if(buttonBulb == HIGH){
digitalWrite(relay1, HIGH);
}else{
digitalWrite(relay1, LOW);
}
//Serial.println(buttonBulb);
}
And the output was:
You can use a global variable to store the current status of the button.
You may also want to debounce your button (using millis() in the below example, unless the debouncing is already done in hardware) - especially when you want to update a database each time the status changes.
int buttonPinBulb = 11;
int relay1 = 10;
int currentStatus = LOW;
// Debounce
unsigned long lastMillis = 0;
const unsigned long debounceTime = 100; // 100ms wait
void setup() {
pinMode(buttonPinBulb, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
Serial.begin(115200);
}
void loop() {
unsigned long currentMillis = millis();
if ( (currentMillis - lastMillis > debounceTime)
|| (currentMillis < lastMillis)) { // protect against overflow
int buttonBulb = digitalRead(buttonPinBulb);
if (buttonBulb != currentStatus) {
digitalWrite(relay1, buttonBulb);
Serial.println(buttonBulb);
currentStatus = buttonBulb;
// update database here
}
lastMillis = currentMillis;
}
}
As you have been told in comments, just store the current value of the pin and issue digitalWrite () only if a change is required.
ccesfully running and giving output. The following is the code:
int buttonPinBulb = 11;
int relay1 = 10;
int curRel1;
void setup()
{
pinMode(buttonPinBulb, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
curRel1 = digitalRead(relay1);
Serial.begin(115200);
}
void loop()
{
int buttonBulb = digitalRead(buttonPinBulb);
if(buttonBulb == HIGH)
{
digitalWrite(relay1, HIGH);
}
else
{
digitalWrite(relay1, LOW);
}
if (buttonBulb != curRel1)
{
Serial.println(buttonBulb);
curRel1 = buttonBulb;
}
}
You could also update curRel1 in loop() calling digitalRead().
I am building an Arduino car, which avoids obstacles and when I try to upload the code to my Arduino from the PlatformIO package of Atom I get an error message like this:
avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0aaa
0x68 != 0x60
avrdude: verification error; content mismatch
avrdude done. Thank you.
This started happening a few days ago for no reason. It worked perfectly and suddenly I started getting this error message.
My code is:
#include <Arduino.h>
#include <Servo.h>
const int trigPin = 6;
const int echoPin = 7;
const int motorRF = 3;
const int sleep = 4;
const int motorRB = 9;
const int motorLF = 10;
const int motorLB = 11;
int minDistance = 350;
long value;
int speed = 1000;
int randNum;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorRF, OUTPUT);
pinMode(motorRB, OUTPUT);
pinMode(motorLF, OUTPUT);
pinMode(motorLB, OUTPUT);
pinMode(sleep, OUTPUT);
Serial.begin(9600);
}
//Define the directions of the motors
void forward() {
analogWrite(motorRF, speed);
analogWrite(motorLF, speed);
analogWrite(motorRB, 0);
analogWrite(motorLB, 0);
}
void backward() {
analogWrite(motorRF, 0);
analogWrite(motorLF, 0);
analogWrite(motorRB, speed);
analogWrite(motorLB, speed);
}
void right() {
analogWrite(motorRF, 0);
analogWrite(motorLF, speed);
analogWrite(motorRB, speed);
analogWrite(motorLB, 0);
}
void left() {
analogWrite(motorRF, speed);
analogWrite(motorLF, 0);
analogWrite(motorRB, 0);
analogWrite(motorLB, speed);
}
void loop() {
//Ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
value = pulseIn(echoPin, HIGH);
Serial.println("Value = "); Serial.println(value);
delay(50);
//Motors
digitalWrite(sleep, HIGH);
if(value > minDistance) {
//Drive forward
backward();
}
else {
//Drive backward
forward();
delay(1000);
//Pick between number 1 and 2
randNum = random(0, 2);
Serial.println("Random Num = "); Serial.println(randNum);
//If the number is 2 then drive right
if(randNum == 1) {
right();
delay(500);
}
//Else drive left
else {
left();
delay(500);
}
}
}
Thank you.
It is a hardware problem. The program is fine. I tried to upload it to another Arduino and it worked perfectly. Thank you.