I see several issues here:
I don't understand any of the code anymore, because the undergrad has been working on it without me
I am basically doing nothing with my time
Well get on understanding it! Get the undergrad to explain to you what they've done. Or if you can't do that, sit down and spend your time working out what they've done. Research contains a lot of tedious sitting and thinking, not just creating and certainly not already understanding.
I was working on the old project for a semester
The project was entirely my advisor's idea and in fact he has expressed frustration with the fact that I don't have any ideas of my own and I am not an independent enough researcher.
If you've only been a research student for under a year then it's entirely normal that your supervisor would be providing the project. You are an apprentice researcher - your job is to learn to be an independent researcher, but you won't necessarily be at the start.
However, that doesn't mean you just wait for him to sort you out. As I said, you are meant to be learning to be an independent researcher. Spend some of your time trying to think of new projects for yourself. Read relevant papers and ask yourself what they could have done differently, or what questions are left unanswered. Think about what happens if you change one word in your original problem. Once in a while, try banging together completely random concepts, just in case you get some sparks.
I don't have any ideas of my own
It's time to start having some. Start generating ideas. For the first step, they don't have to necessarily be good ideas. Come up with lots of ideas, preferably vaguely plausible, first. Then start to think about whether of them might actually be worth testing out. Solutions don't pop into your head fully formed (generally). You have to try small steps and see where they lead. And most of them will be dead ends. Research involves trying lots of things that don't work, and finding a few that do.
I had to teach a class this summer, which consumed most of my time
I didn't make much progress because I was TAing a class while taking a notoriously difficult breadth requirement
Your full-time job is learning to be a researcher. Since most researchers will also spend a lot of time teaching, learning to teach is also a productive part of that. But if you need to teach to fund yourself to the extent you are not getting a reasonable proportion of your time for research then you are not in fact funding your studies. Do you have a realistic plan for getting all the way through your course within the time constraints?
taking medication that impaired my ability to concentrate
Try out some ways of helping yourself concentrate. Breaking everything in to small tasks is probably a good place to start, but I'm sure there are other ideas that could potentially help too. If you are having serious problems, it might be time to consider whether you need to take a break from your studies. You should also be sure your doctor is aware of the problem. It may not be possible to do anything about it, but on the other hand it might be.
My advisor doesn't seem to think I'm a very good grad student and I think he may have given up on me.
I think that's probably your biggest problem. I think you need to have a serious chat with your advisor about where he sees your research going. It may be he really has given up on you, in which case you should think about whether he is correct that this isn't the right path for you, or whether you would get on better with a change of supervisor and/or subtopic. If, on the other hand, you are wrong, you need to know that, for your own sake. Then start thinking about your own goals and motivation. You're going to need more driving you than what (you think) your advisor thinks of you. I think motivation is actually one of the hardest aspects of being a research student.