Motors With Arduino's - arduino

I have been working on getting some motors to work through H-bridges and managed to with this code.
// initialise motors
int enA = 3; // Motor 1
int in1 = 4;
int in2 = 2;
int enB = 5; // Motor 2
int in3 = 8;
int in4 = 7;
int enC = 11; // Motor 3
int in5 = 12;
int in6 = 13;
int enD = 6; // Motor 4
int in7 = 9;
int in8 = 10;
void setup()
{
pinMode(enA, OUTPUT); // set the outputs for motors
pinMode(enB, OUTPUT);
pinMode(enC, OUTPUT);
pinMode(enD, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(in5, OUTPUT);
pinMode(in6, OUTPUT);
pinMode(in7, OUTPUT);
pinMode(in8, OUTPUT);
}
void motorLoop(){
// setting the direction to turn and speed
digitalWrite(in1, HIGH); // Motor 1
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH); // Motor 2
digitalWrite(in4, LOW);
digitalWrite(in5, HIGH); // Motor 3
digitalWrite(in6, LOW);
digitalWrite(in7, HIGH); // Motor 4
digitalWrite(in8, LOW);
// Set the speed for the Motors
analogWrite(enA, 1);
analogWrite(enB, 20);
analogWrite(enC, 100);
analogWrite(enD, 200);
};
void loop()
{
motorLoop();
delay(500);
}
However I am trying to turn the data into an array and have hit some issues.
I have never tried creating arrays with Digital inputs but have with Analog ones.
Here is a link to my project on (TinkerCAD) https://www.tinkercad.com/things/fFQKRTjhDrb-smashing-allis-kieran/editel?tenant=circuits?sharecode=6rKnUZsFtcOAetd_TufIuN8TfUgi8EupA1TMjlxiacM=
As you can see by this code I have tried to setup the enable inputs which show no errors but I am struggling setting up the OUTPUT and Speed that the motors rotate at.
// initialise motors
// Motor 1
int in1 = 4;
int in2 = 2;
// Motor 2
int in3 = 8;
int in4 = 7;
// Motor 3
int in5 = 12;
int in6 = 13;
// Motor 4
int in7 = 9;
int in8 = 10;
// Array of PWM's
int i = 0;
byte pwms[i] = {3,5,6,11};
byte numberPwms = 4;
void setup()
{
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(in5, OUTPUT);
pinMode(in6, OUTPUT);
pinMode(in7, OUTPUT);
pinMode(in8, OUTPUT);
for(byte i = 0; i <= numberPwms; i++){
pinMode(pwms[i], OUTPUT);
};
}
void motorLoop(){
// setting the direction to turn and speed
digitalWrite(in1, HIGH); // Motor 1
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH); // Motor 2
digitalWrite(in4, LOW);
digitalWrite(in5, HIGH); // Motor 3
digitalWrite(in6, LOW);
digitalWrite(in7, HIGH); // Motor 4
digitalWrite(in8, LOW);
// Set the speed for the Motors
for(byte i = 0; i < numberPwms; i++){
analogWrite(pwms[i], 200);
};
};
void loop()
{
motorLoop();
delay(500);
}
Any help with this would be greatly appreciated :D

Your for loop counts 5 times. Try changing it to this:
for (byte i = 0; i < numberPwms; i++) {
pinMode(pwms[i], OUTPUT);
};
Also your array pwms[] is initialized with 0 elements, but should work anyway. But you can change it.
uint8_t pwms[] = {3, 5, 6, 11};

Related

Arduino IDE Blinking led when using multiple sensors/if else statements

