Go Down

Topic: Make another arduino responds?! (Read 574 times) previous topic - next topic

Alghawi

Mar 31, 2013, 05:55 pm Last Edit: Mar 31, 2013, 11:34 pm by Alghawi Reason: 1
Greetings,

I'm actually new to arduino and yet trying to find my away along. What I'm trying to do here is using an EASYVR shield mounted on arduino uno to recieve the sound command.

Then I want this command be transfered to another arduino serially to be actioned with the other arduino. I know this might be redundunt but the idea is through using an xBee modules to link both arduinos but I want to ensure that they can serially work in case xBees are down.

So for the first part, thanks to EasyVR I got things running: I made this little change to the code so it appears like this:

Code: [Select]
   
#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
  #include "SoftwareSerial.h"
  SoftwareSerial port(12,13);
#else // Arduino 0022 - use modified NewSoftSerial
  #include "WProgram.h"
  #include "NewSoftSerial.h"
  NewSoftSerial port(12,13);
#endif

#include "EasyVR.h"
EasyVR easyvr(port);

//Groups and Commands
enum Groups
{
  GROUP_0  = 0,
  GROUP_1  = 1,
};

enum Group0
{
  G0_GO_SMART = 0,
};

enum Group1
{
  G1_LED_ON = 0,
  G1_LED_OFF = 1,
  G1_LED_DIMMER = 2,
};


EasyVRBridge bridge;

int8_t group, idx;

void setup()
{
  // bridge mode?
  if (bridge.check())
  {
    cli();
    bridge.loop(0, 1, 12, 13);
  }
  // run normally
  Serial.begin(9600);
  port.begin(9600);

  if (!easyvr.detect())
  {
    Serial.println("EasyVR not detected!");
    for (;;);
  }

  easyvr.setPinOutput(EasyVR::IO1, LOW);
  Serial.println("EasyVR detected!");
  easyvr.setTimeout(5);
  easyvr.setLanguage(3);

  group = EasyVR::TRIGGER; //<-- start group (customize)
 
 
  pinMode(11, OUTPUT);
  digitalWrite(11, LOW);    // set the LED off
 
}

void action();

void loop()
{
  easyvr.setPinOutput(EasyVR::IO1, HIGH); // LED on (listening)

Serial.print("Say a command in Group ");
  Serial.println(group);
  easyvr.recognizeCommand(group);

  do
  {
    // can do some processing while waiting for a spoken command
  }
  while (!easyvr.hasFinished());
 
  easyvr.setPinOutput(EasyVR::IO1, LOW); // LED off

  idx = easyvr.getWord();
  if (idx >= 0)
  {
    // built-in trigger (ROBOT)
    // group = GROUP_X; <-- jump to another group X
    return;
  }
  idx = easyvr.getCommand();
  if (idx >= 0)
  {
    // print debug message
    uint8_t train = 0;
    char name[32];
    Serial.print("Command: ");
    Serial.print(idx);
    if (easyvr.dumpCommand(group, idx, name, train))
    {
      Serial.print(" = ");
      Serial.println(name);
    }
    else
      Serial.println();
    easyvr.playSound(0, EasyVR::VOL_FULL);
    // perform some action
    action();
  }
  else // errors or timeout
  {
    if (easyvr.isTimeout())
      Serial.println("Timed out, try again...");
    int16_t err = easyvr.getError();
    if (err >= 0)
    {
      Serial.print("Error ");
      Serial.println(err, HEX);
    }
   
   
    group = GROUP_0;
  }
}

void action()
{
    switch (group)
    {
    case GROUP_0:
      switch (idx)
      {
      case G0_GO_SMART:
        // write your action code here
          group = GROUP_1; //<-- or jump to another group X for composite commands
        break;
      }
      break;
    case GROUP_1:
      switch (idx)
      {
      case G1_LED_ON:
        // write your action code here
        group = GROUP_0; //<-- or jump to another group X for composite commands
        Serial.print('a');
        digitalWrite(11, HIGH);   
        break;

      case G1_LED_OFF:
        // write your action code here
        group = GROUP_0; //<-- or jump to another group X for composite commands
        Serial.print('b');
        digitalWrite(11, LOW);   
        break;

      case G1_LED_DIMMER:
        // write your action code here
        group = GROUP_0; //<-- or jump to another group X for composite commands
        Serial.print('c');
        digitalWrite(11, HIGH);   
        break;
      }
      break;
    }
}


My main problem appears with the other one, I'm not sure if things are okay: I have done this sketch below which I'm not sure about:

Code: [Select]

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int ledPin = 13;
char ser = Serial.read();

void setup(){
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("hHllo World!");
pinMode(13, OUTPUT);
pinMode(9, OUTPUT);
analogWrite(9, 15);
}

void loop (){
lcd.setCursor(0, 1);
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
if (Serial.available()) {
if(ser == 'a'){
analogWrite(ledPin, 0);
lcd.print("LED ON");
}
if(ser == 'b'){
analogWrite(ledPin, 255);
lcd.print("LED OFF");
}
if(ser == 'c'){
analogWrite(ledPin, 150);
lcd.print("LED Fade");}
}
}
}



By the way, can someone tell please what pins I use to wire the easy VR to the second arduino so serial communication can be established.?


Regards,

UKHeliBob

On a Uno the hardware serial pins are 0 (RX) and 1 (TX) and are marked as such on the board.
You will get more help if you post all of your code so that it can be seen as a whole.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Alghawi

