use h_bridge to connect two motor let user enter speed and dirrection to one of motors and the another motor will work in the same speed - arduino-c++

use h_bridge to connect two motor let user enter speed and dirrection to one of motors and the another motor will work in the same speed but in different direction. so in this i write a code but does not run with me
// C++ code
//
void setup()
{
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
Serial.begin(9600);
while (! Serial);
Serial.println("Speed 0 to 255");
}
void loop()
{
/*digitalWrite(9, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(8, LOW);
//delay(1000); // Wait for 1000 millisecond(s)
analogWrite(10,200);*/
if (Serial.available()) {
//int en = Serial.parseInt();
int hl = Serial.parseInt();
if(hl==1)
{
analogWrite(10, 80);
analogWrite(6, 0);
// motor1.set_speed(80);
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
digitalWrite(5,LOW);
digitalWrite(4, LOW);
delay(5000);
analogWrite(10, 20);
analogWrite(6, 0);
// motor1.set_speed(20);
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
digitalWrite(5,LOW);
digitalWrite(4, LOW);
// Serial.println("direction is" + hl);
}
/*else if(hl==-1){
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
Serial.println("direction is" + hl);
}*/
else if(hl==2){
analogWrite(10, 0);
analogWrite(6, 50);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(5,HIGH);
digitalWrite(4, LOW);
delay(3000);
analogWrite(10, 0);
analogWrite(6, 90);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(5,LOW);
digitalWrite(4, HIGH);
// Serial.println("direction is" + hl);
}
else {
analogWrite(10, 0);
analogWrite(6, 0);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(5,LOW);
digitalWrite(4, LOW);
}
}
}
if any one can help me
there is no error in code and debgging i check that but there in her i
/*else if(hl==-1){
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
Serial.println("direction is" + hl);
}*/
i thing there is a wrong

Related

