Midi and 4051 multiplexer

Hello, finally I sloved how to use 4051 and I tried to read values of pots and send it as midi. Everything works fine till I describe 3rd pot. 1st pot and 2nd work fine, 3rd works “sometime” (when i rotate it works 1 or 2 times). Im sure that wiring is ok because i tried to describe 1st pot, 3rd pot and 2nd ( in this order) and 2nd don’t work. Here is my code. I know it’s messy but I don’t want to use arrays (and i don’t know how to xD).

#include <MIDI.h>
int analogValue = 0;
int lastAnalogValue = 128;
int analogValue2 = 0;
int lastAnalogValue2 = 128;
int analogValue3 = 0;
int lastAnalogValue3 = 128;

#include "analogmuxdemux.h"
#define READPIN 0 //analog pin 0
#define NO_PINS 3 //count of pins used in 4051
AnalogMux amux(10,9,8, READPIN); //pins on arduino

void setup() {

void loop() {
  //pot 1 works fine
  for (int pinno=0; pinno < 1; pinno++){
    amux.SelectPin(pinno); //pin on 4051 (0)
  int analogValue = analogRead(pinno)/8;     
  if ((analogValue-lastAnalogValue) > 1 || (analogValue-lastAnalogValue) < -1) {
    if (analogValue != lastAnalogValue) {
      MIDI.sendControlChange(1, analogValue, 1);
      lastAnalogValue = analogValue;
//pot 2 works fine
  for (int pinno2=1; pinno2 <2; pinno2++){
  int analogValue2 = analogRead(pinno)/8;     
  if ((analogValue2-lastAnalogValue2) > 1 || (analogValue2-lastAnalogValue2) < -1) {
    if (analogValue2 != lastAnalogValue2) {
      MIDI.sendControlChange(1, analogValue2, 1);
      lastAnalogValue2 = analogValue2;
//pot 3 ;-; im crying
  for (int pinno3=2; pinno3 < 3; pinno3++){
  int analogValue3 = analogRead(pinno3)/8;     
  if ((analogValue3-lastAnalogValue3) > 1 || (analogValue3-lastAnalogValue3) < -1) {
    if (analogValue3 != lastAnalogValue3) {
      MIDI.sendControlChange(1, analogValue3, 1);
      lastAnalogValue3 = analogValue3;


Read the code carefully, you’ll see both the mistake(s) and why its always
a wise idea to avoid repeating the same piece of code multiple times (package the
functionality in a function and call it 3 times - that way all three versions will work
once the function is correct.

Hint: look at the analogRead()'s

I see that I missed pinno 2 in second pot but i was rewritting code here (I lost a pice of original code but it's mixed code from other project). So that can't be fault . @edit Ummm maybe you can tell me how to fix it

    MIDI.sendControlChange(1, analogRead(pinno) / 8, 1);

How I can change it to send only data when change happened? (it sends data indefinitely that make me unavible to use with DAW)