Rotory Encoder + MCP4812

Hey guy just wondering if someone would be able to help us on how to interface these two codes to work together what im trying to achieve is to use the rotory encoder example from hobbytronics
(http://www.hobbytronics.co.uk/arduino-tutorial6-rotary-encoder)

and use the ouput on pin nine to drive the DAC using this code from http://hacking.majenko.co.uk/MCPDAC
to give us better resolution which would be feed into an non inverting amplifer to achive a 0 -10v signal could anyone give us some tips on how to code this as im very green cheers in advance

Rotory encoder sketch

int brightness = 0;    // how bright the LED is, start at half brightness
int fadeAmount = 5;    // how many points to fade the LED by
unsigned long currentTime;
unsigned long loopTime;
const int pin_A = 7;  
const int pin_B = 6;  
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev=0;

void setup()  {
  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);
  pinMode(pin_A, INPUT);
  pinMode(pin_B, INPUT);
  currentTime = millis();
  loopTime = currentTime; 
} 

void loop()  {
  // get the current elapsed time
  currentTime = millis();
  if(currentTime >= (loopTime + 2.5)){
    // 5ms since last check of encoder = 200Hz  
    encoder_A = digitalRead(pin_A);    // Read encoder pins
    encoder_B = digitalRead(pin_B);   
    if((!encoder_A) && (encoder_A_prev)){
      // A has gone from high to low 
      if(encoder_B) {
        // B is high so clockwise
        // increase the brightness, dont go over 255
        if(brightness + fadeAmount <= 255) brightness += fadeAmount;               
      }   
      else {
        // B is low so counter-clockwise      
        // decrease the brightness, dont go below 0
        if(brightness - fadeAmount >= 0) brightness -= fadeAmount;               
      }   

    }   
    encoder_A_prev = encoder_A;     // Store value of A for next time    
    
    // set the brightness of pin 9:
    analogWrite(9, brightness);   
   
    loopTime = currentTime;  // Updates loopTime
  }
  // Other processing can be done here
                           
}

DAC Sketch

// MCPDAC relies on SPI.
#include <SPI.h>
#include <MCPDAC.h>

void setup()
{
  // CS on pin 10, no LDAC pin (tie it to ground).
  MCPDAC.begin(10);
  
  // Set the gain to "HIGH" mode - 0 to 4096mV.
  MCPDAC.setGain(CHANNEL_A,GAIN_HIGH);
  
  // Do not shut down channel A, but shut down channel B.
  MCPDAC.shutdown(CHANNEL_A,false);
  MCPDAC.shutdown(CHANNEL_B,true);
}

void loop()
{
  static unsigned int volts;
  
  // Set the voltage of channel A.
  MCPDAC.setVoltage(CHANNEL_A,volts&0x0fff);

  // Increase the voltage in steps of 100mV.
  volts+=100;
}

Given the two sketches worked for you and also given that the forum system didn't hide characters because you didn't use code tags, this should be what you're looking for:

// MCPDAC relies on SPI.
#include <SPI.h>
#include <MCPDAC.h>


int brightness = 0;    // how bright the LED is, start at half brightness
int fadeAmount = 5;    // how many points to fade the LED by
unsigned long currentTime;
unsigned long loopTime;
const int pin_A = 7;  
const int pin_B = 6;  
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev=0;

void setup()  {
  // CS on pin 10, no LDAC pin (tie it to ground).
  MCPDAC.begin(10);
  
  // Set the gain to "HIGH" mode - 0 to 4096mV.
  MCPDAC.setGain(CHANNEL_A,GAIN_HIGH);
  
  // Do not shut down channel A, but shut down channel B.
  MCPDAC.shutdown(CHANNEL_A,false);
  MCPDAC.shutdown(CHANNEL_B,true);
  pinMode(pin_A, INPUT);
  pinMode(pin_B, INPUT);
  currentTime = millis();
  loopTime = currentTime;
}

