1

So I have to write a code that recognizes colour using a colour-detector. The because the colourpalette is limited to just 6 colours I can be saved as an Integer (Red as 0, Green as 1 aso.). Now I have problems transfering the Integer from the Arduino that handles the detector to the Arduino I have to write code on. I tried using the analog (A0) pin but with that I only ended up with a 19-20 whenever I tried to transfer anything at all. Is there a solution to transfer an Integer from 0-5? Thanks in advance

1
  • 1
    3 digital IO pins can encode 8 values, from 0 to 7. And you don't mention the distance you want to transfer, nor what Arduino part we're talking about. Commented Jun 13, 2020 at 18:23

2 Answers 2

3

Using the Analog is not a good solution. You should use the "Serial Connection". It only requires two cables - no other electronics - and the code is very simple (see below). If you just want to transfer values in the range 0-255 or smaller (as in your case: 0-5), you can use the "byte"-type variable. One or multiple bytes can easily be transferred using a serial connection, using the "TX" (transmit) and "RX" (receive) pins of the Arduinos. You just connect the TX pin from Arduino #1 to the "RX" pin of Arduino #2 - and: connect the GND pins of the two.
In the setup code for both, you need to start the serial connection with the same baud rate, e.g.

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

Arduino #1 is sending a byte using the following command:

byte color = 0;         // declare the variable "color"
color = 3;              // set the variable to any value in the range [0-255]
Serial.write(color);    // transmit the byte-variable "color"

Arduino #2 is receiving the byte. Therefore it needs to continuously check for new serial data.

void loop() {
   byte data = 0;

   // - check for new serial data - and respond accordingly
   if (Serial.available() > 0) {
      int x = Serial.read();    // The "Serial.read" command returns integer-type
      data = x;                 //             
     // - now: do something with "data"
     if (data == 0) { 
       // "red" was received ... do something ...
     } else if (data == 1) {
       // "green" was received ... do something ...
     } else if (data == 2) {
       // ... and so on, and so on ...
     } 
   }

   // ... do other tasks

}

Make sure that in the "other tasks" you are not using the command "delay", as this would prevent your code from checking the serial for new data in a timely manner. In exactly the same way, your Arduino #2 could also send data back to Arduino #1. In that case, you add one more cable connecting "TX" from #2 to "RX" of #1, and use the same code as above, on the respective other Arduino.

Sign up to request clarification or add additional context in comments.

Comments

0

Serial is most universal, as you can test/simulate/use it by a broad variety of partners. Here, you might code your possible values as human readable characters { '0' .. '5' } or {'R', 'G', 'B', 'C', 'M', 'Y'} or whatever you like.

Eventually, I2C is a better way of serial communication.


Of course, an analog signal can be divided into 6 clearly distinguishable areas, if it's more about states than about events. You need electronics (low pass filter) to turn analogWrite PWM into analog constant values. And eventually, you have to handle the state transitions separately.

(Huh, sounds pretty complicated, doesn't it :) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.