Show Posts
Pages: 1 ... 33 34 [35] 36 37 ... 274
511  Using Arduino / Programming Questions / Re: Connecting two Arduino Uno to a Mega through serial on: April 29, 2014, 05:06:01 am
Hello,

Quote
So start with just A and C and a serial connection between them.  Serial.print() a number from A, receive it on C using Serial.read() once you know that there is something to read using Serial.available() and display it on C's serial monitor.

So here's my attempts:

The Arduino Uno (A) generates random number through the following code and I can visualize the number on Serial Monitor when connected via USB, so it should work:

Code:
long randNumber;

void setup(){
  Serial.begin(9600);

  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0));
}

void loop() {
  // print a random number from 0 to ..
  randNumber = random(3000);
  Serial.println(randNumber); 



  delay(500);
}

Now I want the Arduino Mega (C) to read that random number sent from A via serial, and that's the problem.
I've tried a lot of different sketches. I can post this but it didn't work:
Code:
int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);
        Serial2.begin(9600);
            // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
}


I have connected the Mega to my computer via USB while the Uno is connected to a power adapter.
They have a common Ground (GND) and the TX1 of the Uno is connected to the RX2 of the Mega.

It's not working though.

any suggestion?
thanks
Firstly do not initially try to send a random number because you need to know what is being sent in order to know that it is being received correctly.  You can add random number generation later and discuss whether what you are generating is truly random.

So, send a fixed number, say 1234 from the Mega.
Now look at the receiving code.  Each byte that is received by the Mega will have a value between 0 and 255, so how can you ever receive a number like 1234 ?  What the Uno is actually sending is '1' then '2' then '3' then '4', so you must receive each byte and assemble the full number on the receiving side either as an integer of a string.  Note that a String is not the same as a string.

I expect that what you are seeing on the screen as a result of
Code:
Serial.println(incomingByte, DEC);
is a series of numbers between 48 and 5, which is the ASCII code for each single digit.  Remove the DEC parameter and try again.  What do you see ?
512  Using Arduino / Programming Questions / Re: Connecting two Arduino Uno to a Mega through serial on: April 29, 2014, 04:24:16 am
Ray.  Whilst what you suggest and illustrate may be a good idea I can't help think that it is way beyond what the OP will understand and be able to adapt at this stage.

Let's wait and see what code he/she posts so that we can see what has been tried and how and build on that.  I would still like to see something simple such as serial transmission between the Mega and a single Uno before expanding on that. 

Apart from anything else there are still questions to be answered such as generation of a random number, simply using random(255); will not do, how often this is to happen and how the mega will know which pairs of numbers to compare if each Uno is sending a stream of numbers. 

Actually comparing the numbers and turning on an LED is the least of the problems at the moment.

513  Using Arduino / Programming Questions / Re: Make an Array with multiple Sensor value on: April 29, 2014, 02:45:56 am
Why do you need to put the values (what values exactly ?) into an array of ints ?  Do you think that will help with your problem with receiving the data ?
514  Using Arduino / Programming Questions / Re: Arduino Acts weird when using servo libary? on: April 29, 2014, 01:38:15 am
The servo library prevents use of PWM on pins 11 and 12 so you need to use an alternative pin for A4output.

What sort of 9V battery ?  If it is a PP3 then it will last for a very short time before going flat particularly as it is powering a servo. Why not use the LiPo to power everything ?
515  Using Arduino / Programming Questions / Re: How to call functions while motors running as background on: April 29, 2014, 01:32:04 am
You should really get used to using unsigned longs for the timing variables to avoid problems should the program run long enough.  Unlikely with your current project but you will undoubtedly use the technique again and in some projects it will matter.
516  Using Arduino / Programming Questions / Re: Thermal Printer Help on: April 29, 2014, 01:28:23 am
Quote
If I could find a way to show different screens without the use of a delay I would be just fine.
Code:
screenState = 0
screenDisplayPeriod = 500
screenStateStart = millis()

  if millis() - screenStateStart > screenDisplayPeriod and screenState == 0
    //display screen data 0
    screenState = 1
    screenStateStart = millis()
  end if
  else
    //display screen data 1
    screenState = 0
    screenStateStart = millis()
  end else
 
