Saturday, 27 February 2016

Hacking a ridiculously cheap laser.

I bought a couple of really cheap lasers off of eBay for a project I wanted to try at codeclub (Mission impossible style burglar detector using LDRs and lasers). They were about £1.50 each from China. Like this one but cheaper.

They were not easy to use in the chassis they came in, having a momentary push button switch, and eating batteries, so I set about taking one apart.

The chassis needed a bit of hacksaw work to split, and then the controlling chip and laser came out as one. I jammed on the switch with a cable tie, and taped the +ve wire to the body with insulating tape. The spring on the chip is the -ve:




Not quite sure of the power requirements but I think I would power it through a ULN2003 and an external battery supply if I was to include it in a project. Claims to be 1mW with 3V input. My multimeter read 25mA being supplied from the arduino 3.3V pin.

Not sure what project to include it in.

Robot Chassis - PoliceBot project

My son and I spent a bit of time this evening trying to get a robot chassis working.

My daughter and I built it a week or so ago:




This evening my son and I had a go at programming it to do something:



We used my new favourite programming tool - ardublock:



My son found some blue and white LEDs in my kit box, so we decided to make it into a police car.





It was really easy to get going.







I had a few problems with dodgy connections so we ended up soldering the power connections from the battery to the motors.

The next step is to activate the ultrasonic sensor on the front of the chassis.

Tuesday, 23 February 2016

Dodgy HC-SR04 Ultrasonic Distance Sensors

I've had a few issues trying get ultrasonic sensors working recently.

I'd had no problems previously when I'd used Flowol software to get one working with the boat project, so initially I suspected it was me trying to use Ardublock that was the problem, or just lack of skilled wiring on my part.

Turns out it was 4 dodgy HC-SR04 I'd bought through Amazon from China.

I built the circuit and tried the Newping sample sketch on one of the Amazon HC-SR04s. No reading on the serial monitor when using the Newping (well actually a continous zero reading)

I tried each of the sensors in turn, and as soon as I put an older SR04 I had lying around in the circuit, I immediately got good numbers in.

God to isolate the problem, but sucks to have to either return the parts or write them off and buy more.

At least knowing I had a good sensors let me try out the Ardublock program again, this time with some success, although it wouldn't read above 20cm.




Tuesday, 16 February 2016

Arduino with Bluetooth and ArduBlock

My daughter has a holiday project to build a robot, so we are adding Arduino controlled features.

I've tried to get bluetooth working with the arduino before with the boat project. But failed for whatever reason.

I recently discovered Ardublock, which is great way to get a program working without getting bogged down in the typing. It tops S4A because you can upload to the Arduino, and see the actual code it is creating:


This worked well and had the pin 13 LED blinking.

I attached the HC-06 bluetooth chip to the arduino and loaded up the Bluetooth Serial Controller App.

