---
# System prepended metadata

title: Mega2560 potentiometer simulation
tags: [Arduino]

---

###### tags: `Arduino`
# Mega2560 potentiometer simulation

Using PWM to control the ouput voltage of digital pin for one **Arduino Mega2560**.

## pinout
|Board|SCL|SDA|
|-|-|-|
|Uno|A5|A4|
|Nano|A5|A4|
|Mega2560|21|20|

## Index control by code
```cpp=
float index[12] = {0,0,0,0,0,0,0,0,0,0,0,0};  //Volt
void setup(){
    PORTD = B11111100; //output
    PORTB = B111111;
}

int volt(float n, float m){            //(target volt, maximum volt)
    return int(map(n,0,m,0,255));
}

void loop(){
    for(int i=0;i<12;i++)
        analogWrite(i+2, volt(index[i],5.8));
    delay(10);
}
```

## Index control by Serial port
```cpp=
float index[12] = {0,0,0,0,0,0,0,0,0,0,0,0};  //Volt

String mode = "";
int pin = 2;
void setup(){
    PORTD = B11111100;
    PORTB = B111111;
    Serial.begin(9600);
}

void loop(){
    if(Serial.available()){
        float value = Serial.parseFloat();  //value from 10~58
        int tmp = int(value);
        switch(tmp){    //mode switch
            case 0:
                mode = "pin";
                Serial.println("pin");   //pin select
                break;
            case 1:
                mode = "value";
                Serial.println("value");   //value set
                break;
            default:
                if(mode == "pin"){
                    pin = tmp;
                    Serial.print("pin change ");
                    Serial.println(pin);
                }
                if(mode == "value"){
                    index[pin-2] = value/10;   // value/=10
                    Serial.print("value change ");
                    Serial.println(value/10);
                }
        }
    }
    for(int i=0;i<12;i++)
        analogWrite(i+2, map(index[i],0,5.8,0,255));
    delay(100);
}
```

# Multi board control by Serial

## Commend
* -1 : slave board select
* -2 : pin change select
* -3 : value change order (from 10~58)

## Master (for any board)
```cpp=
#include <Wire.h>

String mode = "";
int slv_sel = 0;   //slave select
byte pin_sel = 0;   //pin select


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

void loop(){
    if(Serial.available()){
        float value = Serial.parseFloat();
        int tmp = int(value);
        switch(tmp){
            case -1:
                mode = "slave select";
                Serial.println(mode);
                break;
            case -2:
                mode = "pin select";
                Serial.println(mode);
                break;
            case -3:
                mode = "value change";
                Serial.println(mode);
                break;
            default:
                if(mode == "slave select"){
                    slv_sel = tmp;
                    Serial.print("slave select ");
                    Serial.println(slv_sel);
                }
                if(mode == "pin select"){
                    pin_sel = tmp;
                    Serial.print("pin select ");
                    Serial.println(pin_sel);
                }
                if(mode == "value change"){
                    byte x = tmp;
                    Wire.beginTransmission(slv_sel);
                    Wire.write(pin_sel);
                    Wire.write(x);
                    Wire.endTransmission();
                    Serial.print("value change ");
                    Serial.println(x);
                }
                break;
        }
    }
    delay(100);
}
```

## Slave template (for Mega2560)
```cpp=
#include <Wire.h>

float data[12] = {0,0,0,0,0,0,0,0,0,0,0,0};   //value data

int pin = 0;
float val = 0;
int add = 1;   //slave add

void setup(){
    DDRD = B111111;
    DDRB = B11111100;
    Wire.begin(add);
    Wire.onReceive(receive);
    Serial.begin(9600);
}

void loop(){
    for(int i=0;i<12;i++){
        analogWrite(i+2,map(data[i],0,5.8,0,255));
    }
    delay(100);
}

void receive(int n){
    pin = Wire.read();
    val = Wire.read();
    val /= 10;
    data[pin-2] = val;
    Serial.print("pin select "); Serial.println(pin);
    Serial.print("val change "); Serial.println(val);
}
```