arduino uno parking system project loop run automatically

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
Servo servo1;
Servo servo2;
int IR_ENTER = 3;
int IR_EXIT = 4;
int IR1 = 5;
int IR2 = 7;
int IR3 = 8;
int IR4 = 9;
int welcome = 90;
int goodbye = 90;
int pos = 0;
int carParked = 0;
int parkingLot = 5;
void setup()
{
lcd.init();
lcd.backlight();
servo1.attach(12);
servo2.attach(13);
pinMode(IR_ENTER, INPUT);
pinMode(IR_EXIT, INPUT);
pinMode(IR1, INPUT);
pinMode(IR2, INPUT);
pinMode(IR3, INPUT);
pinMode(IR4, INPUT);
}
void loop()
{
lcd.setCursor(0, 0);
lcd.print("Automatic Car");
lcd.setCursor(0, 1);
lcd.print(" Parking System");
delay(500);
if (digitalRead(IR_ENTER) == LOW)
{
servo1.write(welcome);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" WELCOME!");
lcd.setCursor(0, 1);
lcd.print(" PLEASE ENTER");
delay(1000);
servo1.write(0);
}
if (digitalRead(IR_EXIT) == LOW)
{
servo2.write(goodbye);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" GOODBYE!");
lcd.setCursor(0, 1);
lcd.print(" SAFE JOURNEY");
delay(1000);
servo2.write(0);
}
while (parkingLot >= 1)
{
if (digitalRead(IR1) == LOW || digitalRead(IR2) == LOW || digitalRead(IR3) == LOW || digitalRead(IR4) == LOW)
{
carParked = carParked + 1;
parkingLot = parkingLot - 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Parking Lot");
lcd.setCursor(0, 1);
lcd.print("Left : ");
lcd.print(parkingLot);
delay(2000);
}
}
if (parkingLot == 0)
{
lcd.setCursor (0,0);
lcd.print(" SORRY :( ");
lcd.setCursor (0,1);
lcd.print(" Parking Full ");
delay (3000);
lcd.clear();
}
}
Hi im beginner in arduino, I want to create this car parking system project. I have problem with the while loop for the code, when I compile and upload the code to arduino, it started write to lcd the parking slot left automatically until 0 and show parking full, but actually it should be running once the ir sensor at the parking triggered.
like when the car entered the parking slot which is empty, the sensor will detect the car and write on the lcd, parking slot left : 3
I have 4 slot of parking which have ir sensor for each parking`

how do I connect an ethernet module to a gps module using arduino

First, I apologize for my mediocre English, hello I'm doing a project for college where I need to use a GY-GPS6MV2 gps module and using this module I need to use an enc28j60 ethernet module to save the gps data on a web page and later use a api. I was using the code below but I didn't get any data. Initially I used only the code related to the ethernet module so that later I could add the gps code.
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <UIPEthernet.h>
EthernetServer server = EthernetServer(80);
SoftwareSerial serial1(6, 7); // RX, TX
TinyGPS gps1;
void setup() {
serial1.begin(9600);
Serial.begin(9600);
uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
IPAddress myIP(192, 168, 0, 200);
Ethernet.begin(mac, myIP);
server.begin();
Serial.println("GPS Animal Tracker...");
}
void loop() {
bool recebido = false;
while (serial1.available()) {
char cIn = serial1.read();
recebido = gps1.encode(cIn);
}
if (recebido) {
Serial.println("----------------------------------------");
long latitude, longitude;
unsigned long idadeInfo;
gps1.get_position(&latitude, &longitude, &idadeInfo);
if (latitude != TinyGPS::GPS_INVALID_F_ANGLE) {
Serial.print("Latitude: ");
Serial.println(float(latitude) / 100000, 6);
}
if (longitude != TinyGPS::GPS_INVALID_F_ANGLE) {
Serial.print("Longitude: ");
Serial.println(float(longitude) / 100000, 6);
}
if (idadeInfo != TinyGPS::GPS_INVALID_AGE) {
Serial.print("Idade da Informacao (ms): ");
Serial.println(idadeInfo);
}
int ano;
byte mes, dia, hora, minuto, segundo, centesimo;
gps1.crack_datetime(&ano, &mes, &dia, &hora, &minuto, &segundo, &idadeInfo);
Serial.print("Data (GMT): ");
Serial.print(dia);
Serial.print("/");
Serial.print(mes);
Serial.print("/");
Serial.println(ano);
Serial.print("Horario (GMT): ");
Serial.print(hora);
Serial.print(":");
Serial.print(minuto);
Serial.print(":");
size_t size;
if (EthernetClient client = server.available())
{
while ((size = client.available()) > 0)
{
uint8_t* msg = (uint8_t*)malloc(size);
size = client.read(msg, size);
Serial.write(msg, size);
free(msg);
}
client.println("<h1>ModuloEletronica</h1>");
client.print("<h1>Latitude</h1>");
//client.print((float(latitude) / 100000, 6));
client.println();
client.print ("<h1>Longitude</h1>");
//client.print ((float(longitude) / 100000, 6));
client.println();
client.stop();
}
Serial.print(segundo);
}

PubSubClient & ArduinoJSON - Arduino passing char to digitalWrite

I am not sure how to pass the values in the function in order to be used within the digitalWrite functions.
I'm getting the following error:
error: cannot convert 'String' to 'uint8_t' {aka unsigned char}' for argument '1' to 'void digitalWrite(uint8_t, uint8_t)'
pubsub callback
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("New message on [");
Serial.print(topic);
Serial.print("] ");
Serial.println("");
char s[length];
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
s[i]=payload[i];
}
StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(s);
if (!root.success()) {
Serial.println("parseObject() failed");
}
String relay = root["relay"]; // "relayOne"
int time = root["timestamp"]; // 1351824120
String trigger = root["trigger"]; // "ON"
// Feel free to add more if statements to control more GPIOs with MQTT
commander(relay, trigger);
}
commander function
void commander(String relay, String trigger) {
if(trigger == "ON"){
Serial.print("Turning ");
Serial.println(relay);
Serial.println(" on");
digitalWrite(relay, HIGH);
} else if(trigger == "OFF"){
Serial.println(relay);
digitalWrite(relayOne, LOW);
Serial.print("TRIGGERED!");
} else {
// turn all the LEDs off:
for (int pin = 0; pin < relayPinCount; pin++) {
digitalWrite(relayPins[pin], LOW);
}
}
Serial.println();
}
void commander(String relay, String trigger) {
uint8_t pinNo;
if ( relay == "relayOne" ) {
pinNo = RELAY_1_PIN;
} else
if ( relay == "anotherRelay" ) {
pinNo = OTHER_RELAY_PIN;
} else
if ( ... ) {
...
} else {
return;
}
if(trigger == "ON"){
Serial.print("Turning ");
Serial.println(relay);
Serial.println(" on");
digitalWrite(pinNo, HIGH);
} else if(trigger == "OFF"){
Serial.println(relay);
digitalWrite(pinNo, LOW);
Serial.print("TRIGGERED!");
} else {
// turn all the LEDs off:
for (int pin = 0; pin < relayPinCount; pin++) {
digitalWrite(relayPins[pin], LOW);
}
}
Serial.println();
}

How do you properly read incoming serial data between two arduino pro minis?

I have two arduino pro minis that I'm attempting to use simulate a ripple adder for the purpose of a demonstration in school. Inputs in this case would be two sets of eight switches connected to arduino 1 and the output 9 led's driven from arduino 2.
Arduino 1 reads 2x8pins , converts the two binary numbers into decimal, adds them and transmits over serial to arduino 2. Arduino 2 takes the number converts it back to binary and sets the 9 leds in accordance. Both of the arduinos work on their own when communicating with a usb to serial module but not together. I spent a total of 5h on research and troubleshooting but to no avail.
With my experiments arduino 2 was only able to detect the incoming serial communication but not its value for some reason.
Arduino 1:
void setup() {
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
pinMode(6,INPUT);
pinMode(7,INPUT);
pinMode(8,INPUT);
pinMode(9,INPUT);
pinMode(10,INPUT);
pinMode(11,INPUT);
pinMode(12,INPUT);
pinMode(13,INPUT);
pinMode(14,INPUT);
pinMode(15,INPUT);
pinMode(16,INPUT);
pinMode(17,INPUT);
Serial.begin(9600);
}
void loop() {
int a=0;
if (digitalRead(2)==HIGH)a=a+128;
if (digitalRead(3)==HIGH)a=a+64;
if (digitalRead(4)==HIGH)a=a+32;
if (digitalRead(5)==HIGH)a=a+16;
if (digitalRead(6)==HIGH)a=a+8;
if (digitalRead(7)==HIGH)a=a+4;
if (digitalRead(8)==HIGH)a=a+2;
if (digitalRead(9)==HIGH)a=a+1;
int b=0;
if (digitalRead(10)==HIGH)b=b+128;
if (digitalRead(11)==HIGH)b=b+64;
if (digitalRead(12)==HIGH)b=b+32;
if (digitalRead(13)==HIGH)b=b+16;
if (digitalRead(14)==HIGH)b=b+8;
if (digitalRead(15)==HIGH)b=b+4;
if (digitalRead(16)==HIGH)b=b+2;
if (digitalRead(17)==HIGH)b=b+1;
a=a+b;
Serial.println(a);
delay(1000);
}
Arduino 2:
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10,OUTPUT);
pinMode(13, OUTPUT);
}
void loop() {
int i=0;
int a;
int b;
String x;
int xs;
if (Serial.available() > 0) {
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
x = Serial.readString();
i = x.toInt();
a = i / 256;
if (a >= 1) {
digitalWrite(10, HIGH);
i = i - 256;
Serial.println("256t");
}
else digitalWrite(10, LOW);
a = i / 128;
if (a >= 1) {
digitalWrite(2, HIGH);
i = i - 128;
Serial.println("128t");
}
else digitalWrite(2, LOW);
a = i / 64;
if (a >= 1) {
digitalWrite(3, HIGH);
i = i - 64;
Serial.println("64t");
}
else digitalWrite(3, LOW);
a = i / 32;
if (a >= 1) {
digitalWrite(4, HIGH);
Serial.println("32t");
i = i - 32;
}
else digitalWrite(4, LOW);
a = i / 16;
if (a >= 1) {
digitalWrite(5, HIGH);
i = i - 16;
Serial.println("16t");
}
else digitalWrite(5, LOW);
a = i / 8;
if (a >= 1) {
digitalWrite(6, HIGH);
i = i - 8;
Serial.println("8t");
}
else digitalWrite(6, LOW);
a = i / 4;
if (a >= 1) {
digitalWrite(7, HIGH);
i = i - 4;
Serial.println("4t");
}
else digitalWrite(7, LOW);
a = i / 2;
if (a >= 1) {
digitalWrite(8, HIGH);
i=i-2;
Serial.println("2t");
}
else digitalWrite(8, LOW);
a = i / 1;
if (a >= 1) {
digitalWrite(9, HIGH);
i = i - 1;
Serial.println("1t");
}
else digitalWrite(9, LOW);
}
}
Many thanks in advance and other suggestions arre welcome.

Looping elegantly the message SOS in Arduino LED blinks

I am trying to make my Arduino blink "SOS" in Morse Code as my first real programming project ever.
I have succeeded in doing so, however now I would like to more efficiently write the code. Here is the my version. How can a while loop be implemented to get the S blinking twice? That seems to complicate keeping track of iterations with a counter.
int led = 13;
//yj Integer representing dit (morse ' . ')
int dit = 500;
//yj Integer representing dah (morse ' - '>)
//yj Testing a multiple of another constant integer to make the timing scalable.
int dah = dit*3;
int blankTime = 100;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dah); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dah); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dah); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
}
//TODO:
//Loop counter implementation for multiples (SOS = 3 dit, 3 dah, 3 dit.)
I would suggest for a simple function to generate morse code instead of writing interation again and again. It coulde something like this.
GenerateMorseCode(char MorseArray[], int len);
You can fill MorseArray with some identifier for dit and dah: May be you can use dot (.) and hipen(-). So for 3 dit , 3dah and 3 dit. Your array would be
...---...
with length of array as 9. Now inside GenerateMorseCode function all you need to do to parse the MorseArray. You can use simple switch case something like this
GenerateMorseCode(char MorseArray[], int len)
{
for(int i =0i<len;i++)
{
switch(MorseArray[i])
{
case '.':
//Your led logic in case of dot
break;
case '-':
//Your led logic in case of dah
break;
}
}
}
Further more you can wrap this function inside another function which keep calling GenerateMorseCode function as per specified loops. To generate MorseArray you can create a map of char array with Morse code. You can keep adding new enum and update corresponding Morsemap. Something like this
#define MAX_ELEM 12
enum MorseCode
{
A_M = 0,
B_M,
C_M,
SOS_M
};
typedef struct MorseMap
{
char array[MAX_ELEM];
};
MorseMap mm[3] = {{'.','-','\0'},{'-','.','.','.','\0'},{'-','.','-','.','\0'} };
Also there is no need to length now, it can be calculated from char array. To access any Morse code use mm[A_M] or mm[SOS_M]
GenerateMorseCode(char arr[])
{
int len = strlen(arry);
//Above for loop and switch case case
}
You want a method to represent "dashes" and a method to represent "dots".
Here is an example using pin #13 on an Uno: http://www.devfactor.net/2014/09/22/arduino-made-easy-output-messages-in-morse-code/
/*
Morse SOS
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
for (int a = 0; a < 3; a++) {
dot();
}
for (int b = 0; b < 3; b++) {
dash();
}
for (int c = 0; c < 3; c++) {
dot();
}
}
void dot()
{
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(250);
}
void dash()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(250);
}
Note that the dash() and dot() methods make repeating much more elegant- and require less code than hard-coding each dash or dot.