Programming in Arduino Uno

Hi, can someone help me with my code?

The goal is to read two analog signal from two current sensors on pins A1 and A2. These will then be compared in a function that i called compareValue() and then there will be a output signal in digital pin 7.

Can someone check my code if it looks correct or is there something missing? the void loop() must read the values continuously so that the output signal always varies.

We have a master and a slave and the goal is to compare the two signals and send out a corrective signal to the slave so that slave and master are equal.

example: Master =5, slave=1, to correct this a signal with value 5 must be sendt out, in this case newValueSlave.

CODE:

int sensorValueMaster =0; 
int sensorValueSlave = 0;
// int sensorValueTorque = 0;
int newValueSlave = 0;
//int newMaster = 0;

int sensorPinMaster = A1; //analog input
int sensorPinSlave = A2; // analog input
int newSlave = 7; // digital output going to mk-slave
//int sensorPinTorque = A3; // analog input
//int newValueMaster = 7; // digital output from mc to mk-master




void setup(){
  Serial.begin(9600);
  pinMode(sensorPinMaster, INPUT);
  pinMode(sensorPinSlave, INPUT);
  // pinMode(sensorPinTorque, INPUT);
  pinMode(newSlave, OUTPUT);
}

void loop(){
  int sensorValueMaster;
  int sensorValueSlave;
  int newValueSlave;
  sensorValueMaster = analogRead(sensorPinMaster);
  sensorValueSlave = analogRead(sensorPinSlave); 
  newValueSlave = compareValue(sensorValueMaster, sensorValueSlave);
  digitalWrite(newSlave, newValueSlave);


}

int compareValue(int x, int y){
  // Compare values from Master and Slave and send a signal that corrects the slave singal as a analog output A4
  int result;

  if ( x = y){
    result =y;
    return result;
  }
  else if( x < y){
    result = (x-y)+y;
    return result;
  }
  else if ( x > y){
    result = (x-y)+y;
    return result;
  }
  return 0;
}

Any feedback is welcome. Thanks

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

  digitalWrite(newSlave, newValueSlave);

The second argument to the digitalWrite() function is HIGH or LOW. Any non-zero value is going to have EXACTLY the same affect on the pin. It will be turned on.

Whether that value is the logically correct 1 or 5 or 28 or 937 makes no difference. On is on.

You can't use a digital pin to "send a corrective signal".

taz__90:
Hi, can someone help me with my code?

int compareValue(int x, int y){

// Compare values from Master and Slave and send a signal that corrects the slave singal as a analog output A4
 int result;

if ( x = y){
   result =y;
   return result;
 }
 else if( x < y){
   result = (x-y)+y;
   return result;
 }
 else if ( x > y){
   result = (x-y)+y;
   return result;
 }
 return 0;
}

(x-y)+y = x, whatever values you chose.

  if ( x = y){

= is assignment.
== is equality.

Have I got this correct?
You set the motor speeds and start the motors.
You then compare the actual speeds of the two motors.
You then adjust the set speed of one motor by the difference in actual speeds, bringing the actual speeds into sync.

One motor, in this case Master, get a signal generated from a pulse generator(at the moment).
This signal is then read by arduino uno and compared with the signal from Slave. At startup all sensor values = 0 so the comparison will go to
if (x==y)
return x;

or at least thats the point.

Also, this signal that is suposed to correct the Slave has to be a PWM signal from port 3,5,6,9,10,11 so i figured that out. And it has to be sent from digital port 3 or 11. In the datasheet it says that the normal frequency generated from the PWM port is about 490 hz. which is way to high when running the motors i got.

I need to get a method that lowers the frequenzy to about 32 hz and from this link:
http://playground.arduino.cc/Main/TimerPWMCheatsheet
http://arduino-info.wikispaces.com/Arduino-PWM-Frequency

it should be possible to do this by using timers and counters.

analogWrite(myPWMpin, 192); Outputs a square wave, i want to send out a square wawe with 75% duty cycle and frequenzy to abaout 32 Hz. The the newSlave value should be correct working.

The newSlave value is then x, form my compare method( which is rubbish), and with a PWM signal of 75% duty and about 32 Hz.

Anyone know if this makes sense?

Thanks for any response!

taz__90:
One motor, in this case Master, get a signal generated from a pulse generator(at the moment).

So that is constant.

This signal is then read by arduino uno and compared with the signal from Slave.

How is the signal from the slave derived and what does it represent?