########################
#Base Code
# 4tronix Arduino http:#www.4tronix.co.uk/arduino/Stepper-Motors.php
# from http://hp.hana-neko.com/archives/693
class M_28BYJ
#######################################
def initialize
# Serial.begin(9600)
@motorPin1 = 14 # Blue - 28BYJ48 pin 1
@motorPin2 = 15 # Pink - 28BYJ48 pin 2
@motorPin3 = 16 # Yellow - 28BYJ48 pin 3
@motorPin4 = 17 # Orange - 28BYJ48 pin 4
# Red - 28BYJ48 pin 5 (VCC)
@motorSpeed = 800 # Should be more than 800. 800 = MAX SPEED
@lookup = [0b01000, 0b01100, 0b00100, 0b00110, 0b00010, 0b00011, 0b00001, 0b01001]
@phase = 0
# pinMode(@motorPin1, 1)
# pinMode(@motorPin2, 1)
# pinMode(@motorPin3, 1)
# pinMode(@motorPin4, 1)
end
def anticlockwise
8.times do |i|
setOutput(i)
delay_us(@motorSpeed)
end
end
def clockwise
8.times do |i|
i_invert = ~i + 8 # 7, 6, .. , 1, 0
setOutput(i_invert)
delay_us(@motorSpeed)
end
end
def setOutput(out)
diff = @phase ^ @lookup[out]
digitalWrite(@motorPin1, bitRead(@lookup[out], 0)) if bitRead(diff,0)
digitalWrite(@motorPin2, bitRead(@lookup[out], 1)) if bitRead(diff,1)
digitalWrite(@motorPin3, bitRead(@lookup[out], 2)) if bitRead(diff,2)
digitalWrite(@motorPin4, bitRead(@lookup[out], 3)) if bitRead(diff,3)
@phase = out
end
def bitRead(x, n)
x & (2**n) > 0 ? 1 : 0
end
end
###################
count = 0 # count of steps made
countsperrev = 5120 # number of steps per full revolution
m = M_28BYJ.new
while 1
#digitalWrite(D10, 1)
#if(count < countsperrev )
m.anticlockwise
#elsif (count == countsperrev * 2)
# count = 0
#else
# m.anticlockwise
#end
#p "test"
#count+=1
end