Hello
im about to start my first project.
i need to read voltage from two potentiometers at the same time and then loop out the same voltage trough 2 MCP4725 DACs (voltage are not the same on each pot)
i also need to temporary lower and higher the outputvalue and ignore pot readings when certain switches are active.
i managed to do a simple circuit with som buttons,resistors caps and some coding to read 2 pots and the output PWM signals. but the device that im trying to control does not acceting the PWM very well
const int adc1 = 0 ; //naming pin 0 of analog input side as ‘adc’
const int adc2 = 1 ; //naming pin 0 of analog input side as ‘adc’
const int pwm1 = 2 ; //naming pin 5 as ‘pwm’ variable
const int pwm2 = 4 ; //naming pin 3 as ‘pwm’ variable
int potPin1 = A0;
int potPin2 = A1;
int pushButton1 = 50; //naming pin 8 as "pushbutton
int pushButton2 = 52; //naming pin 8 as "pushbutton
int resetPin = 14;
void setup() {
Serial.begin(9600);
pinMode(potPin1, INPUT);
pinMode(potPin2, INPUT);
pinMode(pwm1,OUTPUT) ; //setting pin 2 as output'
pinMode(pwm2,OUTPUT) ; //setting pin 2 as output
pinMode(52,INPUT_PULLUP);
pinMode(50,INPUT_PULLUP);
}
void loop() {
//read the pushbutton value into a variable
int sensorVal5 = digitalRead(52);
int sensorVal4 = digitalRead(50);
//print out the value of the pushbutton
Serial.println(sensorVal5);
Serial.println(sensorVal4);
// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
// HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
// button's pressed, and off when it's not:
if (sensorVal5 == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
}
if (sensorVal4 == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
}
int adc1 = analogRead(0) ; //reading analog voltage (0)
int adc2 = analogRead(1) ; //reading analog voltage (1)
int ao = analogRead(1) ;
Serial.println(adc2);
Serial.println(adc1);
if (sensorVal5 == LOW && sensorVal4 == HIGH) {
adc1 = map(adc1, 0, 3000, 0, 255) ;
analogWrite(pwm1,adc1) ;
adc2 = map(adc2, 0, 2500, 0, 255) ;
analogWrite(pwm2,adc2) ;
} else {
adc1 = map(adc1, 0, 1023, 0, 255) ;
analogWrite(pwm1,adc1) ;
adc2 = map(adc2, 0, 1023, 0, 255) ;
analogWrite(pwm2,adc2) ;
}
if (sensorVal5 == HIGH && sensorVal4 == LOW && ao <=250) {
adc1 = map(adc1, -2000, 1023, 0, 255) ;
analogWrite(pwm1,adc1) ;
adc2 = map(adc2, -2000, 1023, 0, 255) ;
analogWrite(pwm2,adc2) ; }
basicly pot value1 and 2 are looped trough the arduino and outputs the scaled value 0-1023 in PWM 0-255
but when one of the pushbuttons are pressed, the input scaling differs and the output lowers depending on switch or rises the scaing if the other switch and ao value is lower then 250.
now to my problem.
i manage to read the pots and output som kind of linear value on the DACS but i dont know how to get the map function included in the buffer values
#include <Wire.h> // specify use of Wire.h library
#define MCP47251 0x62 // MCP4725 base address
#define MCP47252 0x63 // MCP4725 base address
int val1 = 0 ;
int val2 = 1 ;
byte buffer[3];
void setup() {
Wire.begin(); // begin I2C
} // end setup
void loop() {
buffer[0] = 0b01000000; // control byte
val1 = analogRead(0) * 4; // read pot
buffer[1] = val1 >> 4; // MSB 11-4 shift right 4 places
buffer[2] = val1 << 4; // LSB 3-0 shift left 4 places
Wire.beginTransmission(MCP47251); // address device
Wire.write(buffer[0]); // pointer
Wire.write(buffer[1]); // 8 MSB
Wire.write(buffer[2]); // 4 LSB
Wire.endTransmission();
buffer[0] = 0b01000000; // control byte
val2 = analogRead(1) * 4; // read pot
buffer[1] = val2 >> 4; // MSB 11-4 shift right 4 places
buffer[2] = val2 << 4; // LSB 3-0 shift left 4 places
Wire.beginTransmission(MCP47252); // address device
Wire.write(buffer[0]); // pointer
Wire.write(buffer[1]); // 8 MSB
Wire.write(buffer[2]); // 4 LSB
Wire.endTransmission();
does anybodu understand what i need help with? im stuck