void loop()  {
  // get the current elapsed time
  currentTime = millis();
  if(currentTime >= (loopTime + 2.5)){
    // 5ms since last check of encoder = 200Hz  
    encoder_A = digitalRead(pin_A);    // Read encoder pins
    encoder_B = digitalRead(pin_B);  
    if((!encoder_A) && (encoder_A_prev)){
      // A has gone from high to low
      if(encoder_B) {
        // B is high so clockwise
        // increase the brightness, dont go over 4095
        if(brightness + fadeAmount <= 0x0FFF) brightness += fadeAmount;              
      }  
      else {
        // B is low so counter-clockwise      
        // decrease the brightness, dont go below 0
        if(brightness - fadeAmount >= 0) brightness -= fadeAmount;              
      }  

    }  
    encoder_A_prev = encoder_A;     // Store value of A for next time    
    
    MCPDAC.setVoltage(CHANNEL_A,brightness&0x0fff);
  
    loopTime = currentTime;  // Updates loopTime
  }
  // Other processing can be done here
                          
}

whoops forgot the code tag changed now :* Thanks so much for doing that mate but unfortunately it doesnt seem to be working ive double checked with just the two original sketches and they seemed fine just wanted to double check my wiring any ideas :~

Do I understand you correctly, the two posted sketches work for itself but the combined one doesn't?

What the result of the sketch? Nothing? What were the results of the original sketches?

thats correct the two sketches work fine by themselves
Rotory encoder -i was able to output a pwm signal which i filtered which gave us a 0 -5v and the encoder worked fine both ways

DAC uploaded sketch worked fine which i checked on the scope which gave a ramped waveform

Try this one and check the PWM output on pin 9. Don't forget you have to turn 16 times as much to have the same effect because you wanted to have more resolution.

// MCPDAC relies on SPI.
#include <SPI.h>
#include <MCPDAC.h>


int brightness = 0;    // how bright the LED is, start at half brightness
int fadeAmount = 5;    // how many points to fade the LED by
unsigned long currentTime;
unsigned long loopTime;
const int pin_A = 7;  
const int pin_B = 6;  
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev=0;

void setup()  {
  // CS on pin 10, no LDAC pin (tie it to ground).
  MCPDAC.begin(10);
  
  // Set the gain to "HIGH" mode - 0 to 4096mV.
  MCPDAC.setGain(CHANNEL_A,GAIN_HIGH);
  
  // Do not shut down channel A, but shut down channel B.
  MCPDAC.shutdown(CHANNEL_A,false);
  MCPDAC.shutdown(CHANNEL_B,true);
  pinMode(pin_A, INPUT);
  pinMode(pin_B, INPUT);
  pinMode(9, OUTPUT);
  currentTime = millis();
  loopTime = currentTime;
}

void loop()  {
  // get the current elapsed time
  currentTime = millis();
  if(currentTime >= (loopTime + 2.5)){
    // 5ms since last check of encoder = 200Hz  
    encoder_A = digitalRead(pin_A);    // Read encoder pins
    encoder_B = digitalRead(pin_B);  
    if((!encoder_A) && (encoder_A_prev)){
      // A has gone from high to low
      if(encoder_B) {
        // B is high so clockwise
        // increase the brightness, dont go over 4095
        if(brightness + fadeAmount <= 0x0FFF) brightness += fadeAmount;              
      }  
      else {
        // B is low so counter-clockwise      
        // decrease the brightness, dont go below 0
        if(brightness - fadeAmount >= 0) brightness -= fadeAmount;              
      }  

    }  
    encoder_A_prev = encoder_A;     // Store value of A for next time    
    
    MCPDAC.setVoltage(CHANNEL_A,brightness&0x0fff);
    analogWrite(9, brightness >> 4);
  
    loopTime = currentTime;  // Updates loopTime
  }
  // Other processing can be done here
                          
}

Hey mate thanks so much for that absolute legend Thumbs Up ended up changing the loop time part to 1 instead of 2.5 worked a treat XD

void loop()  {
  // get the current elapsed time
  currentTime = millis();
  if(currentTime >= (loopTime + 1)){
    // 5ms since last check of encoder = 200Hz

just so i can learn so what i had to do was change the 255 to 4096 but in HEX format

if(brightness + fadeAmount <= 0x0FFF) brightness += fadeAmount;

and add this

 MCPDAC.setVoltage(CHANNEL_A,brightness&0x0fff);

plus all the void setup stuff from the combined sketches