#2
Mar 31, 2013, 11:37 pm Last Edit: Mar 31, 2013, 11:39 pm by Alghawi Reason: 1
Thanks UKHeliBob for your prompt reply. I have actually connected the first Arduino with the easyVR mounted on to the second arduino using the RX and TX pins, RX first goes to TX second and so on. I have amneded the codes in the original post to show the full sketches.

The thing is, on the second one sketch, I could not see any serial inputs being received from the first one in the serial monitor even thought the first microcontroller is working just perfect. it responds to commands and even turns an LED there on it to test things.

But my issue happens that no inputs are being sent to the second arduino, where I want the LED on the second one being controlled through sound commands from the first one.

Thanks a lot.

Regards

PeterH


I have actually connected the first Arduino with the easyVR mounted on to the second arduino using the RX and TX pins, RX first goes to TX second and so on.


Can you describe the connections you've made in terms of the pin numbers on each Arduino?
I only provide help via the forum - please do not contact me for private consultancy.

Alghawi

Hello PeterH, both arduinos are uno ones. I connected pin 0 of first arduino to pin 1 of the second, and pin 1 of the first to pin 0 of the second. Do you believe it is a connection issue rather than the sketches? Thanks

Regards,

PeterH


Hello PeterH, both arduinos are uno ones. I connected pin 0 of first arduino to pin 1 of the second, and pin 1 of the first to pin 0 of the second. Do you believe it is a connection issue rather than the sketches? Thanks

Regards,


Pins 0 and 1 on the Arduino are connected to the hardware Serial port. This is connected (via USB) to the host computer when the USB lead is connected - this is what the Arduino serial monitor connects to. If you want your sketch to communicate with the PC then you need to leave pins 0 and 1 dedicated to that. Since you seem to be using Serial.print to send prompts and messages to the user, I assume you do intend this to be connected to a PC. In that case, you can't also use the same pins to talk to a remote Arduino.
I only provide help via the forum - please do not contact me for private consultancy.

Alghawi

Thanks a lot PeterH, Shall in that case disconnet the PC USB connection, and power the arduino up from a battery, and connect it to the remote arduino using pins 0 and 1? serial.print would work right if no PC is connectred right?

retrolefty


Thanks a lot PeterH, Shall in that case disconnet the PC USB connection, and power the arduino up from a battery, and connect it to the remote arduino using pins 0 and 1? serial.print would work right if no PC is connectred right?


Yes it will. You will however need to disconnect the external connections to pin 0 and 1 when you wish to reattach to the PC to upload new sketches, etc.

Lefty

Alghawi

Thanks gentlemen for the help, I still can't get the second arduino to respond as there still seems something missing, possibly the serial 0 and 1 are being sent out at the first place.

I have got an independent power source for both arduinos not the USB!, tried my luck but nothing actually worked in the second arduino. Does the jumper have anything to do here?

Regards

Alghawi

I'm thinking it is the codes, here, I tried looking up the serial monitor of the first device, and everything relates to the easyVR appears normally, it is just the characters I set to be printed at the very bottom of the code1 never appeard!

Regards

Robin2

You haven't mentioned whether you have the grounds of the two Arduinos connected?

...R

Alghawi

I verified that sketches are the issue now, what I just did, is modifying sketch 2 as i suspected the ability of the 2nd arduino to read and respond.

So to ensure ability to read first, I changed the sketch to the below:

Code: [Select]
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int ledPin = 11;
int  serIn;

void setup(){
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Hello World!");
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
analogWrite(9, 15);
}

void loop (){
lcd.setCursor(0, 1);
if(Serial.available()) {
Serial.print("Arduino ");
 while (Serial.available()>0){
serIn = Serial.read(); //read Serial
               Serial.print("LED ON");

 digitalWrite(ledPin, HIGH);
lcd.print("LED ON");
}
}
}


What happened now, that the LED on the 2nd arduino stays on all the time when it is suppose to trail the led on the first arduino. In other words, it should be that I speak to arduino 1, led 1 turns, then signal communicated to arduino 2 which turns led 2.

Also, the LCD screen had the word LED ON LED ON.... repeatitively.

I would be really grateful if anyone can help me with this.

Regards

Alghawi


You haven't mentioned whether you have the grounds of the two Arduinos connected?

...R


They both share the same ground.

Alghawi

Hello,

I'm getting there, many thanks to you for all your great efforts. Now when the sound signal is received it is then sent to the second arduino as in letters such as H, L and so on.

There seem to be a little issue now in the second's arduino sketch, where it does recieve the signal and lights the led, but writes LED OFF on the LCD, and but it can't receive other characters and hence can't turn the led off either even though the LCD changes to LED ON!

The code is below, I would appreciate all your inputs and help, I seem to have a problem understanding the serial characters and so.

Code: [Select]
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int led = 11; //led at pin 13

void setup(){
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Hello World!");
pinMode(led,OUTPUT);
pinMode(9, OUTPUT);
analogWrite(9, 15);
}

void loop (){
lcd.setCursor(0, 1);
if (Serial.available()) { //If there is data in the Serial Line
    int dataByte = Serial.read(); //save data into integer variable dataByte
      Serial.print((char)13);
        if(dataByte == 'H'){ //if the variable is the character H
              digitalWrite(led, HIGH); //then Red LED pin is ON
               lcd.print("LED ON");

            }

            else if(dataByte == 'L') { // if the variable is the character L
              digitalWrite(led, HIGH); // then Green LED pin is O
                lcd.print("LED off");
              }
}
}

PaulS

Code: [Select]
      Serial.print((char)13);
Why?

Why does
                your code jerk
        all over the page?
                        Haven't you
              seen the benefit
of using
Tools + Auto Format?

Go Up