Combining 2 different codes for 2 different inputs in one sketch

Hallo,
ich habe 2 Sketches ( einen mit dem ich den MPR 121 auslese und einen mit dem ich den Potentiometer auslese) und möchte diese in einem zusammenfügen, sodass ich sie kombiniert auf den Arduino uno hochladen kann.

Der Code für die 4 MPR 121 lautet:


#include <Wire.h>
#include "Adafruit_MPR121.h"

#ifndef _BV
#define _BV(bit) (1 << (bit)) 
#endif


//**********************************For 1st********************************
// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;


//**********************************For 1st********************************


//**********************************For 2nd********************************
// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap_2 = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched_2 = 0;
uint16_t currtouched_2 = 0;

//**********************************For 2nd********************************

//**********************************For 3rd********************************
// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap_3 = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched_3 = 0;
uint16_t currtouched_3= 0;

//**********************************For 3rd********************************

//**********************************For 4th********************************
// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap_4 = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched_4 = 0;
uint16_t currtouched_4 = 0;

//**********************************For 4th********************************

int val[6];

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

  while (!Serial) { // needed to keep leonardo/micro from starting too fast!
    delay(10);
  }
  
  Serial.println("Adafruit MPR121 Capacitive Touch sensor test"); 
  
  // Default address is 0x5A, if tied to 3.3V its 0x5B
  // If tied to SDA its 0x5C and if SCL then 0x5D
  if (!cap.begin(0x5A)) {
    Serial.println("1st :MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("1st:MPR121 found!");


//
 // Default address is 0x5A, if tied to 3.3V its 0x5B
  // If tied to SDA its 0x5C and if SCL then 0x5D
  if (!cap_2.begin(0x5B)) {
    Serial.println("2nd :MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("2nd:MPR121 found!");
//
//
//
//
   // Default address is 0x5A, if tied to 3.3V its 0x5B
  // If tied to SDA its 0x5C and if SCL then 0x5D
  if (!cap_3.begin(0x5C)) {
    Serial.println("3rd :MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("3rd:MPR121 found!");
//
//
//
//
//   // Default address is 0x5A, if tied to 3.3V its 0x5B
//  // If tied to SDA its 0x5C and if SCL then 0x5D
//  if (!cap_4.begin(0x5D)) {
//    Serial.println("4th :MPR121 not found, check wiring?");
//    while (1);
//  }
//  Serial.println("4th:MPR121 found!");


  
}

void loop() {
  // Get the currently touched pads
  currtouched =   cap.touched();
  currtouched_2 = cap_2.touched();
  currtouched_3 = cap_3.touched();
//  currtouched_4 = cap_4.touched();

{
  for(int i = 0; i < 6; i++){
    val[i] = analogRead(i);
    Serial.print(val[i]);
    Serial.print(" ");
    }
  Serial.println();  // send it out
  delay(10);
 }


  
  for (uint8_t i=0; i<12; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" touched");
    }
    // if it *was* touched and now *isnt*, alert!
    if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" released");
    }
  }

  // reset our state
  lasttouched = currtouched;




  for (uint8_t i=0; i<12; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched_2 & _BV(i)) && !(lasttouched_2 & _BV(i)) ) {
      Serial.print(i+12); Serial.println(" touched");
    }
    // if it *was* touched and now *isnt*, alert!
    if (!(currtouched_2 & _BV(i)) && (lasttouched_2 & _BV(i)) ) {
      Serial.print(i+12); Serial.println(" released");
    }
  }

  // reset our state
  lasttouched_2 = currtouched_2;




  for (uint8_t i=0; i<12; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched_3 & _BV(i)) && !(lasttouched_3 & _BV(i)) ) {
      Serial.print(i+24); Serial.println(" touched");
    }
    // if it *was* touched and now *isnt*, alert!
    if (!(currtouched_3 & _BV(i)) && (lasttouched_3 & _BV(i)) ) {
      Serial.print(i+24); Serial.println(" released");
    }
  }

  // reset our state
  lasttouched_3 = currtouched_3;







    for (uint8_t i=0; i<12; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched_4 & _BV(i)) && !(lasttouched_4 & _BV(i)) ) {
      Serial.print(i+36); Serial.println(" touched");
    }
    // if it *was* touched and now *isnt*, alert!
    if (!(currtouched_4 & _BV(i)) && (lasttouched_4 & _BV(i)) ) {
      Serial.print(i+36); Serial.println(" released");
    }
  }

  // reset our state
  lasttouched_4= currtouched_4;



  

  // comment out this line for detailed data from the sensor!
  return;
  
  // debugging info, what
  Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
  Serial.print("Filt: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.filteredData(i)); Serial.print("\t");
  }
  Serial.println();
  Serial.print("Base: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.baselineData(i)); Serial.print("\t");
  }
  Serial.println();
  
  // put a delay so it isn't overwhelming
  delay(100);
  for(int i = 0; i < 6; i++){
    val[i] = analogRead(i);
    Serial.print(val[i]);
    Serial.print(" ");
    }
  Serial.println();  // send it out
  delay(10);
  
}

und der code für die Potentiometer lautet:

int val[6];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  

}

void loop() {
  for(int i = 0; i < 6; i++){
    val[i] = analogRead(i);
    Serial.print(val[i]);
    Serial.print(" ");
    }
  Serial.println();  // send it out
  delay(10);
 }

Ich bin über jede Hilfe dankbar,
die codes einfach zusammenfügen hat leider nicht funktioniert.
Beste Grüße

Das kann auch nicht funktionieren.
Was hat er denn gemeldet ?

Da ist die Integration schon drin.
Zwei Klammern zuviel.
Zeile 120 öffnend
Zeile 128 schliessend.

Und räumd das auf.
Da kann kein Mensch lesen.
Leerzeilen raus, Kommentare auf das notwendigste und STRG-T in der IDE drücken.

Aehm Moment.
Auf was fürn Board versucht Du das denn?

Beim UNO ist A4 A5 belegt durch den I2C!

Ich versuch das ganz auf dem uno und auf dem teensy 3.2
einzeln funktionieren die beiden codes,
das zusammenfügen gelingt mir nicht

er meldet dass er die Sensoren nicht findet, was komisch ist denn wenn ich die codes einzeln verwende findet er ihn

Der MCP benutzt den SDA / SCL - Das ist auf dem UNO PIN A4 und A5.
Wenn Du das val[6] in val[4] umtickerst, könnte es was werden.
Musst die Dinger an A4 / A5 aber auch physisch abklemmen.

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