Skip to main content
deleted 235 characters in body
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

I am used to arduinoArduino sketches with a void setup() part that runs once, and a void loop() part that keeps looping. What happens when you have void functions outside of the main void loop()? Will these all keep looping in parralelparallel or do they run one after the other? Or do certain void functions only run once certain criteria has been met (like a while loop)?

//I2C_test

//This code demonstrates communication via an I2C bus between a raspberry pi and an arduino.
//When the Raspberry pi (master) sends data to the Arduino (slave), the Arduino uses this
//data to control a motor. After the Arduino has recieved data from the master, it then collects
//data from the external environment via a sensor and sends this data back to the Raspberry pi.

#include <Wire.h>
int number = 0;                                //Declare variables
int val = 0;

void setup() {                                
  //Anything between the curly brackets runs once when the arduino is turned on or reset
 
    pinMode(0, INPUT);                         
  //Set pin 0 as input and 3 as output
    pinMode(3, OUTPUT);
    Serial.begin(9600);                       
  //Set the data rate for serial transmission at 9600bps
    Wire.begin(0x04);                         
  //Initiate the Wire library, join the arduinoArduino as a slave, and specify its 7 bit slave address
    Wire.onReceive(receiveData);              
  //Define callbacks for i2c communication
    Wire.onRequest(sendData);
 
}

void loop() {                                 
  //The code between the curly brackets keeps repeating
 
    delay(100);
 
}

void receiveData(int byteCount) {
 
    while(Wire.available()) {
        number = Wire.read();               
    //Set the variable "number" to the data sent by the master 
        analogWrite(3, number);              
    //Write this number to pin 3 (PWM). This controls the motor speed
    }
 
    val = analogRead(0);                      
  //Read the voltage on pin 0 (connected to the sensor). Map input voltages between 0 and 5 volts into integer values between 0 and 1023 

}

void sendData() {
 
    Wire.write(val);                          
  //Send the data read from the sensor to the master.
 
}

I am used to arduino sketches with a void setup() part that runs once, and a void loop() part that keeps looping. What happens when you have void functions outside of the main void loop()? Will these all keep looping in parralel or do they run one after the other? Or do certain void functions only run once certain criteria has been met (like a while loop)?

//I2C_test

//This code demonstrates communication via an I2C bus between a raspberry pi and an arduino.
//When the Raspberry pi (master) sends data to the Arduino (slave), the Arduino uses this
//data to control a motor. After the Arduino has recieved data from the master, it then collects
//data from the external environment via a sensor and sends this data back to the Raspberry pi.

#include <Wire.h>
int number = 0;                                //Declare variables
int val = 0;

void setup() {                                 //Anything between the curly brackets runs once when the arduino is turned on or reset
 
    pinMode(0,INPUT);                          //Set pin 0 as input and 3 as output
    pinMode(3,OUTPUT);
    Serial.begin(9600);                        //Set the data rate for serial transmission at 9600bps
    Wire.begin(0x04);                          //Initiate the Wire library, join the arduino as a slave, and specify its 7 bit slave address
    Wire.onReceive(receiveData);               //Define callbacks for i2c communication
    Wire.onRequest(sendData);
 
}

void loop() {                                  //The code between the curly brackets keeps repeating
 
    delay(100);
 
}

void receiveData(int byteCount){
 
    while(Wire.available()) {
        number = Wire.read();                  //Set the variable "number" to the data sent by the master 
        analogWrite(3,number);                 //Write this number to pin 3 (PWM). This controls the motor speed
    }
 
    val = analogRead(0);                       //Read the voltage on pin 0 (connected to the sensor). Map input voltages between 0 and 5 volts into integer values between 0 and 1023 

}

void sendData(){
 
    Wire.write(val);                           //Send the data read from the sensor to the master.
 
}

I am used to Arduino sketches with a void setup() part that runs once, and a void loop() part that keeps looping. What happens when you have void functions outside of the main void loop()? Will these all keep looping in parallel or do they run one after the other? Or do certain void functions only run once certain criteria has been met (like a while loop)?

//I2C_test

//This code demonstrates communication via an I2C bus between a raspberry pi and an arduino.
//When the Raspberry pi (master) sends data to the Arduino (slave), the Arduino uses this
//data to control a motor. After the Arduino has recieved data from the master, it then collects
//data from the external environment via a sensor and sends this data back to the Raspberry pi.

#include <Wire.h>
int number = 0; //Declare variables
int val = 0;

void setup() { 
  //Anything between the curly brackets runs once when the arduino is turned on or reset
  pinMode(0, INPUT); 
  //Set pin 0 as input and 3 as output
  pinMode(3, OUTPUT);
  Serial.begin(9600); 
  //Set the data rate for serial transmission at 9600bps
  Wire.begin(0x04); 
  //Initiate the Wire library, join the Arduino as a slave, and specify its 7 bit slave address
  Wire.onReceive(receiveData); 
  //Define callbacks for i2c communication
  Wire.onRequest(sendData);
}

void loop() { 
  //The code between the curly brackets keeps repeating
  delay(100);
}

void receiveData(int byteCount) {
  while(Wire.available()) {
    number = Wire.read(); 
    //Set the variable "number" to the data sent by the master
    analogWrite(3, number); 
    //Write this number to pin 3 (PWM). This controls the motor speed
  }
  val = analogRead(0); 
  //Read the voltage on pin 0 (connected to the sensor). Map input voltages between 0 and 5 volts into integer values between 0 and 1023
}

void sendData() {
  Wire.write(val); 
  //Send the data read from the sensor to the master.
}
Tweeted twitter.com/#!/StackArduino/status/525031732899110913
Source Link
Blue7
  • 245
  • 3
  • 4
  • 8

How do functions outside of void loop work?

I am used to arduino sketches with a void setup() part that runs once, and a void loop() part that keeps looping. What happens when you have void functions outside of the main void loop()? Will these all keep looping in parralel or do they run one after the other? Or do certain void functions only run once certain criteria has been met (like a while loop)?

For example in the code below, when will the void receiveData(int byteCount) and the void sendData() functions run?

//I2C_test

//This code demonstrates communication via an I2C bus between a raspberry pi and an arduino.
//When the Raspberry pi (master) sends data to the Arduino (slave), the Arduino uses this
//data to control a motor. After the Arduino has recieved data from the master, it then collects
//data from the external environment via a sensor and sends this data back to the Raspberry pi.

#include <Wire.h>
int number = 0;                                //Declare variables
int val = 0;

void setup() {                                 //Anything between the curly brackets runs once when the arduino is turned on or reset

    pinMode(0,INPUT);                          //Set pin 0 as input and 3 as output
    pinMode(3,OUTPUT);
    Serial.begin(9600);                        //Set the data rate for serial transmission at 9600bps
    Wire.begin(0x04);                          //Initiate the Wire library, join the arduino as a slave, and specify its 7 bit slave address
    Wire.onReceive(receiveData);               //Define callbacks for i2c communication
    Wire.onRequest(sendData);

}

void loop() {                                  //The code between the curly brackets keeps repeating

    delay(100);

}

void receiveData(int byteCount){

    while(Wire.available()) {
        number = Wire.read();                  //Set the variable "number" to the data sent by the master 
        analogWrite(3,number);                 //Write this number to pin 3 (PWM). This controls the motor speed
    }

    val = analogRead(0);                       //Read the voltage on pin 0 (connected to the sensor). Map input voltages between 0 and 5 volts into integer values between 0 and 1023 

}

void sendData(){

    Wire.write(val);                           //Send the data read from the sensor to the master.

}