517  Using Arduino / Programming Questions / Re: Connecting two Arduino Uno to a Mega through serial on: April 29, 2014, 01:13:59 am
Quote
I did already try it but it is not so easy, at least not for me.
Post the code that you tried, even if it did not work.  That way we can see what you did and offer advice to fix or improve it.  Please read the sticky at the start of this forum before posting any code and note and follow the advice given.
518  Using Arduino / Programming Questions / Re: How to call functions while motors running as background on: April 28, 2014, 04:04:52 pm
In a word.  No.
Read what Crossroads said, understand how it is applied in the BWD example then apply it to your program.  Eliminate the delay() function entirely.
519  Using Arduino / Programming Questions / Re: Can i call a loop to run at a specific time? on: April 28, 2014, 11:54:04 am
A small but important point.  I am sorry to have to ask this, but I take it you realise that your loop1() and loop2() functions are not loops at all but functions that will run and exit immediately.  True, they will be called repeatedly by the main loop() function but they themselves will not loop.  It is normal practice to give functions meaningful names indicating what they do.
520  Using Arduino / Programming Questions / Re: Can i call a loop to run at a specific time? on: April 28, 2014, 10:52:23 am
Code:
  if (now.hour() == 15 && now.hour() ==20)
This will only be true if the hour is 15 and the hour is 20 which will be never.  Do you mean
Code:
  if (now.hour() >= 15 && now.hour() <= 20)
which will be true when the hour is between 15 and 20

Code:
    void loop1();
Instead of calling the loop1() function I believe that this will be seen by the compiler as a function prototype and to all intents and purposes will be ignored.  Fix the limits of the if test and remove the void from this line.
521  Using Arduino / Programming Questions / Re: Serial.Write(0x0):What does it do? on: April 28, 2014, 10:26:42 am
Whereas Oxo is a stock cube http://www.oxo.co.uk/oxos-history/

Sorry.  I could not resist  smiley-grin
522  Using Arduino / Programming Questions / Re: Connecting two Arduino Uno to a Mega through serial on: April 28, 2014, 10:20:47 am
Quote
I know how to make A and B generate random numbers
Let's start with that.  How random do your numbers have to be and how are you generating them ?

As to
Quote
C reads these number simultaneously
It can't actually do that, but it can do one after the other very quickly.  How will C know that the numbers are a pair to be compared ?

Quote
The process is really simple but I really don't know how to make communicate the three Arduino
So start with just A and C and a serial connection between them.  Serial.print() a number from A, receive it on C using Serial.read() once you know that there is something to read using Serial.available() and display it on C's serial monitor.

The Mega has 4 hardware serial ports so you are well placed to use 2 of them to receive data from the Unos and to display received data on the serial monitor.
523  Using Arduino / Programming Questions / Re: Buttonbox leonardo keyboard on: April 28, 2014, 06:33:34 am
Have you changed your code as I suggested ?
524  Using Arduino / Programming Questions / Re: Buttonbox leonardo keyboard on: April 28, 2014, 04:34:32 am
Quote
Change INPUT_PULLUP ?
No.  That is keeping the input pin at a known HIGH state when the button is not pressed which is what you want to avoid stray voltages affecting the input.

Change
Code:
if (digitalRead(2) == HIGH)
to
Code:
if (digitalRead(2) == LOW)
Note that I am assuming that one of the connections to the button is connected to Arduino pin 2 and the other one is connected to GND.  Is that how it is wired ?
525  Using Arduino / Programming Questions / Re: Buttonbox leonardo keyboard on: April 28, 2014, 02:43:38 am
INPUT_PULLUP as its name implies pulls the input HIGH when the button is not pressed.
So
Code:
  if (digitalRead(2) == HIGH)
will be true until you press the button.  Try
Code:
  if (digitalRead(2) == LOW)
instead
Pages: 1 ... 33 34 [35] 36 37 ... 274