#include <Wire.h>
int potPin1 = 0; // select the input pin for the potentiometer
int potPin2 = 1;
int potVal1; // variable to store the value coming from the sensor
int potVal2;
int times=0;
int state=0;
int lastState=0;
boolean pot=false;
void setup()
{
pinMode(13, OUTPUT);
pinMode(3, INPUT);
Serial.begin(9600);
Wire.begin();
}
void loop(){
state=digitalRead(3);
if(state != lastState){
if(state==HIGH){
times++;
Serial.println(times);
}
else{
Serial.println(“off”);
}
}
lastState=state;
if(times%2 ==1)
{
turnPotOn();
}
else
{
turnPotOff();
}
if(pot==true)
{
potVal1 = analogRead(potPin1); // read the value from the sensor
potVal2 = analogRead(potPin2);
if((potVal1>640) && (336<potVal2) && (potVal2<683))
{
arduino1_motor1();
}
else if ((potVal1<330) && (336<potVal2) && (potVal2<683))
{
arduino1_motor2();
}
else
{
arduino1_still();
}
}
else
{
arduino1_still();
Serial.println(“OFF”);
}
}
void turnPotOff()
{
digitalWrite(13, LOW);
pot=false;
}
void turnPotOn()
{
digitalWrite(13, HIGH);
pot=true;
}
void arduino1_motor1()
{
Wire.beginTransmission(5);
Wire.write(‘A’);
Wire.endTransmission();
Serial.println(“A1 in M1 d”);
}
void arduino1_motor2()
{
Wire.beginTransmission(5);
Wire.write(‘B’);
Wire.endTransmission();
Serial.println(“A1 in m2 d”);
}
void arduino1_still()
{
still();
Serial.println(“A1 stl”);
}
void still()
{
Wire.beginTransmission(5);
Wire.write(‘S’);
Wire.endTransmission();
}