I'm trying to get my LED to blink if multiple conditions are met.
The following code works fine without the teller1 part inside the if else statement, but when i add the elapsedmillis counter to the if else statement the led doesn't blink, even when all of the conditions are met.
Any help?
Here's my code:
#define trigPin D7
#define echoPin D8
#include <elapsedMillis.h>
elapsedMillis teller1;
elapsedMillis teller2;
const int RED = D5; //het ‘rode’ pootje zit in D5
const int GREEN = D4; //etc.
const int BLUE = D3;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(D6, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
void loop() {
if (teller1 > 500) {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
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;
Serial.print(distance);
Serial.println(" cm");
if ((sensorValue > 20) && (distance > 20) && (teller1 < 250)) {
analogWrite(RED, 255);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
} else if ((sensorValue > 20) && (distance > 20) && (teller1 > 250)) {
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
}
else if (sensorValue <20) {
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
}
teller1 = 0;
}
delay(50);
}
EDIT: I think i've fixed it, but i'm not sure this would be considered well written or the proper way of doing this:
#define trigPin D7 //trigger pin voor de echo sensor definieren op D7
#define echoPin D8 //echo pin voor de echo sensor definieren op D8
#include <elapsedMillis.h> //elapsedMillis library aanroepen
elapsedMillis teller1; //teller1 aanmaken
elapsedMillis teller2; //teller 2 aanmaken
const int RED = D5; //het ‘rode’ pootje zit in D5
const int GREEN = D4; //etc.
const int BLUE = D3;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT); //confirugeer D7 als output
pinMode(echoPin, INPUT); //confirugeer D8 als input
pinMode(D6, OUTPUT); //confirugeer D6 als output
pinMode(RED, OUTPUT); //confirugeer D5 als output
pinMode(GREEN, OUTPUT); //confirugeer D4 als output
pinMode(BLUE, OUTPUT); //confirugeer D3 als output
}
void loop() {
if (teller1 > 500) { //als de timer boven 500ms komt
int sensorValue = analogRead(A0); //sla de sensorwaarde op
Serial.println(sensorValue); //druk de sensorwaarde af in de serial monitor
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; //formule om afstand te berekenen
Serial.print(distance);
Serial.println(" cm");
if ((sensorValue > 20) && (distance < 20) && (teller1 < 750)) {
analogWrite(RED, 255);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
digitalWrite(D6, HIGH);
}
else if (sensorValue <20) {
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
}
teller1 = 0;
} else if (teller1 < 250) {
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
digitalWrite(D6, LOW);
}
delay(50);
Serial.print("teller");
Serial.println(teller1);
}
Your void loop() starts with if (teller1 > 500) {.
Then, when checking the sensor values it says
if ((sensorValue > 20) && (distance > 20) && (teller1 < 250)) {
analogWrite(RED, 255);
teller1 can't be >500 and <250 at the same time.
Edit after comment:
void loop(){
if (teller1 > 500) {
//read sensor values
teller1 = 0;
}
if (sensorValue > 20 && distance > 20) {
//power led
teller2 = 0;
}
if (sensorValue <20 || teller2 > 250){
//unpower led
}
}

Arduino-4X4 matrix keypad with I/O shift register

I need help. I have done some research and my little understanding of keypad scanning is that the ShiftIn value of Input Column should return zero (0) when a keypad button is pressed. Mine is only returning 255 (or 11111111) in BIN. All I need is to track the zero value when a key is pressed and then scan the keys matrix to display the pressed key. I will appreciate any help. I have added my code and schematic.
]1
const int kbdRows = 4;
const int kbdCols = 4;
int LatchIn = 2; //165 pin1
int ClockPin = 3; // 595 pin11 & 165 pin2
int DataIn = 4; //165 pin9
int LatchOut = 5; // 595 pin12
int DataOut = 6; //595 pin14
int led = 7;
int PinState = 0;
char keys[kbdRows][kbdCols] = {
{ '1','2','3','4' },
{ '5','6','7','8' },
{ '9','0','A','B' },
{ 'C','D','E','F' }
};
byte KeyIsDown() {
int row;
int col;
int rowBits;
int colBits;
rowBits = 0X10;
for (row = 0; row < kbdRows; row++) {
digitalWrite(ClockPin, LOW);
digitalWrite(LatchOut, LOW);
shiftOut(DataOut, ClockPin, LSBFIRST, rowBits);
digitalWrite(LatchOut, HIGH);
delay(5);
digitalWrite(ClockPin, HIGH);
digitalWrite(LatchIn, LOW);
delay(5);
digitalWrite(LatchIn, HIGH);
colBits = shiftIn(DataIn, ClockPin, LSBFIRST);
for (col = 0; col < kbdCols; col++) {
if (colBits==0) {
// not sure what condition to put here
byte keypressed = keys[kbdRows][kbdCols]; here
// I know this is the right stuff to return here
}
return colBits;
colBits = colBits >> 1;
}
rowBits = rowBits << 1;
}
}
void setup() {
pinMode(ClockPin, OUTPUT);
pinMode(DataOut, OUTPUT);
pinMode(DataIn, INPUT_PULLUP);
pinMode(LatchOut, OUTPUT);
pinMode(LatchIn, OUTPUT);
digitalWrite(LatchOut, HIGH);
digitalWrite(LatchIn, HIGH);
Serial.begin(9600);
digitalWrite(led, HIGH);
}
void loop() {
byte retColBit = KeyIsDown();
Serial.print("ColBit: ");
Serial.println(retColBit,BIN);
delay(500);
PinState = digitalRead(DataOut);
Serial.print("DataOut: ");
Serial.println(PinState,BIN);
delay(500);
}

using Ultrasonic sensor to find obstecles Arduino

Am trying to make a smart cane for blind ppl using 2 ultrasonic sensors
to detect obstacles, and use a buzzer and a flat vibrating motor as a feedback when an obstacle is detected, where the flat motor should be ON when an obstacle
between 1m - 3m is detected, and the buzzer when it's less than 1m.
now recently i used the NewPing library which solved some of the problems but
the code doesn't do exactly what i want, instead it triggers both the buzzer and motor together when object detected, i'd appreciate it if anyone could help.
#include <NewPing.h>
const int trigPin = 8;
const int trigPin1 = 13;
const int echoPin = 9;
const int echoPin1 = 12;
const int buzzer = 5;
const int motor = 3;
NewPing sonar1(trigPin,echoPin,maxout);
NewPing sonar2(trigPin1,echoPin1,maxout);
// defines variables
long duration;
long duration1;
int distance;
int distance1;
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(motor, OUTPUT);
Serial.begin(9600);
}
void loop() {
distance = sonar1.ping_cm();
distance1 = sonar2.ping_cm();
if (distance > 100 || distance1 > 100) {
digitalWrite(buzzer,LOW);
digitalWrite(motor,HIGH);
}
else if (distance <= 100 || distance1 <= 100) {
digitalWrite(buzzer,HIGH);
digitalWrite(motor,LOW);
}
Serial.print("Distance1: ");
Serial.println(distance);
Serial.print("Distance2: ");
Serial.println(distance1);
}
this is the ultrasonic pins:
first sensor (Vcc = 5V, trig = 8, echo = 9, GND = GND)
second sensor(Vcc = 5V, trig = 13 , echo = 12 , GND = GND)
and this is the buzzer and motor pins:
buzzer = 5 , GND
motor = 3 , GND
Separate the logic for the motor and the buzzer:
if (distance >= 100 && distance <= 300) {
// Motor on
digitalWrite(motor, HIGH);
}
else {
// Motor off
digitalWrite(motor, LOW);
}
if (distance1 < 100) {
// Buzzer on
digitalWrite(buzzer, HIGH);
}
else {
// Buzzer off
digitalWrite(buzzer, LOW);
}

