can i trigger a function in arduino with node-red?
i tried to research it before and i couldn't find any
my arduino code is
int motor1 = 2;
int motor2 = 3;
int motor3 = 4;
int motor4 = 5;
int motor5 = 6;
int motor6 = 7;
int motor7 = 8;
int motor8 = 9;
void setup() {
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(motor3, OUTPUT);
pinMode(motor4, OUTPUT);
pinMode(motor5, OUTPUT);
pinMode(motor6, OUTPUT);
pinMode(motor7, OUTPUT);
pinMode(motor8, OUTPUT);
}
void loop() {
yeetmotor1();
yeetmotor2();
yeetmotor3();
yeetmotor4();
}
void yeetmotor1(){
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
delay(1000);
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
}
void yeetmotor2(){
digitalWrite(motor3, HIGH);
digitalWrite(motor4, LOW);
delay(1000);
digitalWrite(motor3, LOW);
digitalWrite(motor4, LOW);
}
void yeetmotor3(){
digitalWrite(motor5, HIGH);
digitalWrite(motor6, LOW);
delay(1000);
digitalWrite(motor5, LOW);
digitalWrite(motor6, LOW);
}
void yeetmotor4(){
digitalWrite(motor7, HIGH);
digitalWrite(motor8, LOW);
delay(1000);
digitalWrite(motor7, LOW);
digitalWrite(motor8, LOW);
}
If you have any clue for this please send help because i think it is possible to do it
Thank you for anying answering it
Related
I'm trying to Program a Ultrasonic Sensor for a small project where I'm trying to design a level sensor. This is for a beginner mechatronic course that I'm taking right now. But it keeps giving me this error in line 42, bellow the void loop, that says:
"exit status 1'microsecondsToInches' was not declared in this scope"
#include <LiquidCrystal.h>
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int LED1 = 7;
int LED2 = 6;
int LED3 = 5;
int LED4 = 4;
int Bocina = 3;
const int echoPin = 2;
const int trigPin = 1;
void setup() {
pinMode (LED1, OUTPUT);
digitalWrite(LED1, LOW);
pinMode(LED2, OUTPUT);
digitalWrite(LED2, LOW);
pinMode(LED3, OUTPUT);
digitalWrite(LED3, LOW);
pinMode(LED4, OUTPUT);
digitalWrite(LED4, LOW);
pinMode(Bocina, OUTPUT);
lcd.begin(16, 2);
}
void loop() {
long duration;
long inches;
long cm;
lcd.clear();
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
//cm = ( duration / 29 ) / 2;
//inches = cm * 0.393701;
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
lcd.setCursor(5, 0);
lcd.print("Nivel:");
lcd.setCursor(0, 1);
lcd.print(inches);
lcd.print(" in");
}
if (inches >= 4) {
digitalWrite (LED1, HIGH);
digitalWrite (LED2, HIGH);
digitalWrite (LED3, HIGH);
digitalWrite (LED4, LOW);
}
else if (inches >= 3) {
digitalWrite (LED1, LOW);
digitalWrite (LED2, HIGH);
digitalWrite (LED3, HIGH);
digitalWrite (LED4, LOW);
}
else if (inches >= 2) {
digitalWrite (LED1, LOW);
digitalWrite (LED2, LOW);
digitalWrite (LED3, HIGH);
digitalWrite (LED4, LOW);
tone(3, 2000, 100);
delay(1000);
}
else if (inches >= 1) {
digitalWrite (LED1, LOW);
digitalWrite (LED2, LOW);
digitalWrite (LED3, LOW);
digitalWrite (LED4, HIGH);
tone(3, 2000);
}
}
long microsecondsToInches(long microseconds) {
return (microseconds / 74) / 2;
}
long microsecondsToCentimeters(long microseconds) {
return (microseconds / 29) / 2;
}
Can some explain me what's the problem here? And how to solve it?
Thanks in advance :)
Arduino didn't correctly generate the function prototypes, as you have a syntax error within the file.
You have an extra closing bracket }, right before you declare the microsecondsToInches function. Just remove it and you should be good to go
Also welcome to stack overflow!
//Motor A
const int motorpin1 = 6; // Pin 6 of L293
const int motorpin2 = 9; // Pin 3 of L293
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
Serial.begin(9600);
}
// put your main code here, to run repeatedly:
void loop(){
if(Serial.available()>0)
{
char incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=='a'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
delay(200);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
delay(200);
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("other way");
}
else{
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
}
}
}
If we input a in the serial monitor the motor should rotate in one direction and if we input s the motor should rotate in another direction but it is not happening the motor is idle but i get the output like this:
a
one way
s
other way
There is no problem with hardware connections.
Could you please help me with this.Thanks in advance
According to the comments in your code, you are conecting arduino-pin-6 to the l293-pin-6, and arduino-pin-9 to the l293-pin-3.
According to this datasheet, the control pins in L293 are: 2, 7, 10, 15. So, I believe you are connecting it wrong. Also the pulses are being done in the wrong order (from HIGH to LOW and so on).
This should be the correct code (please look at the comments in the code):
//Motor A
const int motorpin1 = 6; // Pin 7 of L293
const int motorpin2 = 9; // Pin 2 of L293
const int motorenablepin = 10; // Pin 1 of L293
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
pinMode(motorenablepin, OUTPUT);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
digitalWrite(motorenablepin, HIGH); // we can let it enabled
Serial.begin(9600);
}
// put your main code here, to run repeatedly:
void loop(){
if(Serial.available()>0)
{
char incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=='a'){
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("other way");
}
delay(200);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
}
}
Please note that I added the motorenablepin that was missing. It must be conected to the l293-pin-1.
Also, since the LOW and LOW states are common to the code, you can simplify it as I did.
There was small mistake in the logic:
if(incomingByte=='a'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);//changed to high
delay(10000);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);//changed to low
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);//changed to high
delay(2000);
digitalWrite(motorpin1, LOW);//changed to low
digitalWrite(motorpin2, LOW);
It this a really simple code about turning on an off some LEDs, but I want it not to be so repetititve
I've tried to make a loop, but i couldn't get it, I did my best, but I'm really bad at this :(( pls need someone's jelp
`
#define LED 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
void setup()
{
pinMode(LED, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
}
void loop()
{
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED2, HIGH);
delay(500);
digitalWrite(LED3, HIGH);
delay(250);
digitalWrite(LED4, HIGH);
delay(125);
digitalWrite(LED5, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(1000);
digitalWrite(LED2, LOW);
delay(500);
digitalWrite(LED3, LOW);
delay(250);
digitalWrite(LED4, LOW);
delay(125);
digitalWrite(LED5, LOW);
delay(500);
}`
I'll rewrite what Oleg Mazurov said in the comment:
#define NUMBER_OF_LEDS 5
static const uint8_t a_led[NUMBER_OF_LEDS] = {2, 3, 4, 5, 6};
static const uint16_t a_delay[NUMBER_OF_LEDS] = {1000, 500, 250, 125, 500};
void setup() {
for (int i = 0; i < NUMBER_OF_LEDS; i++) {
pinMode(a_led[i], OUTPUT);
}
}
void loop() {
for (int i = 0; i < NUMBER_OF_LEDS; i++) {
digitalWrite(a_led[i], !digitalRead(a_led[i]));
delay(a_delay[i]);
}
}
So i have an issue in which when I run my code with my ultrasonic sensors, and h bridge with motors one motor spins at all times and the other spins every 6 seconds for 2 seconds but i don't know why. Any help?
Here is the code:
int in1 = 2;
int in2 = 3;
int in3 = 4;
int in4 = 5;
int in5 = 6;
int in6 = 7;
int trig = 8;
int echol = 9;
int echor = 12;
int echof = 11;
long df, tf, dr, tr, dl, tl;
void setup() {
Serial.begin(9600);
}
void loop() {
pinMode (in1, OUTPUT);
pinMode (in2, OUTPUT);
pinMode (in3, OUTPUT);
pinMode (in4, OUTPUT);
pinMode (in5, OUTPUT);
pinMode (in6, OUTPUT);
pinMode (trig, OUTPUT);
pinMode (echol, INPUT);
pinMode (echor, INPUT);
pinMode (echof, INPUT);
forward();
digitalWrite (trig, HIGH);
delay (0.01);
tf = pulseIn (echof, HIGH);
digitalWrite (trig, LOW);
df = tf * 0.03156;
if (df < 1.5){
digitalWrite (trig, HIGH);
delay (0.01);
tr = pulseIn (echor, HIGH);
tl = pulseIn (echol, HIGH);
digitalWrite (trig, LOW);
dr = tr * 0.03156;
dl = tl * 0.03156;
if (dr > dl) {
right();
delay (5000);
forward();
}
else {
left();
delay (5000);
forward();
}
}
}
void forward(){
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
digitalWrite (in3, HIGH);
digitalWrite (in4, LOW);
digitalWrite (in5, HIGH);
digitalWrite (in6, LOW);
}
void backward(){
digitalWrite (in1, LOW);
digitalWrite (in2, HIGH);
digitalWrite (in3, LOW);
digitalWrite (in4, HIGH);
digitalWrite (in5, LOW);
digitalWrite (in6, HIGH);
}
void left(){
digitalWrite (in1, LOW);
digitalWrite (in2, LOW);
digitalWrite (in3, HIGH);
digitalWrite (in4, LOW);
digitalWrite (in5, HIGH);
digitalWrite (in6, LOW);
}
void right(){
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
digitalWrite (in3, LOW);
digitalWrite (in4, LOW);
digitalWrite (in5, HIGH);
digitalWrite (in6, LOW);
}
Not sure of everything that is wrong, but multiplying a long by tl * 0.03156 and storing the value in a long is probably not doing what you intend. You should use floating-point values to contain the results of that sort of calculation.
First of all, you should move your pin setup in Setup(), there is no need to reinitialize pin i/o setup on every loop.
void Setup()
{
Serial.begin(9600);
pinMode (in1, OUTPUT);
pinMode (in2, OUTPUT);
pinMode (in3, OUTPUT);
pinMode (in4, OUTPUT);
pinMode (in5, OUTPUT);
pinMode (in6, OUTPUT);
pinMode (trig, OUTPUT);
pinMode (echol, INPUT);
pinMode (echor, INPUT);
pinMode (echof, INPUT);
}
From what I know about the H-Bridge module, they usually have 3 inputs per motor, of which only ONE should be on at a time. I could not find any correlation to that effect ion your code.... This is a constraint, so you should organize your code around it. It would make it easier to read and debug. There is no debugger on the Arduino, so organizing code does help a lot. And if you need more help, it will sure be a lot easier for others to understand what you code does.
void MotorA(int dir)
{
// dir = 0 = STOP, +1 = Forward, -1 = Reverse
digitalWrite(in1, dir > 0);
digitalWrite(in2, dir == 0); // You gave no details on the module
digitalWrite(in3, dir < 0); // you have. the actual logic may differ...
}
void MotorB(int dir)
{
// dir = 0 = STOP, +1 = Forward, -1 = Reverse
digitalWrite(in4, dir > 0);
digitalWrite(in5, dir == 0);
digitalWrite(in6, dir < 0);
}
void Stop()
{
MotorA(0);
MotorB(0);
}
void Forward()
{
MotorA(+1);
MotorB(+1);
}
void Reverse()
{
MotorA(-1);
MotorB(-1);
}
void Left()
{
MotorA(+1);
MotorB(-1);
}
void Right()
{
MotorA(-1);
MotorB(+1);
}
May I also suggest you start with a simpler loop to start with, until you have the motors running? Add one feature at a time. This will help you get your project up and ready much faster in the end.
void Loop()
{
Forward()
delay(5000);
Stop()
delay(1000);
Reverse();
delay(1000);
// ---
}
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);
}