I need BIG help with this project of mine! MK II

So I realised that making this code would be useless, As I wasn't going to use this sketch, since I was going to use more than one arduino. So can someone tell me what I'm doing wrong here? basically I want to have one arduino have a sensor and another arduino have an rgb led. When the sensor on the arduino is triggered. the led on the other one changes color. This is the code I put in for the sensor arduino

#include <Wire.h>

#define LIGHT_ADDR 9

#define ANSWERSIZE 5

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(11, 10); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

int analogPin = A0;

int val = A0;

void setup() {
 
      mySoftwareSerial.begin(9600);
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true);
  }
  Serial.println(F("DFPlayer Mini online."));
  
  

Wire.begin();

Serial.begin(9600);
Serial.println("I2C Sensor");
}

void loop() {
repeatedly:
delay(50);
val = analogRead(analogPin);

Wire.beginTransmission(LIGHT_ADDR);
Wire.write(val);
Wire.endTransmission();
}

and this is the code i put in for the light arduino


#include <Wire.h>
 

#define LIGHT_ADDR 9
 

int RED = 7;
int GREEN = 6;
int BLUE = 5;
 

int rd;
 

int br;
 
void setup() {
 
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  

  Wire.begin(LIGHT_ADDR);
   
from master
  Wire.onReceive(receiveEvent);
  

  Serial.begin(9600);
  Serial.println("I2C Light Demonstration");
}
 
 
void receiveEvent() {

  rd = Wire.read();

  Serial.println(rd);
    
}
void loop() {
   delay(50);
 

  br = analogRead(rd)
 
  digitalWrite(RED, HIGH);
  digitalWrite(GREEN, LOW);
  digitalWrite(BLUE, HIGH);

 
}

What am I doing Wrong?

It looks kinda like you are improvising again.

What example code are you basing your use of the wire library on?

Also, step yourself through your LED program’s loop() function. It sets the LEDs to the same value every time many times a second, and nothing else, thus basically does nothing.

a7

The code I am basing it on is this code here from another video. The trouble I am having is he is using something other than a sensor. here is the code from the other guy I am basing this on.