Once the HC-06 was paired (easy to do if you're used to pairing speakers and stuff regularly) the app was able to send '5' to the Arduino and get the LED blinking.

Now do do a few more adventurous things with the robot.

Thursday, 14 January 2016

Tide Indicator Pi Project #9 - Calculation of Current Tide Completed (No bugs)

The last version I posted, v2.2, turned out not to work for all tide states, due to some maths errors. These have all been fixed and the code seems to work well.

I've think I've finally got the hang of updating to github, so here's the latest code:

tideproject/tidenow3.0.py

This is the output:

(datetime.datetime(2016, 1, 14, 21, 34, 13, 517280), u'10.5')
(datetime.datetime(2016, 1, 15, 4, 9, 13, 518325), u'1.9')
Tide is currently:  falling
Tidal Range =  -8.6
Current Tide :  9.55460304533


Next job is to have it running continuously and outputting to this webpage.

Sunday, 3 January 2016

Tide Indicator Pi Project #8 - Calculation of Current Tide Completed

The program below seems to work!

Output:

('Next: ', (datetime.datetime(2016, 1, 3, 6, 18, 23, 116073), u'4.3'), ' is ', datetime.timedelta(0, 21180, 2472), ' away. /n Previous: ', (datetime.datetime(2016, 1, 2, 23, 49, 23, 115191), u'8.1'), ' was ', datetime.timedelta(0, 2159, 998410), ' ago.')
('Sum of both gaps is ', datetime.timedelta(0, 23340, 882))
('Tide is Currently: ', 'falling')
('tide difference = ', -3.8)
('lower tide value', 4.299999999999999)
('Normalised Time =', 2159, 23340, 0.29060405051843885)
0.958070971113
('Current tide : ', 7.940669690228617)


Code:


#version 1.0
#This program pulls tide data from the ports of Jersey Website
#Under a licence from the UKHO
#
#It then calculates the current tide using a simplified sinusoidal harmonic approximation
#By finding the two tide data points either side of now and working out the current tide height


import urllib2
from bs4 import BeautifulSoup
from time import sleep
import datetime as dt
import math

#open site and grab html

rawhtml = urllib2.urlopen("http://www.ports.je/Pages/tides.aspx").read(40000)
soup = BeautifulSoup(rawhtml, "html.parser")


#get the tide data (it's all in tags)

rawtidedata = soup.findAll('td')


#parse all data points (date, times, heights) to one big list
#format of the list is [day,tm,ht,tm,ht,tm,lt,tm,lt]

n=0
parsedtidedata=[]
for i in rawtidedata: 
 parsedtidedata.append(rawtidedata[n].get_text())
 n += 1

#extract each class of data (day, time , height) to a separate list (there are 10 data items for each day)

tidetimes=[]
tideheights=[]
tideday=[]
lastdayofmonth=int(parsedtidedata[-10])

for n in range(0,lastdayofmonth*10,10):

 tideday.append(parsedtidedata[n])
 tidetimes.extend([parsedtidedata[n+1],parsedtidedata[n+3],parsedtidedata[n+5],parsedtidedata[n+7]])
 tideheights.extend([parsedtidedata[n+2],parsedtidedata[n+4],parsedtidedata[n+6],parsedtidedata[n+8]])

#get time now:

currentTime = dt.datetime.now()


#create a list of all the tide times as datetime objects:

dtTideTimes=[]
tideDataList=[]

for j in range (0,lastdayofmonth*4):
 #print tidetimes[j][0:2], tidetimes[j][3:6]
 if tidetimes[j]=='**':
  dtTideTimes.append('**')
 else:

  dtTideTimes.append(dt.datetime.now().replace(day=int(j/4+1), hour=int(tidetimes[j][0:2]), minute=int(tidetimes[j][3:5])))

 #make a tuple for each data point and add it to a list
 tupleHolder =(dtTideTimes[j], tideheights[j])
 tideDataList.append(tupleHolder)
 
 #print what we've got so far
# print tideDataList[j]

#find the two closest times in the list to now:

gap1 = abs(tideDataList[0][0] - currentTime)
gap2 = abs(tideDataList[0][0] - currentTime)
nearest1 = tideDataList[0]

#print gap1 

for j in range (0,lastdayofmonth*4):

 if (tideDataList[j][0] !="**"):                      
  gapx = abs(tideDataList[j][0] - currentTime) 

#check if the data point is the first or second nearest to now. 
#Generates the datapoints either side of now

  if (gapx <= gap1):                            
   nearest1 = tideDataList[j]            
   gap1 = gapx
  if (gap1 < gapx and gapx <= gap2): 
   nearest2 = tideDataList[j]                   
   gap2 = gapx             

#print (nearest1, gap1)
#print (nearest2, gap2)
#print (gap1+gap2)    

#and now the maths begins
#print ('tide height 1 = ', nearest1[1])
#print ('tide height 2 = ', nearest2[1])

#need to get them in order of time: (this works)

if nearest1[0] > nearest2[0]:
 nextDataPoint = nearest1
 prevDataPoint = nearest2
 gapToNext = gap1
 gapToPrev = gap2

else:
 nextDataPoint = nearest2
 prevDataPoint = nearest1
 gapToNext = gap2
 gapToPrev = gap1

gapSum = gapToNext + gapToPrev

print('Next: ', nextDataPoint,' is ',gapToNext, ' away. /n Previous: ', prevDataPoint, ' was ', gapToPrev, ' ago.')
print('Sum of both gaps is ', gapSum) #this works

#is the tide rising or falling?
tideDifference = float(nextDataPoint[1])-float(prevDataPoint[1])

if (tideDifference<0 data-blogger-escaped-0="prev" data-blogger-escaped-:="" data-blogger-escaped-all="" data-blogger-escaped-code="" data-blogger-escaped-currently:="" data-blogger-escaped-currenttide="" data-blogger-escaped-data="" data-blogger-escaped-difference=", tideDifference) #this works


lowerTide = (float(nearest1[1]) + float(nearest2[1]) - abs(tideDifference))/2
print (" data-blogger-escaped-doesn="" data-blogger-escaped-else:="" data-blogger-escaped-falling="" data-blogger-escaped-for="" data-blogger-escaped-ide="" data-blogger-escaped-is="" data-blogger-escaped-lower="" data-blogger-escaped-lowertide="" data-blogger-escaped-math.cos="" data-blogger-escaped-math.pi="" data-blogger-escaped-normalisedtime="" data-blogger-escaped-ormalised="" data-blogger-escaped-pi="next" data-blogger-escaped-print="" data-blogger-escaped-scaled="" data-blogger-escaped-t="" data-blogger-escaped-this="" data-blogger-escaped-tide="" data-blogger-escaped-tidedifference="" data-blogger-escaped-tidestate="" data-blogger-escaped-time=", gapToPrev.seconds, gapSum.seconds, normalisedTime)

print (math.cos(normalisedTime))

if tideState == " data-blogger-escaped-to="" data-blogger-escaped-urrent="" data-blogger-escaped-value="" data-blogger-escaped-work="" data-blogger-escaped-works="">

Saturday, 2 January 2016

Tide Indicator Pi Project #7 - Finding the two tide data points nearest to the current time.

This project is taking ages! I've done a lot since the last post however, but documented very little, so I'll do my best to recall how I got from there to here. You can see all the posts so far here.

The problem in a nutshell: The program needs to get the two tide data points either side of the current time, to work out what the tide is doing now.

Since the last post, the code has been modified to create a list of tuples, with each tuple having two data points (tide time, tide height)

It then works out the gap between each data point and the current time, and tries to store the two nearest times as 'nearest1' and 'nearest2'. Sometime it works:

Time Now:
2015-01-02 16:33

Output:
(datetime.datetime(2016, 1, 2, 17, 52, 40, 854958), u'4.0'),
(datetime.datetime(2016, 1, 2, 11, 9, 40, 854071), u'8.4')

Sometimes it doesn't and misses a point.



#
import urllib2
from bs4 import BeautifulSoup
from time import sleep
import datetime as dt


#open site and grab html

rawhtml = urllib2.urlopen("http://www.ports.je/Pages/tides.aspx").read(40000)
soup = BeautifulSoup(rawhtml, "html.parser")


#get the tide data (it's all in tags)

rawtidedata = soup.findAll('td')


#parse all data points (date, times, heights) to one big list
#format of the list is [day,tm,ht,tm,ht,tm,lt,tm,lt]

n=0
parsedtidedata=[]
for i in rawtidedata: 
 parsedtidedata.append(rawtidedata[n].get_text())
 n += 1

#extract each class of data (day, time , height) to a separate list (there are 10 data items for each day):

tidetimes=[]
tideheights=[]
tideday=[]
lastdayofmonth=int(parsedtidedata[-10])

for n in range(0,lastdayofmonth*10,10):

 tideday.append(parsedtidedata[n])
 tidetimes.extend([parsedtidedata[n+1],parsedtidedata[n+3],parsedtidedata[n+5],parsedtidedata[n+7]])
 tideheights.extend([parsedtidedata[n+2],parsedtidedata[n+4],parsedtidedata[n+6],parsedtidedata[n+8]])

#get time now:

currentTime = dt.datetime.now()


#create a list of all the tide times as datetime objects:

dtTideTimes=[]
tideDataList=[]

for j in range (0,lastdayofmonth*4):
 #print tidetimes[j][0:2], tidetimes[j][3:6]
 if tidetimes[j]=='**':
  dtTideTimes.append('**')
 else:
  dtTideTimes.append(dt.datetime.now().replace(day=int(j/4+1), hour=int(tidetimes[j][0:2]), minute=int(tidetimes[j][3:5])))


#create a tuple of time and height, and add each tuple to a list



 tupleHolder =(dtTideTimes[j], tideheights[j])
 tideDataList.append(tupleHolder)






#print what we've got so far



for j in range (0,lastdayofmonth*4):
 print tideDataList[j]

#find the two closest data points to now in the list:

gap1 = abs(tideDataList[0][0] - currentTime)
nearest1 = tideDataList[0]
print gap1 

for j in range (0,lastdayofmonth*4):


 if (tideDataList[j][0] !="**"):


  gap2 = abs(tideDataList[j][0] - currentTime)
  print tideDataList[j][0], gap2, nearest1


  if (gap2 < gap1):


   nearest2 = nearest1
   nearest1 = tideDataList[j]
   gap1 = gap2

print (nearest1, nearest2)
    
#this nearly works!!! Gave the two nearest high tides, not nearest high and low.