Phase shifting sketch

Hi there.
I'd like to make a variable shifting in time of a pulse. The time range that i like to have control in is 10ms.
In the following sketch, i vary with a pot the x value and i store it at the begin of each loop to the "PreviousValueX" variable. After checking again the "x" value i compare it with the PreviousValueX" and if something changed then it gives me the new delay. If nothing changed then new delay is 0.

How do you see that? Is that a valid consideration? My oscilloscope shows that it works but i am not positive as i can't completely stabilize my waveform.

///////////////////////

void loop() {
PreviousValueX = x; // storage of previous x value
sensorValue = analogRead(PotPin);

x = map(sensorValue, 0, 1023, 0, 20);
PHASE =(x*500);

if (x != PreviousValueX)
{
delayMicroseconds(PHASE);
}
else if (x = y)
{
delayMicroseconds(0);
}

Incomplete code. Whole bunch of errors.

/Users/john/Documents/Arduino/sketch_jun03a/sketch_jun03a.ino: In function 'void loop()':
sketch_jun03a:3: error: 'PreviousValueX' was not declared in this scope
   PreviousValueX = x;                 // storage of previous x value
   ^
sketch_jun03a:3: error: 'x' was not declared in this scope
   PreviousValueX = x;                 // storage of previous x value
                    ^
sketch_jun03a:4: error: 'sensorValue' was not declared in this scope
   sensorValue = analogRead(PotPin);
   ^
sketch_jun03a:4: error: 'PotPin' was not declared in this scope
   sensorValue = analogRead(PotPin);
                            ^
sketch_jun03a:7: error: 'PHASE' was not declared in this scope
   PHASE = (x * 500);
   ^
sketch_jun03a:13: error: 'y' was not declared in this scope
   else if (x = y)
                ^
sketch_jun03a:16: error: expected '}' at end of input
   }
   ^
exit status 1
'PreviousValueX' was not declared in this scope

Dear John, thanks for answering but this is a part of the code. It just shows the way i tried to make the shifting. It is just the first lines in the loop. What i am interested in is if this is a valid way for varying the time before the rest of the code.

Jeg_1:
Dear John, thanks for answering but this is a part of the code. It just shows the way i tried to make the shifting. It is just the first lines in the loop. What i am interested in is if this is a valid way for varying the time before the rest of the code.

Many things look wrong to me. The assignment (x=y) looks particularly wrong. delayMicroseconds(0) also looks wrong. I can't see the declarations of any of your variables so they may look wrong. Without seeing all of the code I can't tell what you are trying to do and if you are doing that correctly.

I don't see any pulse being shifted here. What other code did you miss out? There's no output from this sketch to let you see if it's working or not.

I was expecting to see something like:

const int pinInput = 2;
const int pinOutput = 3;
int getDelay() {
  //get the delay time from the analog pin - I'm not going to write all the details here
  return 500;
}
void setup() {
  pinMode(pinInput, INPUT_PULLUP);
  pinMode(pinOutput, OUTPUT);
}
void loop() {
  static boolean lastValue = HIGH; //assume the input started at HIGH, not important if it doesn't
  boolean inputPulse = digitalRead(pinInput);
  if(inputPulse != lastValue) {
    delayMicroseconds(getDelay());
    digitalWriteFast(pinOutput, inputPulse);
    lastValue = inputPulse;
  }
}

Note that this version has a minimum delay defined by the time it takes to do the analogRead() and calculations inside getDelay(). You may wish to do it another way but that may introduce random delays if the pulse happens to change state during the time that you're off doing getDelay().

On most Arduinos delayMicroseconds() has a maximum resolution of 4. You can't delay for 3.5 microseconds, for example. You can get better timing (and less sensitivity to other time-consuming tasks in your sketch) by using interrupts and hardware timers. The TimerOne library may be useful here.

Sorry guys. Almost the full sketch is following. I have more outputs but i kept just one for more convenience.

The idea behind is to give a delay by my potentiometer, so to sync the pulse with an output of some other circuitry that i have. Once i sync it then i'd like to keep it delay-free for the rest of the operation. I don't want any more delay each time the loop starts from the begin. So once i find the right delay when sketch starts, then i would like to disappear delay from the "equation". What do you think?

Edit: John you were so right abut the y variable...

const int PotPin = A0;
int sensorValue = 0;
int x = 0;
int PreviousValueX = 0;
int y = 0;
int PHASE = 0;
void setup() {

pinMode(23, OUTPUT);
}

void loop() {
PreviousValueX = x; // storage of previous x value
sensorValue = analogRead(PotPin);

x = map(sensorValue, 0, 1023, 0, 100);
PHASE =(x*100);

if (x != PreviousValueX)
{
delayMicroseconds(PHASE);
}
else if (x = PreviousValueX)
{
delayMicroseconds(0);
}

y =139;

digitalWrite(23,HIGH);
delayMicroseconds(y);
digitalWrite(23,LOW);
delayMicroseconds(y);

It sounds like you are producing a pulse stream in software. By some miracle this pulse stream is exactly the same frequency as some external pulse stream. Then you want to manually adjust the phase difference between the two pulse streams. The phase of the two streams will then be maintained by the miracle of them having the exact same frequency. Is that what you are trying to do?

johnwasser:
It sounds like you are producing a pulse stream in software. By some miracle this pulse stream is exactly the same frequency as some external pulse stream. Then you want to manually adjust the phase difference between the two pulse streams. The phase of the two streams will then be maintained by the miracle of them having the exact same frequency. Is that what you are trying to do?

Yes i know that i already broke the law of conservation of miracles, but yes this is what i would to do. A manual phase shift at the begin of the program and then a delay free looping.. :slight_smile:

Jeg_1:
Sorry guys. Almost the full sketch is following. I have more outputs but i kept just one for more convenience.

How many in the final product? That might affect answers given now.