/*
  I2C Master Control Demo
  i2c-master-demo-control.ino
  Demonstrate use of I2C bus
  Master sends potentimeter position data
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/
 
// Include Arduino Wire library for I2C
#include <Wire.h>
 
// Define Slave I2C Address
#define SLAVE_ADDR 9
 
// Analog pin for potentiometer
int analogPin = 0;
// Integer to hold potentiometer value
int val = 0;
 
void setup() {
 
  // Initialize I2C communications as Master
  Wire.begin();
  
}
 
void loop() {
  delay(50);
  
  // Read pot value 
  // Map to range of 1-255 for flash rate
    val = map(analogRead(analogPin), 0, 1023, 255, 1);
    
  // Write a charatre to the Slave
  Wire.beginTransmission(SLAVE_ADDR);
  Wire.write(val);
  Wire.endTransmission();
 
}


/*
  I2C Slave Control Demo
  i2c-slave-demo-control.ino
  Demonstrate use of I2C bus
  Receives potentimeter position data
  Controls LED blink rate
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/
 
// Include Arduino Wire library for I2C
#include <Wire.h>
 
// Define Slave I2C Address
#define SLAVE_ADDR 9
 
// Define LED Pin
int LED = 13;
 
// Variable for received data
int rd;
 
// Variable for blink rate
int br;
 
void setup() {
 
  pinMode(LED, OUTPUT);
  
  // Initialize I2C communications as Slave
  Wire.begin(SLAVE_ADDR);
   
  // Function to run when data received from master
  Wire.onReceive(receiveEvent);
  
  // Setup Serial Monitor 
  Serial.begin(9600);
  Serial.println("I2C Slave Demonstration");
}
 
 
void receiveEvent() {
  // read one character from the I2C
  rd = Wire.read();
  // Print value of incoming data
  Serial.println(rd);
    
}
void loop() {
   delay(50);
 
  // Calculate blink value
  br = map(rd, 1, 255, 100, 2000);
 
  digitalWrite(LED, HIGH);
  delay(br);
  digitalWrite(LED, LOW);
  delay(br);
 
}```

OK, thanks for including a link to that page where the nice old man has two videos and goes over in somewhat excruciating detail how I2C communication between two Arduinos can be set up and programmed.

Go there. Watch the videos. By watch, I mean make half an attempt to figure out HOW the code he is giving you WORKS.

Then get exactly his examples working. This will take time. Some might say it will feel like work. Others may even characterize such work as hard.

We can't learn this stuff for you. No one is born knowing this stuff. Everyone has been as dumb as you are about this at some point.

Your here and there successes have fooled you into thinking you know what you are doing. But as you see, the slightest changes can be difficult if you are winging it.

If you can't follow that tutorial and duplicate his experiments it is a certain sign that you getting ahead of yourself. Back up to some more basic tutorials. If you don't like that old guy, find someone you do like, or some other resource that matches your learning style.

If you review this thread, you will see that this is what we are telling you; this is how we are trying to help.

a7

at alto777 I second that 101%
best regards Stefan

Alright, I will do that. I just would Like to point out That I want to know if it works. I will see if I can do it with a sensor. i bet I can. Funny thing is, this isn't even the hardest part of the project. Later I have to do the monster ai, But that can wait. Thank you!

i think I need to focus On smaller projects. This has been stressing me out so I might have to cancel the project. You all really helped me, but I just don't want to work on this for months, when I could be living my life. this weight on my shoulders was too much. I need to work on... smaller projects first, learn the basics, then focus on the big stuff. But thanks to everyone who helped me

Hi construct,

You are free to decide what to do. So what do you want to do next?
Would you like to start learning the basics? or would you like to pause some time ?

If you decide to start learning basics I still recommend this tutorial. You will walk up the learning curve pretty fast if you take a path that fits to your knowledge-level.

You are welcome to ask as many questions as you like.
Even if you have hundreds of questions.

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Probably smaller stuff. Maybe i can learn about i2c just to know it for later on instead of learning about it for A huge project. Thank you

most of the time I2C is used to communicate with sensors or to send data to displays.
In these cases you have to analyse which IO-pins are connected to the hardware-I2C-interface.

I2C-devices have a bus-adress which can be found in the datasheet and sometimes printed on the modules PCB. For communicating with such a device you have to initialise the corresponding object with this I2C-bus-adress.

The text above is a compact introduction on how to use I2C.
Well I'm very sure that this compact introduction did not clear much for you as a newbee. And even if I would go more into details if it comes to programming you would still not know where and how to write which commands

initialise the "object" (what is this object?)
How and where to lookup how to "start the motor" of this object
How to lookup what commands the I2C-devices library has to offer.

And you still don't know the real basics how a programming-logic works.

There are some basic things that apply to EACH AND EVERY programm TOTALLY regardless of beeing I2C, SPI, serial, WiFi, Bluetooth, ADC, DAC, onewire etc. etc.

Without knowing these basic things you are still lost. You would still stumble around not really knowing what to change in any program and would have to ask in the forum every five minutes again and again.

But maybe you are one of that guys that enjoys pretending to be "I know enough" to tinker around for days, weeks and months in a hectical way making yourself believe "I am fast" while in truth you proceed much slower than somebody who has taken 20 hours of time to learn the real basics. In this first 20 hours of learing the real basic things she/he does not proceed in his project. But after that she/he starts through like a rocket and will leave you behind.

My recommendation is to Take a look into this tutorial:

this is a clickable link >>>>>Arduino Programming Course <<<<<< This is a clickable link

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

As all this is on a hobby-level you are free to decide what ever you want.
So it is up to you to decide which way you want to go.

best regards Stefan

I tots agree with @StefanL38 and @alto777 and... you!

Start. Over. At. The. Beginning.

Don't try to prepare an 11 course meal whilst still getting confused about salt and pepper.

You are srsly wasting your time, and ours. Look over the entire thread and any others where we've been pleading with you to approach this differently.

a7

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.