Anyone know how I can combine theses 2 sketches?

Hi I am trying to combine 2 different sketches with 0 success -- They are attached.

I have tried bring 1 sketch into the other and removing the "void set" term in the added sketch and many other machinations to no avail-- to the point of total confusion.

I then tried using Tabs -- same issues-- redefine "void set" , redefine "void loop"...

Last I tried using Tabs with #include" statement " With the file type .ino and tried changing file type to .cpp---this gives Compile error--"No such file in directory"--its in the library.

Attached : 1st and 2nd attachments are the 2 working sketches.

last is both files in a new sketch to try using include statement--the tab names are different the code is the same - they were changed to .cpp files

Any and all help appreciated!!!

Thanks--Mike

LoadCellworking_start-2cells.ino (790 Bytes)

MAD_MIKES_720_-360_ROTARY_ENCODER.ino (1.16 KB)

Rotary_before_loadcells_combined.ino (185 Bytes)

#include "_2LOADCELLS.cpp"
#include "MMENCODER720.cpp"
void setup() {

nope.

looky here

I took a shot at combining them correctly. I think you have a logic error in loop. Forum pasting screwed up the CTRL-T formatting on it.

 #include "HX711.h"

HX711 cell_1(9, 8);

volatile unsigned int counter = 0;

long val = 0;

float count = 0;

void setup() {

 Serial.begin(9600);

 pinMode(2, INPUT); // set pin to input

 pinMode(3, INPUT); // set pin to input

 digitalWrite(2, HIGH); // turn on pullup resistors

 digitalWrite(3, HIGH); // turn on pullup resistors

 //Setting up interrupt

 //A rising pulse from encoder activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.

 attachInterrupt(0, ai0, RISING);

 //B rising pulse from encoder activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.

 attachInterrupt(1, ai1, RISING);

}

void loop() {

 count = count + 1;

 // Use only one of these

 //val = ((count-1)/count) * val + (1/count) * cell.read(); // take long term average

 //val = 0.5 * val + 0.5 * cell.read(); // take recent average

 val = cell_1.read(); // most recent reading

 Serial.print( val );

HX711 cell_2(5, 4);

long val = 0; // need this here? 

float count = 0; //need this here?

 count = count + 1;
// >>>>> val will always be 0, wiping out the first part of this calculation, while count will always be 1. Seem like a logic error to me.

 // Use only one of these

 //val = ((count-1)/count) * val + (1/count) * cell.read(); // take long term average

 //val = 0.5 * val + 0.5 * cell.read(); // take recent average

 val = cell_2.read(); // most recent reading

 Serial.println( val );}

 // Send the value of counter

 Serial.println((counter%720) * .5);

}

void ai0() {

 // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH

 // Check pin 3 to determine the direction

 if (digitalRead(3) == LOW) {

 counter++;

 }

else {

 counter--;

 }

}


void ai1() {

 // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH

 // Check with pin 2 to determine the direction

 if (digitalRead(2) == LOW) {

 counter--;

 } 

else {

 counter++;

 }

}

RE:

CrossRoads:
I took a shot at combining them correctly. I think you have a logic error in loop. Forum pasting screwed up the CTRL-T formatting on it.

Thanks Crossroads!!--I gave it a try but It still produces this error-Arduino: 1.8.2 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\Mike\Documents\Arduino\Combined_44\Combined_44.ino

Compiling sketch...
"C:\Users\Mike\Desktop\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR  "-IC:\Users\Mike\Desktop\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Users\Mike\Desktop\Arduino\hardware\arduino\avr\variants\standard" "C:\Users\Mike\AppData\Local\Temp\arduino_build_476378\sketch\Combined_44.ino.cpp" -o "C:\Users\Mike\AppData\Local\Temp\arduino_build_476378\sketch\Combined_44.ino.cpp.o"
C:\Users\Mike\Documents\Arduino\Combined_44\Combined_44.ino: In function 'void setup()':

Combined_44:30: error: 'ai0' was not declared in this scope

attachInterrupt(0, ai0, RISING);

       
Combined_44:34: error: 'ai1' was not declared in this scope

attachInterrupt(1, ai1, RISING);

^

C:\Users\Mike\Documents\Arduino\Combined_44\Combined_44.ino: At global scope:

Combined_44:73: error: 'Serial' does not name a type

Serial.println((counter%720) * .5);

^

Combined_44:75: error: expected declaration before '}' token

}

