Do this in reverse to avoid gaps in the final image
To rotate a pixel around an axis, we need to do some sinus and co-sinus calculation to it because, in short, rotation follows a circular path.
The good part is that the sinus and co-sinus part of the calculation remains the same for every pixel, because they all rotate the same amount.
The next code sets up the values needed later in the main loop. And C#’s math functions want to see Radians, not degrees.
double rotationRadians = userRotate * (Math.PI / 180);
float sin = (float) Math.Cos(rotationRadians);
float cos = (float) Math.Sin(rotationRadians);
If we where to go over every pixel in the original image and calculate it’s new position, then there would be gaps in the destination image.
Therefore, we go over every pixel in the destination image, and calculate back where its original position is, that way we always have a pixel color when within the image’s border.
for (x=rectangleLeft; x<rectangleRight; x++)
for (y=rectangleTop; y<rectangleBottom; y++)
xx = x * cos - y * sin;
yy = x * sin + y * cos;
// Check if we are within the original image
if (xx > 0 && xx < imageWidth && yy>0 && yy < imageHeight)
c = lockBitmap.GetPixel((int) xx, (int) yy);
c.A = 255;
destination.SetPixel(x, y, c);
destination.SetPixel(x, y, MyColor.Black);
As you can see in the code, we loop over every pixel, apply the same sin and co-sinus values to it and end up with a source pixel-coordinate xx and yy.
Then we get the color of the pixel or assign a background color if not within the bounds of the original.
They both compare two input voltages and drive their output high or low depending on which voltage is higher. An op-amp however also has a third mode when voltages are equal, holding the voltage level.
Comparators compare input voltages
Short explanation; if the positive input voltage is greater than the negative, the output goes high, otherwise low.
The two images above show the two states a comparator can be in, either drive the output high, or low, depending on the which of the input voltages is higher.
Op-amps can deal with equal inputs
Consider the circuit above, when the op-amp starts, its output is at 0V, and because the negative input is connected to the output, the negative input is also at 0V.
Now the op-amps does what its made for, positive input is greater than the negative, so drive the output to high. I put a graph next to the circuit to show you the output voltage.
As soon as the output voltage hits the +2V level, both inputs are in balance, and so the output voltage holds at that level. If the positive input would change, the output would again follow it. This is a voltage-follower.
If we apply double our maximum voltage over two identical resistors in series, we expect to measure half our voltage at the midpoint. It is this midpoint we are going to measure.
The voltage at the top can be calculated by multiplying the analog-read-voltage by 2. In this example I will assume a 5V microcontroller.
If we add another identical resistor, we add another 5V to the maximum voltage we can read, we can extend this as many times as we like.
We do only intend to measure after the first resistor, and then multiply the measured voltage by three to get the correct voltage. This means we can group the other resistors together by adding them up.
The voltage is below ground/negative
If we want to measure below ground, we have to set this resistor network upside down. This example assumes a 3.3V microcontroller.
Instead of connecting the bottom end to the ground, we now connect the top to the maximum of our controller. And it is now after the top resistor where we read our voltage.
Once again, we simplify the network to only use two resistors by adding them up.
Calculating the actual voltage is not that complicated. First we multiply the voltage on the analog input by three and then add the lowest voltage we can read on the network, which is -6.6V. On the positive network we did not do this because that value was 0V.
So if we read +3.3V the actual value = (3.3 x 3) – 6.6 = +3.3V.
And if we read 0V the actual value = (0 x 3) – 6.6 = – 6.6V.
Both voltages are out of range
When the input can be below ground as well as above the controller’s maximum, we can’t tie one end of the network to ground or max-voltage. We need to tie it in the middle, using another resistor divider. This example assumes a 5V controller.
The standing resistors on the left are equal, and without any voltage to measure would produce +2.5V at the middle for the analog pin to read.
The to-measure-voltage applied to the right will then pull that voltage up or down through the third resistor.
Then how to calculate that third resistor? And how to translate the value read at the analog pin back to the real world value? I could not figure this out in a simple way so I wrote a tool for the Windows platform to do this for me.
This tool gives you the resistor values you need and a map function to translate the value back to real-world-voltage. At the right side you can see the simulation results.
The resistor values are very high, to use as little power as possible, but you can lower them as long as you divide all of them with the same value.
With price-aware-switches you can use power when its cheapest. Like charge your car at night if you have flexible rates or night tariff, for example.
A power-limiter on the other hand, can keep the electricity meter still for most of the day, if you have batteries or solar panels.
You need options, lots of them
Because no electric situation is the same, I am developing a range of generic solutions, both hard- and software, so you can create your own personal solution with them.
Open source and highly serviceable
With proper documentation, you can do your own installation, if you feel like it. And with that same documentation, and a well designed circuit board, most people will also be able to repair these devices themselves.
Current status: prototyping
I have two working prototypes with schematics and software. So if you like, you can start building yourselves. Otherwise you will have to wait for the finished product.
If you are one of the many people exporting electricity to the grid, chances are you are familiar with the following…
When you export electricity you also get to pay energy taxes and or other grid fees. So even if your net-consumption is zero, you still own taxes. This might not be the case in your country or your specific utilities provider, but it will be the future for most of us.
The graph above is a simplification of typical grid usage for a solar house over the course of 24 hours. At night we consume power from the grid, and at daytime we are exporting. Either way there is power crossing the meter which can potentially cost us money as taxes.
My electric water heater consumes a lot of electricity, and it does so at times when it is most expensive. This is because I have dynamic prices which change every hour and I shower at an expensive time. And even if a had a high&low tariff, the problem would still exist.
There is more than one way to solve this problem, and I prefer one that is technological in nature.
There are a couple of goals I want to keep in sight.
Use electricity when it is cheap
Zero grid consumption
Use electricity when it is cheap
With solar and wind power getting a bigger share of energy production, I can image a growing part of energy consumption being charged by the hour. I myself have such an energy contract called dynamic prices.
This means, that if you switch off your heavy electricity consumers in your house at expensive time, your money savings can be in the 10s of percents.
Zero grid consumption
In my own country (and most others) we love to tax everything. If I produce 100 kWh of power and consume 100 kWh, then I have to pay for 0 kWh and get taxed for 200 kWh. Might not be 100% accurate but we are heading in that direction fast.
With the use of batteries and smart switching, we can keep the meter at near zero net consumption most of the time. Allowing us to keep a lot of tax money that where otherwise lost.
These devices will using through hole components and circuit boards with some room to work with so that most people should be able to repair these devices themselves.
I realize that most energy situations are unique, everyone should therefore be able to customize the workings to suit their own needs.
I am going to create a smart switch to shut of my two biggest electricity consumers; my electric boiler and car. This will be done with a Raspberry Pi-ish device and a solid state relay. Might even throw in a mosfet for switching the relay.
I want to prevent as much electricity as possible from crossing the smart meter in either direction. And I am planning to use energy automation for this. It’s just regular home automation but focused on electricity usage.