Arduino for Christmas light control - invert code

I have an arduino uno connected to a 8 channel relay board. I want to use it with Vixen 3. When I upload the code all the relays turn on. So, when i send a signal in Vixen the relay turns off. I need a way to invert this so when I send the signal from Vixen it turns on the relay. The coding side isn't really my strong point, so please go easy on me.
With love xx
int C1 = 2;
int C2 = 3;
int C3 = 4;
int C4 = 5;
int C5 = 6;
int C6 = 7;
int C7 = 8;
int C8 = 9;
int i = 0;
int incomingByte[8];
void setup()
{
Serial.begin(9600);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
pinMode(C3, OUTPUT);
pinMode(C4, OUTPUT);
pinMode(C5, OUTPUT);
pinMode(C6, OUTPUT);
pinMode(C7, OUTPUT);
pinMode(C8, OUTPUT);
}
void loop()
{
if (Serial.available() >= 8) {
for (int i=0; i<=8; i++)
{
incomingByte[i] = Serial.read();
}
analogWrite(C1, incomingByte[0]);
analogWrite(C2, incomingByte[1]);
analogWrite(C3, incomingByte[2]);
analogWrite(C4, incomingByte[3]);
analogWrite(C5, incomingByte[4]);
analogWrite(C6, incomingByte[5]);
analogWrite(C7, incomingByte[6]);
analogWrite(C8, incomingByte[7]);
}
}
Try this code:
int C1 = 2;
int C2 = 3;
int C3 = 4;
int C4 = 5;
int C5 = 6;
int C6 = 7;
int C7 = 8;
int C8 = 9;
int i = 0;
int incomingByte[8];
void setup()
{
Serial.begin(9600);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
pinMode(C3, OUTPUT);
pinMode(C4, OUTPUT);
pinMode(C5, OUTPUT);
pinMode(C6, OUTPUT);
pinMode(C7, OUTPUT);
pinMode(C8, OUTPUT);
digitalWrite(C1, LOW);
digitalWrite(C2, LOW);
digitalWrite(C3, LOW);
digitalWrite(C4, LOW);
digitalWrite(C5, LOW);
digitalWrite(C6, LOW);
digitalWrite(C7, LOW);
digitalWrite(C8, LOW);
}
void loop()
{
if (Serial.available() >= 8) {
for (int i = 0; i <= 8; i++)
{
incomingByte[i] = Serial.read();
}
analogWrite(C1, incomingByte[0]);
analogWrite(C2, incomingByte[1]);
analogWrite(C3, incomingByte[2]);
analogWrite(C4, incomingByte[3]);
analogWrite(C5, incomingByte[4]);
analogWrite(C6, incomingByte[5]);
analogWrite(C7, incomingByte[6]);
analogWrite(C8, incomingByte[7]);
}
}

I m transmitting flex sensor values through serial port of arduino using Serial.print() function but I'm not able to read it at the receiving end