^

exit status 1
'ai0' was not declared in this scope

Attached is the resulting compile with HX711.h and HX711.cpp (HX711.cpp is needed for calibration)          ^




[Combined_44.ino|attachment](upload://hh4qZrnhahZesEKaQP041PsOFZU.ino) (1.87 KB)

I can't offer more on the interrupt subroutines without looking at examples at home of how I've done them in the past.
Probably just a simple syntax change is needed. I think I had them before setup(), but I'm not sure location matters.

Maybe just take out 'void' from in front of 'ai0' and 'ai1' ?

For example, this routine for a PCINT interrupt to read a rotary encoder just starts with the routine name:

ISR (PCINT0_vect)

{

static byte pinA, pinB; 

static boolean ready;

static unsigned long lastFiredTime;

 byte newPinA = digitalRead (Encoder_A_Pin);

 byte newPinB = digitalRead (Encoder_B_Pin);

 if (pinA == newPinA && pinB == newPinB)

 return; // spurious interrupt

 // so we only record a turn on both the same (HH or LL)

 // Forward is: LH/HH or HL/LL

 // Reverse is: HL/HH or LH/LL

 if (newPinA == newPinB)

 {

 if (ready)

 {

 if (millis () - lastFiredTime >= ROTARY_DEBOUNCE_TIME)

 {

 if (newPinA == HIGH) // must be HH now

 {

 if (pinA == LOW)

 fileNumber ++;

 else

 fileNumber --;

 }

 else

 { // must be LL now

 if (pinA == LOW) 

 fileNumber --;

 else

 fileNumber ++; 

 }

 if (fileNumber > MAX_FILE_NUMBER)

 fileNumber = 0;

 else if (fileNumber < 0)

 fileNumber = MAX_FILE_NUMBER;

 lastFiredTime = millis ();

 fired = true;

 }

 ready = false;

 } // end of being ready

 } // end of completed click

 else
ready = true;

 pinA = newPinA;

 pinB = newPinB;

 } // end of PCINT2_vect

CrossRoads:
Maybe just take out 'void' from in front of 'ai0' and 'ai1' ?

For example, this routine for a PCINT interrupt to read a rotary encoder just starts with the routine name:

ISR (PCINT0_vect)

{

static byte pinA, pinB;

static boolean ready;

static unsigned long lastFiredTime;

byte newPinA = digitalRead (Encoder_A_Pin);

byte newPinB = digitalRead (Encoder_B_Pin);

if (pinA == newPinA && pinB == newPinB)

return; // spurious interrupt

// so we only record a turn on both the same (HH or LL)

// Forward is: LH/HH or HL/LL

// Reverse is: HL/HH or LH/LL

if (newPinA == newPinB)

{

if (ready)

{

if (millis () - lastFiredTime >= ROTARY_DEBOUNCE_TIME)

{

if (newPinA == HIGH) // must be HH now

{

if (pinA == LOW)

fileNumber ++;

else

fileNumber --;

}

else

{ // must be LL now

if (pinA == LOW)

fileNumber --;

else

fileNumber ++;

}

if (fileNumber > MAX_FILE_NUMBER)

fileNumber = 0;

else if (fileNumber < 0)

fileNumber = MAX_FILE_NUMBER;

lastFiredTime = millis ();

fired = true;

}

ready = false;

} // end of being ready

} // end of completed click

else
ready = true;

pinA = newPinA;

pinB = newPinB;

} // end of PCINT2_vect

Thanks !!!!
This will Get me even more confused -- I cant understand why Im not able to compile the sketches I have --they work perfectly standing alone--the encoder functions as needed as does the 2 loadcells
ERROR MESSAGE on my combined 44:

Arduino: 1.8.2 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\Mike\Desktop\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Users\Mike\Desktop\Arduino\hardware -tools C:\Users\Mike\Desktop\Arduino\tools-builder -tools C:\Users\Mike\Desktop\Arduino\hardware\tools\avr -built-in-libraries C:\Users\Mike\Desktop\Arduino\libraries -libraries C:\Users\Mike\Documents\Arduino\libraries -fqbn=arduino:avr:uno -ide-version=10802 -build-path C:\Users\Mike\AppData\Local\Temp\arduino_build_476378 -warnings=none -build-cache C:\Users\Mike\AppData\Local\Temp\arduino_cache_737176 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=C:\Users\Mike\Desktop\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Users\Mike\Desktop\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Users\Mike\Desktop\Arduino\hardware\tools\avr -verbose C:\Users\Mike\Documents\Arduino 1\Combined_44\Combined_44.ino
C:\Users\Mike\Desktop\Arduino\arduino-builder -compile -logger=machine -hardware C:\Users\Mike\Desktop\Arduino\hardware -tools C:\Users\Mike\Desktop\Arduino\tools-builder -tools C:\Users\Mike\Desktop\Arduino\hardware\tools\avr -built-in-libraries C:\Users\Mike\Desktop\Arduino\libraries -libraries C:\Users\Mike\Documents\Arduino\libraries -fqbn=arduino:avr:uno -ide-version=10802 -build-path C:\Users\Mike\AppData\Local\Temp\arduino_build_476378 -warnings=none -build-cache C:\Users\Mike\AppData\Local\Temp\arduino_cache_737176 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=C:\Users\Mike\Desktop\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Users\Mike\Desktop\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Users\Mike\Desktop\Arduino\hardware\tools\avr -verbose C:\Users\Mike\Documents\Arduino 1\Combined_44\Combined_44.ino
Using board 'uno' from platform in folder: C:\Users\Mike\Desktop\Arduino\hardware\arduino\avr
Using core 'arduino' from platform in folder: C:\Users\Mike\Desktop\Arduino\hardware\arduino\avr
Detecting libraries used...
"C:\Users\Mike\Desktop\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Users\Mike\Desktop\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Users\Mike\Desktop\Arduino\hardware\arduino\avr\variants\standard" "C:\Users\Mike\AppData\Local\Temp\arduino_build_476378\sketch\Combined_44.ino.cpp" -o "nul"
"C:\Users\Mike\Desktop\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Users\Mike\Desktop\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Users\Mike\Desktop\Arduino\hardware\arduino\avr\variants\standard" "C:\Users\Mike\AppData\Local\Temp\arduino_build_476378\sketch\HX711.cpp" -o "nul"
Generating function prototypes...
"C:\Users\Mike\Desktop\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Users\Mike\Desktop\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Users\Mike\Desktop\Arduino\hardware\arduino\avr\variants\standard" "C:\Users\Mike\AppData\Local\Temp\arduino_build_476378\sketch\Combined_44.ino.cpp" -o "C:\Users\Mike\AppData\Local\Temp\arduino_build_476378\preproc\ctags_target_for_gcc_minus_e.cpp"
"C:\Users\Mike\Desktop\Arduino\tools-builder\ctags\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\Users\Mike\AppData\Local\Temp\arduino_build_476378\preproc\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"C:\Users\Mike\Desktop\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Users\Mike\Desktop\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Users\Mike\Desktop\Arduino\hardware\arduino\avr\variants\standard" "C:\Users\Mike\AppData\Local\Temp\arduino_build_476378\sketch\Combined_44.ino.cpp" -o "C:\Users\Mike\AppData\Local\Temp\arduino_build_476378\sketch\Combined_44.ino.cpp.o"
C:\Users\Mike\Documents\Arduino 1\Combined_44\Combined_44.ino: In function 'void setup()':

Combined_44:28: error: 'ai0' was not declared in this scope

attachInterrupt(0, ai0, RISING);

^

Combined_44:32: error: 'ai1' was not declared in this scope

attachInterrupt(1, ai1, RISING);

^

C:\Users\Mike\Documents\Arduino 1\Combined_44\Combined_44.ino: In function 'void loop()':

Combined_44:46: error: 'cell_1' was not declared in this scope

val = cell_1.read(); // most recent reading

^

C:\Users\Mike\Documents\Arduino 1\Combined_44\Combined_44.ino: At global scope:

Combined_44:74: error: 'Serial' does not name a type

Serial.println((counter%720) * .5);

^

Combined_44:76: error: expected declaration before '}' token

}

^

exit status 1
'ai0' was not declared in this scope

Is this file

#include "HX711.h"

in the same folder as your combined sketch?

Or is it in your libraries folder? In which case you need

#include <HX711.h>

CrossRoads:
Is this file

#include "HX711.h"

in the same folder as your combined sketch?

Or is it in your libraries folder? In which case you need

#include <HX711.h>

It is in both-- if removed from the file (its own tab) it has error "HX711.h not in library" even though it is.
If the "#include HX711.h" is removed from my original sketch it gives error " HX711 does not name a type"

Thanks --Mike