An apparatus for controlling spinal cord stimulation to a patient comprising: a first array of electrodes embedded in a flexible substrate; a second array of electrodes embedded in the flexible substrate; the first array of electrodes and the second array of electrodes arranged in laterally opposed to positions; a set of photoemitters embedded in the flexible substrate; a first set of photodetectors embedded in the flexible substrate; a second set of photodetectors embedded in the flexible substrate; a signal processing unit comprising: a central processing unit and a memory, a pulse generator, in communication with the central processing unit, connected to the first and second array of electrodes, and an optical signal processor, in communication with the central processing unit, connected to the set of photoemitters, the first set of photodetectors and the second set of photodetectors; a calibration table resident in the memory; a stimulation controller in communication with the signal processing unit and programmed to control the operation of the signal processing unit; wherein the flexible substrate is configured to be implanted in the epidural space adjacent to the spinal cord of the patient and positioned such that light emitted from the set of photoemitters is reflected from the spinal cord of the patient into the first and second set of photodetectors, wherein the signal processing unit is further programmed to carry out the steps of: measuring a first photodetector value for a photodetector in the first set of photodetectors; measuring a second photodetector value for a photodetector in the second set of photodetectors; determining a position of the spinal cord from detected light intensities of the first photodetector value and the second photodetector value; determining a group of electrode settings for at least one electrode in the first array of electrodes and at least one electrode in the second array of electrodes based on the first and second photodetector values and the calibration table; adjusting the pulse generator based on the group of electrode settings; and, initiating an electric pulse according to the group of electrode settings to provide spinal cord stimulation. a first array of electrodes embedded in a flexible substrate; a second array of electrodes embedded in the flexible substrate; the first array of electrodes and the second array of electrodes arranged in laterally opposed to positions; a set of photoemitters embedded in the flexible substrate; a first set of photodetectors embedded in the flexible substrate; a second set of photodetectors embedded in the flexible substrate; a signal processing unit comprising: a central processing unit and a memory, a pulse generator, in communication with the central processing unit, connected to the first and second array of electrodes, and an optical signal processor, in communication with the central processing unit, connected to the set of photoemitters, the first set of photodetectors and the second set of photodetectors; a central processing unit and a memory, a pulse generator, in communication with the central processing unit, connected to the first and second array of electrodes, and an optical signal processor, in communication with the central processing unit, connected to the set of photoemitters, the first set of photodetectors and the second set of photodetectors; a calibration table resident in the memory; a stimulation controller in communication with the signal processing unit and programmed to control the operation of the signal processing unit; wherein the flexible substrate is configured to be implanted in the epidural space adjacent to the spinal cord of the patient and positioned such that light emitted from the set of photoemitters is reflected from the spinal cord of the patient into the first and second set of photodetectors, wherein the signal processing unit is further programmed to carry out the steps of: measuring a first photodetector value for a photodetector in the first set of photodetectors; measuring a second photodetector value for a photodetector in the second set of photodetectors; determining a position of the spinal cord from detected light intensities of the first photodetector value and the second photodetector value; determining a group of electrode settings for at least one electrode in the first array of electrodes and at least one electrode in the second array of electrodes based on the first and second photodetector values and the calibration table; adjusting the pulse generator based on the group of electrode settings; and, initiating an electric pulse according to the group of electrode settings to provide spinal cord stimulation. measuring a first photodetector value for a photodetector in the first set of photodetectors; measuring a second photodetector value for a photodetector in the second set of photodetectors; determining a position of the spinal cord from detected light intensities of the first photodetector value and the second photodetector value; determining a group of electrode settings for at least one electrode in the first array of electrodes and at least one electrode in the second array of electrodes based on the first and second photodetector values and the calibration table; adjusting the pulse generator based on the group of electrode settings; and, initiating an electric pulse according to the group of electrode settings to provide spinal cord stimulation.