Actually in my code I'm transmitting accelerometer values as well as 4 flex sensor values. I have converted accelerometer value to 4 state as F(forward), B(backward),R(right),L(left). I am able to receive this 4 states and also I'm able to write code for these states. With these states I'm also sending flex sensor values to control servo motor remotely. But I'm not able to read those flex values as it is a varying integer. Please help me to read the flex values. I have tried Serial.read(), Serial.parseInt().
My transmitter code is:
int xpin = A0;
int x;
int ypin = A1;
int y;
int vcc=13;
const int flexpin1 = 2;
const int flexpin2 = 3;
const int flexpin3 = 4;
const int flexpin4 = 5;
int flex1[20];
int flex2[20];
int flex3[20];
int flex4[20];
int flexsum1=0;
int flexsum2=0;
int flexsum3=0;
int flexsum4=0;
char state1 = 'S';
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(flexpin1, INPUT);
pinMode(flexpin2, INPUT);
pinMode(flexpin3, INPUT);
pinMode(flexpin4, INPUT);
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(vcc, OUTPUT);
digitalWrite(vcc, HIGH);
}
void loop() {
x = analogRead(xpin);
Serial.print("xpin=");
Serial.print(x);
y = analogRead(ypin);
Serial.print("\t ypin=");
Serial.print(y);
if ( y > 415)
{
state1 = 'F';
}
else if (y < 315)
{
state1 = 'B';
}
else if (x > 410)
{
state1 = 'R';
}
else if (x < 310)
{
state1 = 'L';
}
else {
state1 = 'S';
}
// flex readings stabelized//
for(int x=0; x<25; x++)
{
flex1[x]=analogRead(flexpin1);
flex2[x]=analogRead(flexpin2);
flex3[x]=analogRead(flexpin3);
flex4[x]=analogRead(flexpin4);
flexsum1=flexsum1+flex1[x];
flexsum2=flexsum2+flex2[x];
flexsum3=flexsum3+flex3[x];
flexsum4=flexsum4+flex4[x];
delayMicroseconds(20);
}
flexsum1=flexsum1/25;
flexsum2=flexsum2/25;
flexsum3=flexsum3/25;
flexsum4=flexsum4/25;
Serial.print("\t\t\tflexsum1= ");
Serial.print(flexsum1);
Serial.print("\tflexsum2= ");
Serial.print(flexsum2);
Serial.print("\tflexsum3= ");
Serial.print(flexsum3);
Serial.print("\tflexsum4= ");
Serial.print(flexsum4);
Serial.print("\t state1=");
Serial.println(state1);
delay(200);
}
My receiving code:
#include<Servo.h>
int IN1 = 13;
int IN2 = 12;
int IN3 = 7;
int IN4 = 6;
char state1 = 'S';
Servo servo1, servo2, servo3, servo4;
int flex1, flex2, flex3, flex4;
int angle1, angle2, angle3, angle4;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
servo1.attach(9);
servo2.attach(8);
servo3.attach(10);
servo4.attach(11);
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available()) {
delay(10);
state1 = Serial.read();
Serial.print(state1);
switch (state1)
{
case 'F' : digitalWrite(IN1 , HIGH);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , HIGH);
digitalWrite(IN4 , LOW);
Serial.print("\tcar is moving forward ");
delay(100);
break;
case 'B' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , HIGH);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , HIGH);
Serial.print("\tcar is moving backward ");
delay(100);
break;
case 'L' : digitalWrite(IN1 , HIGH);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , HIGH);
Serial.print("\tcar is turning left ");
delay(100);
break;
case 'R' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , HIGH);
digitalWrite(IN3 , HIGH);
digitalWrite(IN4 , LOW);
Serial.print("\tcar is turning right");
delay(100);
break;
case 'S' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , LOW);
delay(100);
break;
}
if (flex1 >= 845)
{
flex1 = Serial.read();
angle1 = map(flex1, 848, 1000, 0, 180);
angle1 = constrain(angle1, 0, 90);
servo1.write(angle1);
Serial.print("\tangle1=");
Serial.print(angle1);
delay(200);
}
if (flex2 >= 820)
{
flex2 = Serial.read();
angle2 = map(flex2, 825, 1000, 0, 180);
angle2 = constrain(angle2, 0, 90);
servo2.write(angle2);
Serial.print("\tangle2=");
Serial.print(angle2);
delay(200);
}
if (flex3 >= 770)
{
flex3 = Serial.read();
angle3 = map(flex3, 770, 930, 0, 180);
angle3 = constrain(angle3, 90, 180);
servo3.write(angle3);
Serial.print("\tangle3=");
Serial.print(angle3);
delay(200);
}
if (flex4 >= 870)
{
flex4 = Serial.read();
angle4 = map(flex4, 875, 1020, 0, 180);
angle4 = constrain(angle4, 90, 180);
servo4.write(angle4);
Serial.print("\tangle4=");
Serial.print(angle4);
delay(200);
}
}
}

Resources