Back to Parent

\\ARDUINO CODE

int pot_pin = A0;   // Initializing the Potentiometer pin

int pot_output;     // Declaring a variable for potentiometer output
const byte interruptEnter = 2;
const byte interruptBack = 3;
volatile byte stateEnter=LOW;
volatile byte stateBack=LOW;
int Enter=0;
int Back=0;
int myArray[3];
int oldEnter=0;
int oldBack=0;
int old_pot=0;
int diff=0;
//__________________________________________________________________________________________________________________


void setup ( ) {
pinMode(interruptEnter, INPUT_PULLUP);
pinMode(interruptBack, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptEnter), enter, CHANGE);
attachInterrupt(digitalPinToInterrupt(interruptBack), back, CHANGE);
Serial.begin(9600);       // Starting the serial communication at 9600 baud rate

} 

//__________________________________________________________________________________________________________________

void loop ( ) { 

pot_output = analogRead (pot_pin); // Reading from the potentiometer

int pot_mapped_output = map (pot_output, 0, 1023, 0, 255); // Mapping the output of potentiometer to 0-255 to be read by the Processing IDE 

if (stateEnter==HIGH)
{
  Enter=1;
  stateEnter=LOW;
}

if (stateBack==HIGH)
{
  Back=1;
  stateBack=LOW;
}
myArray[0]=Enter;
myArray[1]=pot_mapped_output;
myArray[2]=Back;
diff=old_pot-pot_mapped_output;
if (oldEnter!=Enter || oldBack!=Back || diff>1 || diff<-1)
{
  String package=String(myArray[0])+","+String(myArray[1])+","+String(myArray[2]);
while(Serial.available()==0)
{  
}
  char getData = Serial.read();
  Serial.println(package);     // Sending the output to Processing IDE
}
oldEnter=Enter;
oldBack=Back;
old_pot=pot_mapped_output;
Enter=0;
Back=0;

delay(50);

}

//__________________________________________________________________________________________________________________

void enter() {
  stateEnter=digitalRead(interruptEnter);
}

//__________________________________________________________________________________________________________________

void back() {
  stateBack=digitalRead(interruptBack);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0