My bad, I thought it was hardware related problem so i didn't post my code.
#include <SPI.h>
const int slaveSelectPin1 = 10;
const int slaveSelectPin2 = 12;
const int slaveSelectPin3 = 9;
const int sw1 = 2;
const int sw2 = 3;
const int sw3 = 4;
const int sw4 = 5;
const int sw5 = 6;
const int sw6 = 7;
const int sw7 = 8;
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(115200);
// set the slaveSelectPin as an output:
pinMode (slaveSelectPin1, OUTPUT);
pinMode (slaveSelectPin2, OUTPUT);
pinMode (slaveSelectPin3, OUTPUT);
pinMode (sw1, OUTPUT);
pinMode (sw2, OUTPUT);
pinMode (sw3, OUTPUT);
pinMode (sw4, OUTPUT);
pinMode (sw5, OUTPUT);
pinMode (sw6, OUTPUT);
pinMode (sw7, OUTPUT);
// initialize SPI:
//SPI.begin();
//make sure all select pins are low
digitalWrite(slaveSelectPin1,LOW);
digitalWrite(slaveSelectPin2,LOW);
digitalWrite(slaveSelectPin3,LOW);
//Set default switch states
digitalWrite(sw1,LOW);
digitalWrite(sw2,LOW);
digitalWrite(sw3,LOW);
digitalWrite(sw4,LOW);
digitalWrite(sw5,LOW);
digitalWrite(sw6,LOW);
digitalWrite(sw7,LOW);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV16); //UNO has 16MHz clock
printPrompt();
}
// the loop routine runs over and over
void loop() {
int input;
// delay(2000);
if (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
input = Serial.parseInt();
Serial.println(input);
if ((input >= 100) && (input < 132)){
spiWrite(slaveSelectPin1,(input - 100)*2);
Serial.print("attenuator 1 adjusted \n\n");
printPrompt();
}
if ((input >= 200) && (input < 232)){
spiWrite(slaveSelectPin2,(input - 200)*2);
Serial.print("attenuator 2 adjusted \n\n");
printPrompt();
}
if ((input >= 300) && (input < 332)){
spiWrite(slaveSelectPin3,(input - 300)*2);
Serial.print("attenuator 3 adjusted \n\n");
printPrompt();
}
if ((input >= 10) && (input < 12)){
digitalWrite(sw1,input-10);
Serial.print("switch 1 written \n\n");
printPrompt();
}
if ((input >= 20) && (input < 22)){
digitalWrite(sw2,input-20);
Serial.print("switch 2 written \n\n");
printPrompt();
}
if ((input >= 30) && (input < 32)){
digitalWrite(sw3,input-30);
Serial.print("switch 3 written \n\n");
printPrompt();
}
if ((input >= 40) && (input < 42)){
digitalWrite(sw4,input-40);
Serial.print("switch 4 written \n\n");
printPrompt();
}
if ((input >= 50) && (input < 52)){
digitalWrite(sw5,input-50);
Serial.print("switch 5 written \n\n");
printPrompt();
}
if ((input >= 60) && (input < 62)){
digitalWrite(sw6,input-60);
Serial.print("switch 6 written \n\n");
printPrompt();
}
if ((input >= 70) && (input < 72)){
digitalWrite(sw7,input-70);
Serial.print("switch 7 written \n\n");
printPrompt();
}
}
}
void printPrompt(){
Serial.print("__________________________________________ \n");
Serial.print("Enter Command \n");
Serial.print("10 thru 71 to set one of 7 switches (10*sw# + state) \n");
Serial.print("100 to 132 for attenuator1 (100 + gain) \n");
Serial.print("200 to 232 for attenuator2 (200 + gain) \n");
Serial.print("300 to 332 for attenuator3 (300 + gain) \n");
}
void spiWrite(int slave, int value) {
// send in the value via SPI to the shift register
// initialize SPI:
SPI.begin();
byte value_b = byte(value); //convert to byte. no need to shift.2 bits will fall off automatically.
SPI.transfer(value_b);
// close SPI:
SPI.end();
// pulse the LE on slave to latch the data in the shift registers
digitalWrite(slave,HIGH);
delay(100);
digitalWrite(slave,LOW);
}
So as a result, I read some other post and it seems like initializing SPI changes pinmode of pin 12.
So I moved SPI.begin(); to where I actually send serial data.
component I'm using is mini circuit digital attenuator. (zx76-31r5-sp)
pin 12 is connected as "latch enable" to the digital attenuator.
After I moved SPI.begin() and end it after sending serial data, things work fine now.
However, I'm still open to any other suggestion or improvement.
Thank you.